Results 1 to 10 of 10
  1. #1
    toppcon is offline Member
    Join Date
    May 2011
    Posts
    46
    Rep Power
    0

    Default trouble with a mismatch exception...help please

    I need to use the hasNextDouble() method instead of using the mismatchException. It has to be a valid double and also be between 0 and 10000. I have the code running. I am getting unwanted results. I call the getValidSubtotal method and it will ask me to enter subtotal, but after I enter it, I have to enter the same number again to continue. Also, I don't think it is getting in my if or while loop?
    Can you give me some advice?

    Code:

    Java Code:
    public static double getValidSubtotal(double subtotal){
    	Scanner sc = new Scanner(System.in);
    	subtotal = 0.0;
    	System.out.println("Enter subtotal: ");
    	System.out.println();
    	subtotal = sc.nextDouble();
    
    				
    	if(sc.hasNextDouble()|| subtotal>0 || subtotal < 10000){
    		subtotal =  sc.nextDouble();
    	}else{
    		while(!sc.hasNextDouble()|| subtotal<=0 || subtotal >=10000){
    		        sc.nextLine();
    			System.out.println(); //blank line
    			System.out.println("Error! Invalid number entered. Try Again\n");
    			System.out.println(); //blank line
    			continue;
    			}
    				
    	}
    				
    	return subtotal;
    				
    	}

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    You can probably replace the if else with a do while.
    Java Code:
    do
      get input 
      if(out of range)
        print error and re prompt
    while(input is an invalid range);
    This will work since you want to get input at least once and it also handles cases where the incorrect input is entered.

  3. #3
    toppcon is offline Member
    Join Date
    May 2011
    Posts
    46
    Rep Power
    0

    Default

    It doesn't ask for it twice, now it thinks all input is invalid.


    Java Code:
    public static double getValidSubtotal(double subtotal){
    	Scanner sc = new Scanner(System.in);
    	subtotal = 0.0;
    	
    	do{
    	        System.out.println("Enter subtotal: ");
    		System.out.println();
    		subtotal = sc.nextDouble();
    
             	if(!sc.hasNextDouble()||subtotal<=0 || subtotal >= 10000)
    			sc.nextLine();
    			System.out.println(); //blank line
    			System.out.println("Error! Invalid number entered. Try Again\n");
    			System.out.println(); //blank line
    	}
    	while(!sc.hasNextDouble()|| subtotal<=0 || subtotal >=10000);
    
    				
    	return subtotal;
    	}
    is !sc.hasNextDouble ok?

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Remove the !sc.hasNextDouble(), it returns true when there isn't any input, and there generally won't be so the loop goes forever.

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    Java Code:
    	do{
    	        System.out.println("Enter subtotal: ");
    		System.out.println();
    		subtotal = sc.nextDouble();    // << Gets the double
    
                    // Following tests IF there is a next double??? Why test for more? You have one from above
             	if(!sc.hasNextDouble()||subtotal<=0 || subtotal >= 10000)
    			sc.nextLine();
    			System.out.println(); //blank line
    			System.out.println("Error! Invalid number entered. Try Again\n");
    			System.out.println(); //blank line
    	}
    	while(!sc.hasNextDouble()|| subtotal<=0 || subtotal >=10000);

  6. #6
    toppcon is offline Member
    Join Date
    May 2011
    Posts
    46
    Rep Power
    0

    Default

    It works when a number is entered just fine, but when I enter something other than a number, throws exception. I can't use a mismatchException case either.

  7. #7
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Read a line and store it as a string instead, then attempt to parse it to a double. If it throws an exception continue the loop.

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    Time to think of a design for the code before writing any more code.
    What are the steps you need to take and in what order?
    You tried to read double data before testing for it first. Hence you get an exception when bad data is entered.

  9. #9
    toppcon is offline Member
    Join Date
    May 2011
    Posts
    46
    Rep Power
    0

    Default

    Thanks for info now, but I have to go somewhere for a while. Ill try this when I get back. Thanks

  10. #10
    toppcon is offline Member
    Join Date
    May 2011
    Posts
    46
    Rep Power
    0

    Default

    I quick tried to make the changes on checking for a good double and it worked!

    Java Code:
    do{
    	System.out.println("Enter subtotal: ");
    	System.out.println();
    		if (sc.hasNextDouble())//check for good double     [COLOR="red"]<---------Changed to do check before storing[/COLOR]
    			subtotal = sc.nextDouble();
    		if(subtotal<=0 || subtotal >= 10000)//print error if out of range
    			sc.nextLine();
    			System.out.println(); //blank line
    			System.out.println("Error! Invalid number entered. Try Again\n");
    			System.out.println(); //blank line
    		}
    		while(subtotal<=0 || subtotal >=10000);//do while out of range

Similar Threads

  1. Object mismatch, not sure why
    By olddog in forum New To Java
    Replies: 1
    Last Post: 01-29-2011, 11:18 PM
  2. Type Mismatch error
    By and0rsk in forum New To Java
    Replies: 2
    Last Post: 10-10-2010, 12:16 PM
  3. Replies: 12
    Last Post: 05-06-2010, 11:54 AM
  4. Replies: 6
    Last Post: 02-21-2010, 01:54 AM
  5. Replies: 1
    Last Post: 03-08-2008, 07:41 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •