Need Help on Java H/W. Thanks Student 123 150 999 100 225 849 123 123 150 643 Fi
ID: 3842423 • Letter: N
Question
Need Help on Java H/W. Thanks
Student
123
150
999
100
225
849
123
123
150
643
First Name
Tim
Diablo
Tom
Buffy
LeROY
Bruce
Tom
Dick
Harry
Jane
Last Name
Smith
HO
Doe
Slayer
Jackson
Lee
Smith
Smith
HO
Doe
DOB
19960215
19900420
19860215
19960314
20001114
19560215
19890130
19880615
19870420
19860210
Address
777 Heavens Way
666 Hells Gate
999 Nevermind
456 seven
123 Right Here Alley
333 Enter the Dragon
321 zero
836 Pick Up Sticks
577 Eleven
888 Haight
Explanation / Answer
Hi, Please find my implementation.
######## input file ########
123, Tim , Smith , 19960215 , 777 Heavens Way
150, Diablo , HO , 19900420 , 666 Hells Gate
999, Tom , Doe , 19860215 , 999 Nevermind
100, Buffy , Slayer , 19960314 , 456 seven
225, LeROY , Jackson , 20001114 , 123 Right Here Alley
849, Bruce , Lee , 19560215 , 333 Enter the Dragon
123, Tom , Smith , 19890130 , 321 zero
123, Diick , Smith , 19880615 , 836 Pick Up Sticks
150, Harry , HO , 19870420 , 577 Eleven
643, Jane, Doe, 19860210 , 888 Haight
#########
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class StudentInfoFromFile {
public static void main(String[] args) throws FileNotFoundException {
Scanner sc = new Scanner(System.in);
System.out.print("Enter input file name: ");
String fileName = sc.next();
Scanner fileScanner = new Scanner(new File(fileName));
System.out.println("Student ID# First Name Last Name DOB Address");
while(fileScanner.hasNextLine()) {
String line = fileScanner.nextLine();
// splitting by comma(,)
String[] str = line.split(",");
//System.out.println(Arrays.toString(str));
System.out.println(str[0].trim()+" "+str[1].trim()+" "+str[2].trim()+
" "+str[3].trim()+" "+str[4].trim());
}
fileScanner.close();
sc.close();
}
}
/*
Sample run:
Enter input file name: mydata.txt
Student ID# First Name Last Name DOB Address
123 Tim Smith 19960215 777 Heavens Way
150 Diablo HO 19900420 666 Hells Gate
999 Tom Doe 19860215 999 Nevermind
100 Buffy Slayer 19960314 456 seven
225 LeROY Jackson 20001114 123 Right Here Alley
849 Bruce Lee 19560215 333 Enter the Dragon
123 Tom Smith 19890130 321 zero
123 Diick Smith 19880615 836 Pick Up Sticks
150 Harry HO 19870420 577 Eleven
643 Jane Doe 19860210 888 Haight
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.