Question

Complete the following code snippet using java so that it finds the sum of doubles the...

Complete the following code snippet using java so that it finds the sum of doubles the user inputs. Your answer should be a while loop with a condition that needs to be true if the user inputs a double. Your answer should be a single while loop.

Scanner scnr = new Scanner(System.in);

int sum = 0;

0 0
Add a comment Improve this question Transcribed image text
Answer #1

ANSWER: Here below is the code where I am taking input as a double so when input a string input the while will break and print the sum and since you need to sum of the double values so sum should be double type not int. Please like it or comment your problem before going to downvote.

CODE:

import java.util.Scanner;

public class Demo {

   public static void main(String[] args) {
       Scanner scnr = new Scanner(System.in);

       double sum = 0;
           while(scnr.hasNextDouble()) {//here I am checking that user input is double or not.
               double in=scnr.nextDouble();// taking input
               sum+=in;//adding into sum.
           }
          
           System.out.println("Sum:"+sum);//displaying sum
   }

}

OUTPUT:

Add a comment
Know the answer?
Add Answer to:
Complete the following code snippet using java so that it finds the sum of doubles the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Answer in JAVA 1. Complete the method definition to output the hours given minutes. Output for...

    Answer in JAVA 1. Complete the method definition to output the hours given minutes. Output for sample program: 3.5 import java.util.Scanner; public class HourToMinConv {    public static void outputMinutesAsHours(double origMinutes) {       /* Your solution goes here */    }    public static void main (String [] args) {       Scanner scnr = new Scanner(System.in);       double minutes;       minutes = scnr.nextDouble();       outputMinutesAsHours(minutes); // Will be run with 210.0, 3600.0, and 0.0.       System.out.println("");    } } 2....

  • Complete the do-while loop to output 0 to countLimit. Assume the user will only input a...

    Complete the do-while loop to output 0 to countLimit. Assume the user will only input a positive number. import java.util.Scanner; public class CountToLimit { public static void main (String [] args) { Scanner scnr = new Scanner(System.in); int countLimit = 0; int printVal = 0; // Get user input countLimit = scnr.nextInt(); printVal = 0; do { System.out.print(printVal + " "); printVal = printVal + 1; } while ( /* Your solution goes here */ ); System.out.println(""); return; } }

  • Complete the following Java class. In this class, inside the main() method, user first enters the...

    Complete the following Java class. In this class, inside the main() method, user first enters the name of the students in a while loop. Then, in an enhanced for loop, and by using the method getScores(), user enters the grades of each student. Finally, the list of the students and their final scores will be printed out using a for loop. Using the comments provided, complete the code in methods finalScore(), minimum(), and sum(). import java.util.Scanner; import java.util.ArrayList; public class...

  • (a)How many times does the code snippet given below display "Hello"? int x = 1; while...

    (a)How many times does the code snippet given below display "Hello"? int x = 1; while (x != 15) {    System.out.println ("Hello");    x++; } (b)What is the output of the following code fragment? int i = 1; int sum = 0; while (i <= 5) {    sum = sum + i;    i++; } System.out.println("The value of sum is " + sum); Quie 2 What is the output of the following snipped code? public class Test {...

  • Hey I need help on Java. I have to create a while loop that reads 5 fields in each row, nextInt, ...

    Hey I need help on Java. I have to create a while loop that reads 5 fields in each row, nextInt, nextInt, next, next, and nextLine. Then I have to instantiate a User object using those 5 fields and insert that object in the usersArr. I just need to see what this would look like if a text file with 5 fields for 5 users existed. Here's my code so far: int [] usersArr = new int[200];           System.out.print("Enter file name:...

  • What will be the value of x after the following code is executed? int x =...

    What will be the value of x after the following code is executed? int x = 5; while (x <50) { x += 5; } a) 50 b) 45 c) Infiniteloop d) 55 Given the following code segment: Scanner in = new Scanner (System.in); boolean done = false; while (!done) { int input = in.next(); if(input.equalsIgnoreCase("q")) done = false; } What will cause the loop to terminate? a) When the user enters -1 b) The loop will never terminate c)...

  • JAVA

    Write a Java class (program) which inputs a simple integer and outputs a list of values from the input variable down to 0 (not including 0). You should only show every other value in the output, starting with the input value. You must use a for loop.For example, if the input is 23, the output should be:The input value was 2323 21191715131197531Code given:import java.util.Scanner;/**  * This program counts down from the input number down to (but not including)   * 0, skipping...

  • Please edit my following JAVA code so that there is no main function. I would like...

    Please edit my following JAVA code so that there is no main function. I would like one function in this class that completes what the current program is trying to do so that I can call this class in my main class which will be separate. import java.math.RoundingMode; import java.text.DecimalFormat; import java.util.Scanner; public class enterwklyincme { private static DecimalFormat df = new DecimalFormat("0.00"); public static float readFloat(Scanner reader) { float num; while (true) { try { num = Float.parseFloat(reader.nextLine()); return...

  • a Java code Complete the provided code by adding a method named sum() to the LinkedList...

    a Java code Complete the provided code by adding a method named sum() to the LinkedList class. The sum() method should calculate the sum of all of the positive numbers stored in the linked list. The input format is the number of items in the list, followed by each of the items, all separated by spaces. Construction of the linked list is provided in the template below. The output should print the sum of the positive values in the list....

  • Computer Science - Java Explain the code step by step (in detailed order). Also explain what...

    Computer Science - Java Explain the code step by step (in detailed order). Also explain what the program required. Thanks 7 import java.io.File; 8 import java.util.Scanner; 9 import java.util.Arrays; 10 import java.io.FileNotFoundException; 12 public class insertionSort 13 14 public static void insertionSort (double array]) 15 int n -array.length; for (int j-1; j < n; j++) 17 18 19 20 21 double key - array[j]; int i-_1 while ( (i > -1) && ( array [i] > key array [i+1] -...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT