0

Here's my code, my problem is that after I enter the values for rowNum and colNum, it displays the "Please enter a value" prompt twice and fills the first part w/ a space it seems like. Help would be appreciated!

import java.util.*;

public class Lab11 {
    public static void main(String[] args){

        // A scanner object for requesting input from the user
        Scanner scan = new Scanner(System.in);
        // An integer for the number of rows.
        int rowNum;
        // An integer for the number of columns.
        int colNum;
        //a string search for the string to search for
        String search;

     // Print this message "Enter the number of rows in the array" //
        System.out.print("Enter the number of rows in the array" );
        rowNum = scan.nextInt();
     // Print this message "Enter the number of columns in the array //
        System.out.print("Enter the number of columns in the array");
        colNum = scan.nextInt();
     // Use the scanner to store the values entered by the user
     // in the integers declared above.

     // Declare a 2D String array using the number of rows and columns previously entered by the user
     String[][] stringArray = new String[rowNum][colNum];

     for (int i = 0; i < stringArray.length; i++){
         for (int j = 0; j < stringArray[i].length; j++){
             System.out.println("Please enter a value");
             stringArray[i][j] = scan.nextLine();
         }
     }

     for (int ii = 0; ii < stringArray.length; ii++){
           for (int jj = 0; jj < stringArray[ii].length; jj++){
               System.out.print(stringArray[ii][jj]+" ");
           }
           System.out.println();
     }

     System.out.println("Please enter the string you are searching for");
     search = scan.nextLine();
     for (int iii = 0; iii < stringArray.length; iii++){
         for (int jjj = 0; jjj < stringArray[iii].length; jjj++){
             if(stringArray[iii][jjj].equals(search)){
                 System.out.println("Element found at position ("+iii+","+jjj+")");
             }
         }
     }



    }

}
Piyush Gupta
  • 2,103
  • 3
  • 11
  • 28
  • Take a look at this: http://stackoverflow.com/a/13102066/1552587 – Titus Apr 14 '16 at 08:36
  • Something different to your question, you might want to change the way you comment your code. Comments are for clarification. What you do is essentially repeat the line of code - "An integer for the number of columns", int colNum. In this case, it is completely clear from the name of the variable for what it stands, doesn't require a comment. Use comments when something is not obvious, like before a big block of code that does strange stuff. In your code, you can basically delete all comments. – Aziuth Apr 14 '16 at 08:52

1 Answers1

0

Thats because the scan.nextInt(); method does not consume the last newline character of your input, and thus that newline is consumed in the next call to scan.nextLine so you need to use like this way Integer.parseInt(scan.nextLine()); here we are doing typecasting and using nextLine() method instead of nextInt(). and scan is object of Scanner class.

So, I modified your code it is working fine,

public class Test {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int rowNum;
        int colNum;
        String search;

        System.out.print("Enter the number of rows in the array");
        rowNum = Integer.parseInt(scan.nextLine());
        System.out.print("Enter the number of columns in the array");
        colNum = Integer.parseInt(scan.nextLine());

        String[][] stringArray = new String[rowNum][colNum];

        for (int i = 0; i < stringArray.length; i++) {
            for (int j = 0; j < stringArray[i].length; j++) {
                System.out.println("Please enter a value");
                stringArray[i][j] = scan.nextLine();
            }
        }

        for (int ii = 0; ii < stringArray.length; ii++) {
            for (int jj = 0; jj < stringArray[ii].length; jj++) {
                System.out.print(stringArray[ii][jj] + " ");
            }
            System.out.println();
        }

        System.out.println("Please enter the string you are searching for");
        search = scan.nextLine();
        for (int iii = 0; iii < stringArray.length; iii++) {
            for (int jjj = 0; jjj < stringArray[iii].length; jjj++) {
                if (stringArray[iii][jjj].equals(search)) {
                    System.out.println("Element found at position (" + iii + "," + jjj + ")");
                }
            }
        }

    }

}
Piyush Gupta
  • 2,103
  • 3
  • 11
  • 28