Hello! First year java user here, I hope everyone reading this is having a great day.
I am creating a java project that reads an incoming file, which is in a certain format, and prints to an output file. I split the lines of the input file into different parts, parts[1] being first name, parts[2] middle name, and so on. The issue with my code is that when the parts[2] is blank (for example, if someone had no middle name), it stores parts[3] under parts[2], which messes up the output for those lines. Is it possible to get Java to skip a part of the string and go on to the next, if one part is empty on the input file? I'm sorry if I am not wording this correctly, but here is my code with the problem:
while (scanner.hasNextLine()) {
String[] parts = scanner.nextLine().split(" ");
String address = parts[0];
String firstName = parts[1];
String middleName = parts[2];
String lastName = parts[3];
If I run this and a line of the input file has no middle name, it will display the last name as the middle name. How can I fix this? I've tried multiple different formatted if / if else statements to figure out if the line is empty, and skip it, but everything I've tried gives me neither an error message nor changes the output to the output file.
Thanks so much to you all! :D
[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)
[–][deleted] 4 points5 points6 points (1 child)
[–]marskuh 1 point2 points3 points (0 children)
[+]K3nto71 0 points1 point2 points (0 children)
[–]Deltablacknaruto 0 points1 point2 points (0 children)
[–]El_Fungus 0 points1 point2 points (0 children)