I created a scanner class to read a text file and got the value that I need. Suppose I have a text file.
List of people: length 3
1: Fnjiei: ID 7868860: Age 18
2: Oipuiieerb: ID 334134: Age 39
3: Enekaree: ID 6106274: Age 31
I try to get the name and number and age, but every time I try to run my code, it gives me an exception. Here is my code. Any suggestion from a java guru? :) He was able to read one single line ......., but not more than one line of text.
public void readFile(String fileName)throws IOException{
Scanner input = null;
input = new Scanner(new BufferedReader(new FileReader(fileName)));
try {
while (input.hasNextLine()){
int howMany = 3;
System.out.println(howMany);
String userInput = input.nextLine();
String name = "";
String idS = "";
String ageS = "";
int id;
int age;
int count=0;
for (int j = 0; j <= howMany; j++){
for (int i=0; i < userInput.length(); i++){
if(count < 2){
if(Character.isLetter(userInput.charAt(i))){
name+=userInput.charAt(i);
}else if(userInput.charAt(i)==':'){
count++;
i++;
}
}else if(count == 2){
if(Character.isDigit(userInput.charAt(i))){
idS+=userInput.charAt(i);
}
else if(userInput.charAt(i)==':'){
count++;
i++;
}
}else if(count == 3){
if(Character.isDigit(userInput.charAt(i))){
ageS+=userInput.charAt(i);
}
}
id = Integer.parseInt(idS);
age = Integer.parseInt(ageS);
Fighters newFighters = new Fighters(id, name, age);
fighterList.add(newFighters);
}
userInput = input.nextLine();
}
}
}finally{
if (input != null){
input.close();
}
}
}
My application, if my simple code requires a change.
Edited . This gives me an exception in numerical format !!! I do not know how many empty spaces there would be between these values.