JAVA PROGRAMMING: This is a java file from our textbook: PetFood.java Our teache
ID: 3592090 • Letter: J
Question
JAVA PROGRAMMING:
This is a java file from our textbook: PetFood.java
Our teacher wants us to update this java file by:
Use dialog boxes to allow user input and messages to the user
Have code to check for invalid user inputs (e.g., empty strings, strings instead of numbers or vice-versa, negative numbers, floats instead of integers or vice-versa, etc.)
Also, he wants us to use a command that will accept input for lower and upper case for the switch statements - i believe it might be ignorecase , but I am not sure how to use it
*** THE LAST EXPERT DID NOT READ DIRECTIONS FULLY, I NEED DIALOG BOXES FOR INPUT AND FOR SWITCH STATEMENTS, A COMMAND SHOULD BE USED FOR UPPER AN LOWER CASE... I THINK IT IS IGNORE CASE -
import java.util.Scanner; // Needed for the Scanner class
/**
This program demonstrates a switch statement.
*/
public class PetFood
{
public static void main(String[] args)
{
String input; // To hold the user's input
char foodGrade; // Grade of pet food
// Create a Scanner object for keyboard input.
Scanner keyboard = new Scanner(System.in);
// Prompt the user for a grade of pet food.
System.out.println("Our pet food is available in " +
"three grades:");
System.out.print("A, B, and C. Which do you want " +
"pricing for? ");
input = keyboard.nextLine();
foodGrade = input.charAt(0);
// Display pricing for the selected grade.
switch(foodGrade)
{
case 'a':
case 'A':
System.out.println("30 cents per lb.");
break;
case 'b':
case 'B':
System.out.println("20 cents per lb.");
break;
case 'c':
case 'C':
System.out.println("15 cents per lb.");
break;
default:
System.out.println("Invalid choice.");
}
}
}
Explanation / Answer
Hi,
i have added input validation, ignore cases and inout dialog box as requested, added commentts too to help you understand. here is the code
import java.util.Scanner; // Needed for the Scanner class
import javax.swing.*;
/**
This program demonstrates a switch statement.
*/
public class PetFood
{
public static void main(String[] args)
{
String input; // To hold the user's input
char foodGrade; // Grade of pet food
// Prompt the user for a grade of pet food via dialog box
String input=JOptionPane.showInputDialog(null,"Our pet food is available in three grades:A, B, and C. Which do you want pricing for?");
if(input=="")//check for empty input
return;
foodGrade = input.charAt(0);
if(Character.isDigit(foodGrade))
{
System.out.println("invalid input");//checking for integer input
}
// Display pricing for the selected grade.
switch(Character.toLowerCase(foodGrade))//ignoring the case by making same case
{
case 'a':
System.out.println("30 cents per lb.");
break;
case 'b':
System.out.println("20 cents per lb.");
break;
case 'c':
System.out.println("15 cents per lb.");
break;
default:
System.out.println("Invalid choice.");
}
}
}
Thumbs up if this was helpful, otherwise let me know in comments
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.