Home > Mobile >  Nested while loops with scanner ints & sentinel values
Nested while loops with scanner ints & sentinel values

Time:02-27

So I've been a little stuck with this for a bit now. I have a program where I'm trying to run two loops, the first loop I'm running will try something until I enter a "ending" character other then my check condition.

My next loop I'm trying to read in (scanner inputs) to check them for a sentinel value (-1) and to collect and do some work with them. Like add then eventually print out after my program is complete. Inputs to read in as an example: try 1: (- 6) 5 4 7 9 -1, try 2: -9 6 9 4 -1. Numbers have spaces between.

The code works until it arrives at line "int UserValue = YN.nextInt() ;." At this point it throws an error such as exception but not sure why? I'm never able to insert an integer.

I tried creating an internal scanner with different name to see if it was my scanner but the same happened. Reading online about scanners, the embedded scanner is a BIG no no.

Well, thanks for reading and if you have any suggestions I will review them any input will be much appreciated!! Thanks!

import java.util.Scanner;  // Importing the java package for reading from users (me) 

public class InputScannerWhile {

public static void main(String args[] ) { 
    
    Scanner  YN = new Scanner(System.in) ; // create new scanner to grab scanner data       
    System.out.println("Retry: Enter Y/N " ); // enter y or n
    String retry = "y" ; // Skip the first run on the code, this will jump right into the while loop    
    
    
    while(retry.equalsIgnoreCase("Y")) { //  loop for retry or not code  
        
            String IntList = " " ;              /// output string is initially empty 
            int SumValues = 0 ;                 /// add positive vales 
                            
            System.out.println("Enter Positive integers (-1 to quit) ") ;   // Print prompt for user to
            int UserValue = YN.nextInt() ; 
            
            while(UserValue != (-1)) {
            
                if(UserValue == (-1)) {
                        break ; 
                                       } // Exit the 1st IF condition for -1 value  
                if(UserValue != (-1) )  {
                    
                    SumValues  = UserValue ; 
                    IntList  = IntList   UserValue   " " ;  
                    System.out.println("Entered numbers: "   IntList );
                    System.out.println("The Added Values: "  SumValues);
                                        }  
                
            } // Exit the second while loop                 */          
                                            
        System.out.print("Retry: Y/N " ) ;                              // enter y or n to continue 
        retry = YN.next() ;                                             // Retry variable
        
                                            } // Exit the Outside while loop
    System.out.println("!!! Program terminated !!!") ; 
                                        }
                        } //public class bracket
error details below***
    Retry: Enter Y/N 
    y
    Exception in thread "main" java.util.InputMismatchException
    at java.base/java.util.Scanner.throwFor(Scanner.java:939)
    at java.base/java.util.Scanner.next(Scanner.java:1594)
    at java.base/java.util.Scanner.nextInt(Scanner.java:2258)
    at  java.base/java.util.Scanner.nextInt(Scanner.java:2212)
    at InputScannerWhile.main(InputScannerWhile.java:18)

CodePudding user response:

you have an extra sub while loop which is unnecessary and causes a logic error. Remove this (and the corresponding close curly bracket):

 while(UserValue != (-1)) {

CodePudding user response:

Actually you are prompting the message to enter Y/N but you are not taking input from the user... So add the input taking statement before starting the loop. Also one problem will come after that you will run into an endless loop. To get that worked out, take the input for UserValue as well within that loop itself where you are checking otherwise if you once entered a number except -1 then you will run into a forever loop. These changes may get you rid out of the errors (but I don't know whether you will actually be able to achieve what you are aiming through this program since I never got what is you aim through this code...)

Also remove

if(UserValue == (-1)) {
                    break ; 

from your code since that does not have any use (since if UserValue would have been -1, the loop condition must had thrown the control out of the loop.)

  • Related