1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
lara31 [8.8K]
3 years ago
7

Write a program that asks the user to input a set of floating-point values. When the user enters a value that is not a number, g

ive the user a second chance to enter the value. After two chances, quit reading input. Add all correctly specified values and print the sum when the user is done entering data. Use exception handling to detect improper inputs.
Computers and Technology
2 answers:
forsale [732]3 years ago
4 0

Answer:

See Explaination for program code

Explanation:

Code below

import java.io.*;

import java.util.*;

// Declare a class

public class DataReader

{

// Start the main method.

public static void main(String[] args)

{

// create the object of scanner class.

Scanner scan = new Scanner(System.in);

// Declare variables.

boolean done = false;

boolean done1 = false;

float sum = 0;

double v;

int count = 0;

// start the while loop

while (!done1)

{

// start the do while loop

do

{

// prompt the user to enter the value.

System.out.println("Value:");

// start the try block

try

{

// input number

v = scan.nextDouble();

// calculate the sum

sum = (float) (sum + v);

}

// start the catch block

catch (Exception nfe)

{

// input a character variable(\n)

String ch = scan.nextLine();

// display the statement.

System.out.println(

"Input Error. Try again.");

// count the value.

count++;

break;

}

}

// end do while loop

while (!done);

// Check whether the value of count

// greater than 2 or not.

if (count >= 2)

{

// display the statement on console.

System.out.println("Sum: " + sum);

done1 = true;

}

}

}

}

Lady_Fox [76]3 years ago
3 0

Answer:

Check the explanation

Explanation:

// include the necessary packages

import java.io.*;

import java.util.*;

// Declare a class

public class DataReader

{

// Start the main method.

public static void main(String[] args)

{

// create the object of scanner class.

Scanner scan = new Scanner(System.in);

// Declare variables.

boolean done = false;

boolean done1 = false;

float sum = 0;

double v;

int count = 0;

// start the while loop

while (!done1)

{

// start the do while loop

do

{

// prompt the user to enter the value.

System.out.println("Value:");

// start the try block

try

{

// input number

v = scan.nextDouble();

// calculate the sum

sum = (float) (sum + v);

}

// start the catch block

catch (Exception nfe)

{

// input a character variable(\n)

String ch = scan.nextLine();

// display the statement.

System.out.println(

"Input Error. Try again.");

// count the value.

count++;

break;

}

}

// end do while loop

while (!done);

// Check whether the value of count

// greater than 2 or not.

if (count >= 2)

{

// display the statement on console.

System.out.println("Sum: " + sum);

done1 = true;

}

}

}

}

Sample Output:

Value:

12

Value:

12

Value:

ten

Input Error. Try again.

Value:

5

Value:

nine

Input Error. Try again.

Sum: 29.0

You might be interested in
The _________ indicates the number of elements, or values, an array can hold
Stolb23 [73]
The size declarator indicates the number of elements, or values, an array can hold.It is used in a definition of an array.<span>The size declarator must be a constant integer expression with a value greater than zero. </span>A subscript is used to access a specific element in an array. 
8 0
4 years ago
Who knows songs that are sad,happy,excited, and calm
anzhelika [568]

sad-

you broke me first

can we kiss forever

love the way you lie

happy-

Say Yes

Valerie

Walking on Sunshine

excited-

who let the dogs out

September

calm-

heather

put your records on

lonely

5 0
3 years ago
.Assume the following schedule for a set of three jobs, A, B, and C, with a single CPU. Assume that there is no i/o. --A runs fi
Fiesta28 [93]

Answer:

Check the explanation

Explanation:

Kindly check the attached images below to the see the step by step explanation to the question above.

7 0
4 years ago
. Find the supplements of : 150' and 70°​
suter [353]
30 is the supplement of 150
7 0
3 years ago
How good are vw beetle heaters in winter?
Vinvika [58]
It depends on the heater but in most case it works pretty well
7 0
3 years ago
Other questions:
  • Which is a benefit of peer-to-peer networking?
    10·1 answer
  • When an application contains an array and you want to use every element of the array in some task, it is common to perform loops
    14·1 answer
  • Write down the complete AJAX code to avoid page caching.
    6·1 answer
  • Imagine you had a learning problem with an instance space of points on the plane and a target function that you knew took the fo
    11·1 answer
  • Next, Jamal wants to copy text from a Word document to the slide he just added. He outlines the steps to complete his task. Step
    15·2 answers
  • Hi Alaza, when you submit this form, the owner will be able to see
    12·1 answer
  • A question to determine your ability to use the reference materials effectively. Your boss has determined that you will be using
    9·1 answer
  • A_______ to show the working of an object before it is built or made. A. Prototype b. Test c. Evaluation d. Plan
    10·1 answer
  • What is information communication technology?
    7·2 answers
  • Sidney needs to create a decimal number variable that will be added to another number. What kind of variable is required?binaryB
    15·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!