jeudi 28 août 2014

Scanner nextLine issue after using nextInt


Vote count:

0




I am writing a code which reads the following input



3
Ruby
Diamond
Sapphire


Here is my program



import java.util.Scanner;

public class GemStones {

private int numOfStones;
private String[] gemArray;

public void solve() throws Exception{

Scanner in = new Scanner(System.in);

//Reading the integer
numOfStones = Integer.parseInt(in.nextLine());
//in.nextLine();
System.out.println(numOfStones);


//reading the strings
for(int i=0;i<numOfStones;i++){
gemArray[i] = in.nextLine();
System.out.println(gemArray[i]);
}


for(int i=0;i<numOfStones;i++){
System.out.println(gemArray[i]);
}
in.close();
}


public static void main(String[] args) throws Exception {
GemStones check = new GemStones();
check.solve();


}

}


I have a problem reading the strings following it. Whenever I try to read the strings it shows me error! please help me..



asked 59 secs ago







Scanner nextLine issue after using nextInt

Aucun commentaire:

Enregistrer un commentaire