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.
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
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...
I would just like to know if ANOVA would be the correct test to use for...
I would just like to know if ANOVA would be the correct test to use for the following problem below? Fancy Fish, a fine dining upscale restaurant in Northridge, California and 2016 Open Table Diners’ Choice award winner, is enjoying its eighteenth season of providing delectable food, exceptional service, and beautiful outdoor dining experiences. “Saturday - Half-off Bottled Wine Night” has made Fancy Fish one of the San Fernando Valley’s favorite restaurants. Every Saturday night, guests can enjoy half-off every...
My Java program keeps "running." I know I need to close a "loop" but I can't...
My Java program keeps "running." I know I need to close a "loop" but I can't find it. I'm learning how to code. This is confusing for me. import java.util.Scanner; import java.util.ArrayList; public class SteppingStone4_Loops {    public static void main(String[] args) { Scanner scnr = new Scanner(System.in); String recipeName = ""; ArrayList<String> ingredientList = new ArrayList(); String newIngredient = ""; boolean addMoreIngredients = true; System.out.println("Please enter the recipe name: "); recipeName = scnr.nextLine();    do {    System.out.println("Would you...
Decision Making under uncertainty Hello, I would like to know the steps by steps on how...
Decision Making under uncertainty Hello, I would like to know the steps by steps on how to solve this on excel decision tree, please do not just answer the final result. Thank you Carlisle Tire and Rubber, Inc., is considering expanding production to meet potential increases in the demand for one of its tire products. Carlisle’s alternatives are to construct a new plant, expand the existing plant, or do nothing in the short run. The market for this particular tire...
I would like to know how topoisomerase 1 and 2 works e what is the difference...
I would like to know how topoisomerase 1 and 2 works e what is the difference between negative and positive supercoiling.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT