Question: Hello , i need help in java , i need help to add exception handling InputMismatchException to my code this my source code public class
Hello , i need help in java , i need help to add exception handling InputMismatchException to my code
this my source code
| public class lab2 { public static void main(String[] args) { Numbers obj = new Numbers(); Scanner sc = new Scanner(System.in); while (true) {// infinte loop System.out.print("Please select one of the following: " // added extra options + "1: Initialize a default array " + "2: To specify the max size of the array " + "3: Add value to the array " + "4: Display values in the array " + "5: Display the average of the values " + "6: Enter multiple values " + "7: Read values from file " + "8: Save values to file " + "9: To Exit " + "> "); int choice = sc.nextInt();// taking user choice sc.nextLine(); switch (choice) { // showing result as per user choice case 1: System.out.println("New array initialized"); obj = new Numbers(); break; case 2: System.out.print("Enter new size of the array: "); int size = sc.nextInt(); sc.nextLine(); obj = new Numbers(size); break; case 3: obj.addValue(sc, true); break; case 4: System.out.println("Numbers are: " + obj); break; case 5: System.out.println("Average is: " + obj.calcAverage()); break; case 6: // adding multiple values from keyboard obj.addMultiple(sc, true); break; case 7:// loading values from file obj.loadData(sc); break; case 8:// saving values to file obj.saveData(sc); break; case 9: System.out.println("Good Bye"); System.exit(0);// exiting the program default: System.out.println("Invalid choice!"); //if we enter wrong choice break; } } } } | import java.io.File; import java.io.FileNotFoundException; import java.io.PrintWriter; import java.util.Scanner; public class Numbers { private float[] numbers; // default values private int numItems = 0, size = 1; public Numbers() { numbers = new float[size];// default size } public Numbers(int size) { // user size this.size = size; numbers = new float[size]; } // modified the method to accept one extra argument for denoting if we are // reading from a keyboard or not public void addValue(Scanner scanner, boolean fromKeyboard) { // displaying prompt only if we are reading from keyboard if (fromKeyboard) System.out.print("Enter value: "); float value = scanner.nextFloat(); numbers[numItems++] = value; // dynamic array implementation if (numItems >= size) { // increasing the size of the array by 5 when array is full float[] temp = new float[size + 5]; // copying the array for (int i = 0; i < numItems; i++) { temp[i] = numbers[i]; } numbers = temp; } } public float calcAverage() { float sum = 0; for (int i = 0; i < numItems; i++) { sum += numbers[i];// calculating sum of the numbers in the array } return (float) sum / numItems;// calculating average } @Override public String toString() { String data = ""; for (int i = 0; i < numItems; i++) { data += numbers[i] + " "; } return data; } // method to add multiple values from keyboard or from file based on value // of fromKeyboard flag public void addMultiple(Scanner scanner, boolean fromKeyboard) { if (fromKeyboard) { System.out.print("How many values do you wish to add? "); } // reading number of values int n = scanner.nextInt(); // looping and calling addValue n times for (int i = 0; i < n; i++) { addValue(scanner, fromKeyboard); } } // method to load data from file to array public void loadData(Scanner scanner) { // reading name of file, assuming file name contain no spaces System.out.println("Name of the file to read from:"); String name = scanner.next(); try { // scanner to read from file Scanner fileScanner = new Scanner(new File(name)); // simply calling addMultiple with fileScanner and fromkeyboard to // false addMultiple(fileScanner, false); } catch (FileNotFoundException e) { System.out.println("File not found!"); } catch (Exception e) { System.out.println("Invalid file format!"); } } // method to save data to a file public void saveData(Scanner scanner) { // reading name of file, assuming file name contain no spaces System.out.println("Name of the file to save to:"); String name = scanner.next(); try { // printwriter to write the contents to file PrintWriter writer = new PrintWriter(new File(name)); // writing number of items writer.println(numItems); // writing each value to file for (int i = 0; i < size; i++) { writer.println(numbers[i]); } // closing file, saving changes writer.close(); } catch (FileNotFoundException e) { System.out.println("File cant be opened!"); } } } |
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
