#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    2
    Rep Power
    0

    Help save a newb


    Edit: So I realized I am retarded and fixed the problem in the function here.

    java Code:
    	public static boolean exit() {
    		boolean exitProgram = false;
    		Scanner input = new Scanner(System.in);
    		String close = "n";
    		System.out.println("Are you sure you want to exit? (y/n)");
    		close = input.nextLine();
    			if (close.equals("y")) {
    				exitProgram = true;
    			} else {
    				exitProgram = false;
    			}
    		// input.close();
    		return exitProgram;
    	}


    However I had to comment out my close on the scanner because it gives an error on "n". So my next question is how would I close the scanner in this instance? Or better yet use the same scanner? Thanks!

    java Code:
    import java.util.Scanner;
    import java.util.Arrays;
     
    abstract class Shape {
    	private static double scaleFactor;
     
    	abstract void getArea();
    	abstract void getPerimeter();
    	abstract void scaleShape();
    }
    //
    //class Circle extends Shape {
    //	
    //}
    // 
    //class Rectangle extends Shape {
    //	
    //}
     
     
    public class lab5 {
     
    	public static boolean exit() {
    		boolean exitProgram = false;
    		Scanner input = new Scanner(System.in);
    		String close = "n";
    		System.out.println("Are you sure you want to exit? (y/n)");
    		close = input.nextLine();
    			if (close == "y") {
    				exitProgram = true;
    			} else {
    				exitProgram = false;
    			}
    		input.close();
    		return exitProgram;
    	}
     
    	public static void main(String[] args) {
     
    		boolean closeProgram = true;
    		int userOption= 0;
     
    		// Create scanner
    		Scanner input = new Scanner(System.in);
     
    		do {	
    			// User selects a menu option
    			System.out.println("Please select an option. \n" + 
    					"1. Add a new circle \n" + 
    					"2. Add a new rectangle \n" +
    					"3. Delete all Shapes \n" +
    					"4. Scale all shapes \n" +
    					"5. Display the perimeters of all shapes \n" +
    					"6. Display the areas of all shapes \n" +
    					"7. Enter scale factor \n" +
    					"8. Exit the program \n");
    			userOption = input.nextInt();
     
    				switch (userOption) {
    					case 1: 
     
    						break;
    					case 2: 
     
    						break;
    					case 3: 
     
    						break;
    					case 4: 
     
    						break;
    					case 5: 
     
    						break;
    					case 6: 
     
    						break;
    					case 7: 
     
    						break;
    					case 8: 
    						closeProgram = exit();
    						break;
    					}
     
    				// Reset vars
    				userOption = 0;
     
    		} while (closeProgram == false);
    		// Close scanner
    		input.close();
    	}
    }


    I get this error at the end:

    Code:
    Exception in thread "main" java.util.InputMismatchException
    	at java.util.Scanner.throwFor(Unknown Source)
    	at java.util.Scanner.next(Unknown Source)
    	at java.util.Scanner.nextInt(Unknown Source)
    	at java.util.Scanner.nextInt(Unknown Source)
    	at lab5.main(lab5.java:59)
    Last edited by vertroa; February 21st, 2013 at 10:54 PM. Reason: Fixed it kinda
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,714
    Rep Power
    348
    InputMismatchException
    At line 59 the Scanner class's nextInt() method tried to read a numeric input but got non-numeric data and threw the exception.

    The user should be careful to enter the right data for the program
    or the program should be careful when reading data to make sure the right data is available. The Scanner class's hasNext... methods could be used to test the type of the input.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    2
    Rep Power
    0
    Originally Posted by NormR
    At line 59 the Scanner class's nextInt() method tried to read a numeric input but got non-numeric data and threw the exception.

    The user should be careful to enter the right data for the program
    or the program should be careful when reading data to make sure the right data is available. The Scanner class's hasNext... methods could be used to test the type of the input.
    Okay, I am going to take a look at the hasNext thing. I have been only inputting ints so I don't know how it is reading non ints.

IMN logo majestic logo threadwatch logo seochat tools logo