Reputation: 47
I have a file, which looks like follows:
Barack Obama 3
Michael Jackson 1
Dua Lipa 2
... all of 15 lines
I need to read from a file and place names in a one-dimensional array and integers into another, then sort everything from smallest int to biggest.
I was looking for help everywhere, I tried to google it and looked through many articles. I couldn't find the answer. That's what I tried but I know it's wrong.
import java.io.File;
import java.util.Scanner;
public class Boombastic {
public static void main(String[] args) throws Exception {
File file = new File("C:\\Users\\bobo\\Documents\\NetBeansProjects\\" + "boombastic\\boombastic.txt");
String[] fullName;
Scanner sc = new Scanner(file);
while (sc.hasNext()) {
fullName[] = sc.next();
}
}
}
Could you please help me to fix it?
Upvotes: 0
Views: 286
Reputation: 6808
Instead of trying to assign the data to an array using separators and stuff, it is better to read all lines in a String
and then process this String
. There are plenty ways of how to read the lines of a text file into a String
. Some basic of them can be found in this question.
However, I would not split the data and "store" them into Arrays since you do not know how much your data is. Also, I would choose "a more object oriented programming way" to achieve it. I would create a class, and objects of it will represent this data. After that I'd add these objects to collection and use Java's APIs to sort it within one line.
Finally, instead of new File("C:\\Users\\bobo\\Documents\\NetBeansProjects\\" + "boombastic\\boombastic.txt");
use File
class constructor to build the path. Messing with separators is yikes...
An example would be:
public class Boombastic {
public static class Person {
private String firstName, lastName;
private int id; // ??
public Person(String firstName, String lastName, int id) {
this.firstName = firstName;
this.lastName = lastName;
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Override
public String toString() {
return "Person [firstName=" + firstName + ", lastName=" + lastName + ", id=" + id + "]";
}
}
public static void main(String[] args) throws Exception {
File desktop = new File(System.getProperty("user.home"), "Desktop");
File textFileWithData = new File(desktop, "data.txt");
List<Person> persons = new ArrayList<>();
try (BufferedReader br = new BufferedReader(new FileReader(textFileWithData))) {
String line = br.readLine();
while (line != null) {
String[] lineSplit = line.split("\\s");
Person person = new Person(lineSplit[0], lineSplit[1], Integer.parseInt(lineSplit[2]));
persons.add(person);
line = br.readLine();
}
} catch (IOException e) {
System.err.println("File cannot be read.");
e.printStackTrace();
System.exit(0);
}
// print unsorted
persons.forEach(System.out::println);
Collections.sort(persons, (p1, p2) -> p1.getId() - p2.getId()); // Sort persons by ID
System.out.println();
// print sorted
persons.forEach(System.out::println);
}
}
If you are having hard time to understand the sort part, take a look at how to use comparator in java.
My text file:
John Doe 3
Someone Else 2
Brad Pitt 5
Kevin Spacey 1
Output:
Person [firstName=John, lastName=Doe, id=3]
Person [firstName=Someone, lastName=Else, id=2]
Person [firstName=Brad, lastName=Pitt, id=5]
Person [firstName=Kevin, lastName=Spacey, id=1]
Person [firstName=Kevin, lastName=Spacey, id=1]
Person [firstName=Someone, lastName=Else, id=2]
Person [firstName=John, lastName=Doe, id=3]
Person [firstName=Brad, lastName=Pitt, id=5]
Upvotes: 1
Reputation: 376
Use scanner.nextLine()
.
Also, I recommend using ArrayList because you don't know how many lines scanner has. If you do, then you can keep using arrays.
Using ArrayList:
public class Boombastic {
public static void main(String[] args) throws Exception {
File file = new File("C:\\Users\\bobo\\Documents\\NetBeansProjects\\" + "boombastic\\boombastic.txt");
ArrayList<String> fullName = new ArrayList<>();
ArrayList<Integer> ints = new ArrayList<>();
Scanner sc = new Scanner(file);
Scanner sc2 = new Scanner(file); // For the integers.
while(sc.hasNext()) {
fullName.add(sc.nextLine());
ints.add(sc2.nextInt());
}
}
}
Using arrays (assuming the scanner has 3 lines):
public class Boombastic {
public static void main(String[] args) throws Exception {
File file = new File("C:\\Users\\bobo\\Documents\\NetBeansProjects\\" + "boombastic\\boombastic.txt");
String[] fullName = new String[3];
int[] ints = new int[3];
Scanner sc = new Scanner(file);
Scanner sc2 = new Scanner(file);
int i = 0;
while (sc.hasNext()) {
fullName[i ++] = sc.next();
ints[i ++] = sc2.nextInt();
}
}
}
And to sort it, do
Arrays.sort(fullName);
Arrays.sort(ints);
This isn't the most efficient method, since it uses two scanners, but it works.
Upvotes: 2