Question

In: Computer Science

Q. In java, I would like to know, especially how the program detect there is an...

Q. In java, I would like to know, especially how the program detect there is an error which is user should put only numerical data but if user put letter data (five, sixty instead of 5,60), *I want the program display "Invalid data. Please try again" instead of long error message(from the IDE)* when user put letter data(five, sixty stuff...) and GO BACK TO THE BEGINNING SO USER CAN PUT THE DATA AGAIN!

Write a program that counts change. The program should ask for the number of quarters, the number of dimes, the number of nickels and the number of pennies. The program should then compute the value of all the coins and tell the user how much money there is, expressed in dollars. Display a title at the top of the screen when the program first runs. You must use constants for the value of each coin type. Multiply the number of each coin times the value for each coin (the defined constant). Refer to the PowerPoint or video on the use of the constants.

1) if quarters =100 dimes = 100 nickels = 100 pennies = 99

=>expected result = 40.99

2) if quarters = hundred dimes = hundred nickels = nine pennies = hundred

=> expected result ""Invalid data. Please try again" (GO BACK TO THE BEGINNING, SO USER CAN PUT THE DATA AGAIN!) <- this is the part i am struggling. Please Help!

Solutions

Expert Solution

import java.util.Scanner;

public class CountChange {
   public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);
       double total = 0;
       int q, d, n, p;
       // loop to ask the user to re-enter the value
       while (true) {
           sc = new Scanner(System.in);
           try {
               //reading data
               System.out.println("Enter quarters: ");
               q = sc.nextInt();
               System.out.println("Enter dimes: ");
               d = sc.nextInt();
               System.out.println("Enter nickels: ");
               n = sc.nextInt();
               System.out.println("Enter pennies: ");
               p = sc.nextInt();
               // if all are valid breaking the loop
               break;
           } catch (Exception e) {
               System.out.println("Invalid data. Please try again");
           }

       }
       //finding total cents
       total = (q*25)+(d*10)+(n*5)+p;
       //converting into dollors
       total=total/100;
       System.out.println("Total : $"+total);
   }
}

Note : Please comment below if you have concerns. I am here to help you

If you like my answer please rate and help me it is very Imp for me


Related Solutions

Hi, I would like to test a java program. I am learning linked list and going...
Hi, I would like to test a java program. I am learning linked list and going to make a linked lists for integer nodes. For instance, I am going to add the numbers 12, 13, and 16 to the list and then display the list contents and add 15 to the list again and display the list contents and delete 13 from the list and display the list contents and lastly delete 12 from the list and display the list...
In a Java program, how could I write a program that can assign values that would...
In a Java program, how could I write a program that can assign values that would make a rock paper scissors game work? I have a program that will generate a computer response of either rock, paper, or scissors but how can I compare a user input of "rock", "paper", or "scissors" so that we can declare either the user or the computer the winner.
I would like to know what is controlled substance and how should it be wasted? and...
I would like to know what is controlled substance and how should it be wasted? and what is the plan of care for a patient with neuropathic pain? Thank you.
Hi, i would like to know how to get information of an 'image' and put it...
Hi, i would like to know how to get information of an 'image' and put it on 'C programming' I would also like to know how to compress the image information and decompress it.
I need know how to Write a java program called character length that prompts the user...
I need know how to Write a java program called character length that prompts the user to enter a string and displays: The length of the string is: xxxxx The strings first character is: x    (where xxxxx is the length of the string and x is the first character.)
I would like to know how to plot two graphs in matlab and find where they...
I would like to know how to plot two graphs in matlab and find where they intercept.
I would like to know the critical thinking portion of the questions
I would like to know the critical thinking portion of the questions
i would like to know about the marketing mix for ikea
i would like to know about the marketing mix for ikea
What is a virtual ground? I would like to know what it is
What is a virtual ground? I would like to know what it is
topic- how food travels through the body I would like to know how a salad, French...
topic- how food travels through the body I would like to know how a salad, French fries or concentrated sweets are digested.  If you were teaching a basic lesson on digestion, how would you describe the digestive process and what does the body do if eating salad or French fries, is there a difference? where hormones and enzyme information begins about how the activities vary depending on the food.   The goal is to understand what happens to the food when the body digests...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT