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
Create a class named Circle with fields named radius, diameter, and area. Include a constructor that sets the radius to 1 and ca
REY [17]

Answer:

The code for this class is shown in the explanation section

Explanation:

public class Circle {

   private double radius;

   private double diameter;

   private double area;

   public Circle(double radius, double diameter, double area) {

       this.radius = 1;

       this.diameter = diameter;

       this.area = area;

   }

   public void setRadius(double radius) {

       this.radius = radius;

       diameter = radius*2;

       area= Math.PI*(radius*radius);

   }

   public double getRadius() {

       return radius;

   }

}

6 0
3 years ago
In what real-life situations might you want to use the modulus operation in a program?
KonstantinChe [14]

Answer:

Since any even number divided by 2 has a remainder of 0, we can use modulo to determine the even-ess of a number. This can be used to make every other row in a table a certain color, for example.

5 0
2 years ago
Write a function, called valFrequency, that given a list of values as a parameter, counts the frequencies for each value in the
Marat540 [252]

Answer:

pray for me i fell sick

Explanation:

i ate a piece of carrot now im blind

7 0
2 years ago
Please help with this:
Tanzania [10]

Answer:

I'm not gonna give you the answer. I want you to think about this what is math, what is science, how does it help the world, and etc. These are some example questions to help.

Explanation:

5 0
2 years ago
What operating system are you using? On most computers, you can answer this question by right-clicking on the "My Computer" icon
Viktor [21]

Explanation:

hbbbbhygjjjjkllkkjhkkky

5 0
2 years ago
Read 2 more answers
Other questions:
  • Leslie’s form collected a huge amount of data. Over time the employees felt that the applications slowed down. What should lesli
    12·1 answer
  • What is the code for loading image in matlab
    15·1 answer
  • A hierarchical topology is also called a _____ topology.
    9·1 answer
  • What category of sensory receptors are sensitive to touch sound and motion?
    10·1 answer
  • How do you check how much space is left in current drive?
    9·1 answer
  • Cual
    5·1 answer
  • What are the four steps for planning a table?​
    10·1 answer
  • How many total bits are required for a direct-mapped cache with 128 blocks and a block size of 8 bytes, assuming a 4GB main memo
    15·1 answer
  • What happens when the electrons flowing through the light bulb change direction?
    11·1 answer
  • A ____ transmits all data received to all network devices connected to it, regardless of which device the data is being sent to.
    6·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!