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

    Join Date
    Oct 2012
    Posts
    3
    Rep Power
    0

    Trouble with array, code runs but skips my first array value for some reason


    In our computer science class, we are suppose to make a program which has 4 functions when you press 1 it allows user to input 10 values into an array and stores it when you press 2 it shows you the entire array value and when you press 3 user inputs a value and gives you the value assigned to the array number and four is exit. Anyways, i am stuck on the first function because of a weird error. my code runs but for some reason when i type in one, it skips my first input value and goes onto the second one. It is strange because it only does it for the first value. Any help would be appreciated. Here is the code:

    import java.util.Scanner;

    public class a {


    public static void main(String[] args) {

    // TODO code application logic here
    Scanner keyboard = new Scanner(System.in);
    int caseNum, arrayNum, inputNum, arrayDisplay,user = 0;
    arrayNum = 0;
    caseNum = 0;
    arrayDisplay = 0;
    inputNum = 0;
    String[] input = new String[10000000];



    while (caseNum != 4){
    System.out.println("Please put a case number ");
    caseNum = keyboard.nextInt();
    arrayDisplay = 0; // reset variable


    // what happens when you press one
    if (caseNum == 1) {
    arrayNum += 10;
    while (inputNum < arrayNum) {
    System.out.println("Please input a string");
    input [inputNum] = keyboard.nextLine();// problem it skips the first input array value for some reason
    inputNum += 1;
    }
    }
    }
    }
    }
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2010
    Posts
    42
    Rep Power
    4
    Code:
    while (caseNum != 4){
                System.out.println("Please put a case number ");
                caseNum = keyboard.nextInt();
                keyboard.nextLine();
                arrayDisplay = 0; // reset variable
    
                // what happens when you press one
                if (caseNum == 1) {
                    arrayNum += 10;
                    while (inputNum < arrayNum) {
                        System.out.println("Please input a string");
                        input [inputNum] = keyboard.nextLine();// problem it skips the first input array value for some reason
                        inputNum += 1;
                    }
                }
            }
    Adding the line highlighted in green will fix your problem. The reason for this is because when you input 1 on the keyboard, you then push enter. So your input is "1\n". The next time you call keyboard.nextLine() it will read the empty line.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    3
    Rep Power
    0
    thank you it worked

IMN logo majestic logo threadwatch logo seochat tools logo