0

My code is as follows

import java.util.*;
import java.io.*;

class Main {
  public static int MAXROUTINE = 10;    //max number of routines
  public static int MAXDANCERS = 26;    //and dancers for test case
  
  
  public static void main(String[] args) {
    Scanner scnr = new Scanner(System.in);
    int r = scnr.nextInt();      //num routines, R
    //get routine info
    int recital[][] = new int[MAXROUTINE][MAXDANCERS];    //initialize default array
    
    for(int i=0; i<r; i++){
      String routine = scnr.nextLine();        //get the routine
      
      for(int j=0; j<routine.length(); j++){   //split routine
        recital[i][j] = routine.charAt(j);     //by dancer
        System.out.println(i + ", " + j);
      }
    }
    scnr.close();
  }//end of main
}

However, it appears that the first instance of the nested loop has i=1. When given input

5
ABC
ABEF
DEF
ABCDE
FGH

The code outputs

1, 0
1, 1
1, 2
.
.
.

Because of this recital[0][] is not being altered. Why is i being incremented, and how do I make it not increment on that initial loop?

idgit
  • 11
  • 3
  • Stepping through your code with the debugger would have revealed the problem. – tgdavies Feb 17 '22 at 02:31
  • The first call to nextline() is returning a line of length 0, in which case you print nothing for that line. This is part of the common problem of not understanding where the 'nextXyz()' methods leave the input stream position. – passer-by Feb 17 '22 at 02:32

0 Answers0