Is there another way to re-write without this using the \".split\" that is easy
ID: 3719939 • Letter: I
Question
Is there another way to re-write without this using the ".split" that is easy and basic for a beginner? It's reading in a text file like this: Bob/Morris/123 Sunny Street/Jacksonville, FL/113.1
//Goes through the loop until the plane reaches capacity.
//16 passengers distributed by weight.
while(scanner.hasNextLine() && count < 16)
{
String info = scanner.nextLine();
String line = info;
String[] full_info = line.split("/");
if(full_info.length == 5)
{
String full_name = full_info[0] + " " + full_info[1];
String street_address = full_info[2];
String[] city_state = full_info[3].split(",");
String city = city_state[0];
String state = city_state[1];
double weight = Double.parseDouble(full_info[4]);
String seat = getSeatNumber(passengers, weight);
Passengers passenger = new Passengers(full_name, street_address, city, state, weight, seat);
passengers[count] = passenger;
count++;
}
}
Explanation / Answer
Easiest way to do it is to use the split method It is basic java, and you should learn it. It's very simple.. line.split('/') splits the line into an array of words seperated by /
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.