0

I could not find out the reason why this code skips scan.nextLine()... I put comment on that line... when I go the debugger and skip over it doesnt allow me to enter the String data..

import java.util.Scanner;

public class Main {

    public static void main(String[] args){
        int x =0, y=0;
        char path[][];

        Scanner scan = new Scanner(System.in);
        x = scan.nextInt();
        y = scan.nextInt();

        path = new char[x][y];
        String[] input = new String[y];



        for(int i = 0; i < x; i++){

            input[i] = scan.nextLine(); // this line is skipped...

            for(int yIndex = 0; yIndex < input.length; yIndex++){
                path[i][yIndex] = input[i].charAt(yIndex);
            }
        }



    }
XDProgrammer
  • 787
  • 2
  • 9
  • 27
  • What is the input? If `x` is less than or equal to zero, this is the correct inpterpretation. – Willem Van Onsem Jan 04 '15 at 01:53
  • The `Scanner` simply reads the first line `4 5` but doesn't automatically read the next line. You need to insert a `scan.nextLine()` before the `for` loop. – Willem Van Onsem Jan 04 '15 at 01:56
  • Hi thanks, I had added it before I post but adding scan.nextLine while not actually scanning somehow doesnt make sense to me.. is this some kind of bug in Java? – XDProgrammer Jan 04 '15 at 01:59
  • *" ... is this some kind of bug in Java"* - Nope. It works this way by design. If `nextInt()` consumed the characters in the case when they weren't a valid integer, you wouldn't have any way to reparse them as something else. – Stephen C Jan 04 '15 at 02:05

0 Answers0