简体   繁体   中英

Scanner asking for input twice and only storing second input to an array

I'm making a switch case statement in java with conditions. If a person adds a project to the system between 2 and 12 months long, it will be added to an array. If the project duration is less than 2 or more than 12 the system should request them to re-enter a valid number. For some reason, my system is prompting them to enter the number twice, and only storing the second entry to the array: Image of terminal prompting twice & Image of second entry only being stored to the array .

I am thinking it's because I have 'myMonths[index] = sc.nextInt();' declared twice, but I dont know how I can get around it without it being there twice. Can anyone figure out why this is happening and how I could rectify it? Any help would be greatly appreciated. Here's my code:

           //Menu loop
            int myMonths[] = new int[5];
            int index = 0;
            while(choice !=6){


                switch (choice){
                case 1:
                //int n = number of projects
                int n = 1;
                Scanner sc = new Scanner(System.in);
                System.out.println("How many months was your project?");

                for(int i=0; i<1; i++){
                    myMonths[index] = sc.nextInt();
                    //if months is lesser than 2/greater than 12
                    if((myMonths[index] < 2) || (myMonths[index] > 12)){
                        System.out.println("Enter a number between 2 and 12 months");}

                   //if months is between 2 and 12 add it to the array
                    for(int x=0; x<1; x++){
                    //read the elements of the array
                    myMonths[index++]=sc.nextInt();}
    for(int i=0; i<1; i++){
                myMonths[index] = sc.nextInt();// put this in a variable int a = sc.nextInt();
                //if months is lesser than 2/greater than 12
                if((myMonths[index] < 2) || (myMonths[index] > 12)){
                    System.out.println("Enter a number between 2 and 12 months");}//here your are just checking if provided value is between 2 or 12 but there is no logical branching i.e. an else

               //if months is between 2 and 12 add it to the array
                for(int x=0; x<1; x++){
                //read the elements of the array
                myMonths[index++]=sc.nextInt();}//since there is no else this block will always execute

You can try something like this in your code

    for(int i=0; i<1; i++){
                int ip = sc.nextInt();
                //if months is lesser than 2/greater than 12
                if((ip < 2) || ip > 12){
                    System.out.println("Enter a number between 2 and 12 months");
    //here ask for ip again
ip = sc.nextInt();//if you wanna continue doing this until you get a valid ip use an infinite while loop with a flag for break --- see below
}else{
//code for saving ip to array whatever logic you might need
}

An example on how you could use while loop for prompting ip until it's valid

int ip;
boolean valid = false;

while(!valid){
 ip = sc.nextInt();
if(condition){
//if codition is satisfied 
valid = true;
}

//else sysout("invalid ip"); loop will continue  until you get a valid ip
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM