java: Read text file and store the info in an array using scanner class
Posted
by Amateur
on Stack Overflow
See other posts from Stack Overflow
or by Amateur
Published on 2010-03-13T21:30:56Z
Indexed on
2010/03/13
21:35 UTC
Read the original article
Hit count: 1183
Hi,
I have a text file include Student Grades like
Kim $ 40 $ 45 Jack $ 35 $ 40
I'm trying to read this data from the text file and store the information into an array list using Scanner Class
could any one guied me to write the code correctly ?
Here is what I have so far
public class ReadStudentsGrade {
public static void main(String[] args) throws IOException {
ArrayList stuRec = new ArrayList();
File file = new File("c:\\StudentGrade.txt");
try {
Scanner scanner = new Scanner(file).useDelimiter("$");
while (scanner.hasNextLine())
{
String stuName = scanner.nextLine();
int midTirmGrade = scanner.nextInt();
int finalGrade = scanner.nextInt();
System.out.println(stuName + " " + midTirmGrade + " " + finalGrade);
}
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
}
© Stack Overflow or respective owner