mardi 3 mars 2015

reading a string into an int array



I want the scanner to read an input with numbers and spaces and put it into an int array, my code is as follows, but even when i enter a valid input such as "5 3 4" I still recieve the "you must specify atleast one rotor". I am a begginner so sorry if the code is messy or not exactly what I need.



String rotorConfiguration = scnr.nextLine();

Scanner readRotorConfig = new Scanner(rotorConfiguration);
int [] intRotorConfig = new int[rotorConfiguration.length()];
for (int i = 0; i < rotorConfiguration.length(); i++){
if (readRotorConfig.hasNextInt()){
int testRotorConfig = readRotorConfig.nextInt();
if (testRotorConfig >= 0 && testRotorConfig <= 8){
intRotorConfig [i] = testRotorConfig;
}else{
System.out.println("Invalid rotor. You must enter and integer"
+ " between 0 and 7");
System.exit(-1);
}

}else{
System.out.println("You must specify at least one rotor");
System.exit(-1);


}
}



Aucun commentaire:

Enregistrer un commentaire