Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: 2dArray index 00 input problem

  1. #1
    Junior Member
    Join Date
    May 2022
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 2dArray index 00 input problem

    Hello I need help in my code

    The problem is that it skips the index [0][0] and it I cannot enter a value and it displays blank.
    So here's the output:

     

    Enter row size: 3
    Enter colum size: 4

    Enter a string in location [0][0]: Enter a string in location [0][1]: asdw
    Enter a string in location [0][2]: asdw
    Enter a string in location [0][3]: asdw
    Enter a string in location [1][0]: asdw
    Enter a string in location [1][1]: asdw
    Enter a string in location [1][2]: asdw
    Enter a string in location [1][3]: asdw
    Enter a string in location [2][0]: asdw
    Enter a string in location [2][1]: asdw
    Enter a string in location [2][2]: asdw
    Enter a string in location [2][3]: asdw

    The list of the following strings are:
    asdw asdw asdw
    asdw asdw asdw asdw
    asdw asdw asdw asdw




    Here's my code:
    import java.util.Scanner;
     
    public class FSWtwo
    {
        public static void main (String[] args){
     
            Scanner sc = new Scanner(System.in);
     
            System.out.print("Enter row size: ");
            int row = sc.nextInt();
            System.out.print("Enter colum size: ");
            int column = sc.nextInt();
            System.out.println();
            String arr[][] = new String [row][column];
     
            for(int x = 0; x < row; x++){
                    for(int y = 0; y < column; y++){
                    System.out.print("Enter a string in location [" + x + "][" + y + "]: ");
                    arr[x][y] = sc.nextLine();
                    System.out.print("");
                }
            } 
            System.out.println("\nThe list of the following strings are: ");
            for(int x = 0; x < row; x++)
            {
                for(int y = 0; y < column; y++){
                    System.out.print(arr[x][y] + "\t ");
                }
                    System.out.println();
                }
            }
    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: 2dArray index 00 input problem

    The issue is in the Scanner class when calling any next... method followed by a call to nextLine.
    See https://www.geeksforgeeks.org/why-is...ext-functions/
    The solution is to call nextLine to clear the newline character before calling nextLine to read the next line of data.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    Maureen22 (June 5th, 2022)

  4. #3
    Junior Member
    Join Date
    May 2022
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: 2dArray index 00 input problem

    Thank you! it's Ok now

Similar Threads

  1. User input string's problem
    By eddieJava in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 19th, 2022, 01:07 AM
  2. Replies: 3
    Last Post: February 9th, 2019, 02:37 PM
  3. Replies: 3
    Last Post: August 26th, 2013, 04:41 PM
  4. ArrayList initial capacity problem (Index out of bounds Exception)
    By chronoz13 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: June 20th, 2011, 11:24 AM
  5. Skipping Input Problem
    By Matty Alan in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 15th, 2011, 07:45 AM