This is the source code: Please incorporate the Exception Handling
import javax.swing.JOptionPane;
import java.awt.*;
public class GuessingGame {
public static void main(String[] args) {
int computerNumber = (int) (Math.random() * 10 + 1);
System.out.println("The correct guess would be " +
computerNumber);
int userAnswer = 0;
int count = 0;
while (computerNumber != userAnswer) {
count++;
String response = JOptionPane.showInputDialog(null,
"Enter a guess between 1 and 10");
userAnswer = Integer.parseInt(response);
String result = null;
if (userAnswer == computerNumber) {
result = "Correct";
} else {
if (userAnswer <= 0 || userAnswer > 10) {
result = "Invalid guess";
} else if (userAnswer > computerNumber) {
result = "Too High";
} else {
result = "Too Low;";
}
}
JOptionPane.showMessageDialog(null, result +
"\nTry Number " + count);
}
if (count == 1){
JOptionPane.showMessageDialog(null,"You must be psychic!");
} else if (count < 3){
JOptionPane.showMessageDialog(null, "Amazing");
} else if (count < 5){
JOptionPane.showMessageDialog(null,"Great Job");
} else {
JOptionPane.showMessageDialog(null, "You need some
practice");
}
}
}
Screenshot
Program
import java.util.InputMismatchException;
import javax.swing.JOptionPane;
public class GuessingGame {
public static void main(String[] args) {
//Generate random number
int computerNumber = (int)
(Math.random() * 10 + 1);
System.out.println("The
correct guess would be " + computerNumber);
int userAnswer =
0;
int count = 0;
//Loop until guess will
be correct
while (computerNumber !=
userAnswer) {
//Guess
count
count++;
try {
//Prompt for user guess
String response =
JOptionPane.showInputDialog(null,"Enter a guess between 1 and
10");
//Read user guess
userAnswer = Integer.parseInt(response);
String result = null;
if (userAnswer == computerNumber) {
result = "Correct";
}
else {
//Exception out of
range
if (userAnswer <= 0 ||
userAnswer > 10) {
//result =
"Invalid guess";
throw new
InputMismatchException("Invalid guess!Must be 1-10!!");
}
else if (userAnswer >
computerNumber) {
result =
"Too High";
}
else {
result =
"Too Low;";
}
}
JOptionPane.showMessageDialog(null, result
+"\nTry Number " + count);
}catch(NumberFormatException ex) { //Exception
when input is not integer
JOptionPane.showMessageDialog(null,"Invalid input||must be
integer!!!other than "+ex);
} catch (InputMismatchException ex) { //Out of
range excepion generator
JOptionPane.showMessageDialog(null,ex);
}
}
//Display revies result according to attempt count
if (count
== 1){
JOptionPane.showMessageDialog(null,"You must be
psychic!");
}
else if
(count < 3){
JOptionPane.showMessageDialog(null,
"Amazing");
}
else if
(count < 5){
JOptionPane.showMessageDialog(null,"Great
Job");
}
else
{
JOptionPane.showMessageDialog(null, "You need
some practice");
}
}
}
Note
Please add your second program for adding exception handling.
Here i add input mismatch and out of range exceptions
This is the source code: Please incorporate the Exception Handling import javax.swing.JOptionPane; import java.awt.*; public class...
Please help, Array does not print properly. I need to print out the 8 class numbers entered by the user ! Java only using J option pane, you cannot use ulti or anything else only J option pane Program compiles but does not print the array correctly! import javax.swing.JOptionPane; public class programtrial { public static void main(String[] args) { int newclass = 0; int countclass = 0; final int class_Max = 8; int[] classarray =...
I need help running this code. import java.util.*; import java.io.*; public class wordcount2 { public static void main(String[] args) { if (args.length !=1) { System.out.println( "Usage: java wordcount2 fullfilename"); System.exit(1); } String filename = args[0]; // Create a tree map to hold words as key and count as value Map<String, Integer> treeMap = new TreeMap<String, Integer>(); try { @SuppressWarnings("resource") Scanner input = new Scanner(new File(filename));...
Fix the following null pointer error. import java.util.*; import java.io.*; public class PhoneBook { public static void main(String[]args)throws IOException { PhoneBook obj = new PhoneBook(); PhoneContact[]phBook = new PhoneContact[20]; Scanner in = new Scanner(System.in); obj.acceptPhoneContact(phBook,in); PrintWriter pw = new PrintWriter("out.txt"); obj.displayPhoneContacts(phBook,pw); pw.close(); } public void acceptPhoneContact(PhoneContact[]phBook, Scanner k) { //void function that takes in the parameters //phBook array and the scanner so the user can input the information //declaring these variables String fname = ""; String lname = ""; String...
Write a program in Java that prompts a user for Name and id number. and then the program outputs students GPA MAIN import java.util.StringTokenizer; import javax.swing.JOptionPane; public class Main { public static void main(String[] args) { String thedata = JOptionPane.showInputDialog(null, "Please type in Student Name. ", "Student OOP Program", JOptionPane.INFORMATION_MESSAGE); String name = thedata; Student pupil = new Student(name); //add code here ...
Can you please enter this python program code into an IPO Chart? import random def menu(): print("\n\n1. You guess the number\n2. You type a number and see if the computer can guess it\n3. Exit") while True: #using try-except for the choice will handle the non-numbers #if user enters letters, then except will show the message, Numbers only! try: c=int(input("Enter your choice: ")) if(c>=1 and c<=3): return c else: print("Enter number between 1 and 3 inclusive.") except: #print exception print("Numbers Only!")...
Project 7-3 Guessing Game import java.util.Scanner; public class GuessNumberApp { public static void main(String[] args) { displayWelcomeMessage(); // create the Scanner object Scanner sc = new Scanner(System.in); String choice = "y"; while (choice.equalsIgnoreCase("y")) { // generate the random number and invite user to guess it int number = getRandomNumber(); displayPleaseGuessMessage(); // continue until the user guesses the number int guessNumber = 0; int counter = 1; while (guessNumber != number) { // get a valid int from user guessNumber...
I tried to add exception handling to my program so that when the user puts in an amount of change that is not the integer data type, the code will catch it and tell them to try again, but my try/catch isnt working. I'm not sure how to fix this problem. JAVA code pls package Chapter9; import javax.swing.*; import java.awt.*; import java.awt.event.*; public class GUIlab extends JFrame implements ActionListener { private static final int WIDTH = 300; private...
Hi, for my Java class I have built a number guessing game. I need to separate my code into two classes and incorporate a try-catch statement. Any help would be appreciated! Here is my code. import javax.swing.JOptionPane; import javax.swing.UIManager; import java.awt.Color; import java.awt.color.*; import java.util.Random; public class game { public static void main (String [] args) { UIManager.put("OptionPane.backround", Color.white); UIManager.put("Pandelbackround", Color.white); UIManager.put("Button.background", Color.white); Random nextRandom = new Random(); int randomNum = nextRandom.nextInt(1000); boolean playerCorrect = false; String keyboardInput; int playerGuess...
I'm attempting to convert ch object array to double array public void calculateSalesPrice() { double salePrice[] = new double[ch.length]; int p = 0; for (int i = 0; i < ch.length; i += 3) { p = i + 1; for(int j = 0;j } } error message:items.java:16: error: cannot find symbol for(int j = 0;j ^ symbol: variable length location: class Object items.java:17: error: array required, but Object found salePrice[j] = (double full program import java.io.File; import java.util.Scanner; import...