so I am trying to read and split the following data up into an array. However, the txt file only has one line.
1, 5, 3, 7, 9, 5, 3, 1, 9, 5, 7, 3, 7, 5, 9, 1, 3, 5
And I am not sure if I am correctly doing it with my code.
try {
File cars = new File("cars.txt");
Scanner myReader = new Scanner(cars);
String data = myReader.nextLine();
while (myReader.hasNextLine()) {
data = myReader.nextLine();
String values[] = data.split(",");
System.out.print(values);
}
CodePudding user response:
How to read in values from a single line in a txt file
I would do it like this.
[\\s ,]
- use a delimiter of any mix of whitespace and commas (including multiple commas).
try (Scanner myReader = new Scanner(new File("f:/cars.txt"))) {
myReader.useDelimiter(Pattern.compile("[\\s ,] "));
while (myReader.hasNextInt()) {
System.out.println(myReader.nextInt());
}
} catch (IOException ioe) {
ioe.printStackTrace();
}
CodePudding user response:
I'd do it something like this without too much error handling or complex stuff. This is tested with a one line cars.txt and a more than one line cars.txt. Have fun with it!
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
public class Main {
public static void main(String args[]) throws Exception {
File cars = new File("cars.txt");
List<Integer> numbers = new ArrayList<>();
Scanner myReader = new Scanner(cars);
while (myReader.hasNextLine()) {
processLine(myReader.nextLine(), numbers);
}
myReader.close();
System.out.println(Arrays.deepToString(numbers.toArray()));
}
private static void processLine(String oneline, List<Integer> numbers) {
String values[] = oneline.split(",");
Arrays.stream(values).forEach((value) -> numbers.add(Integer.parseInt(value.trim())));
}
}
Or maybe for beginners this would be better to look at:
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
public class Main {
public static void main(String args[]) throws Exception {
File cars = new File("cars.txt");
List<Integer> numbers = new ArrayList<>();
Scanner myReader = new Scanner(cars);
while (myReader.hasNextLine()) {
processLine(myReader.nextLine(), numbers);
}
myReader.close();
System.out.println(Arrays.deepToString(numbers.toArray()));
}
private static void processLine(String oneline, List<Integer> numbers) {
String numbersAsStrings[] = oneline.split(",");
for (int loopIndex = 0; loopIndex < numbersAsStrings.length; loopIndex ) {
String oneNumberAsString = numbersAsStrings[loopIndex].trim();
Integer oneNumber = Integer.parseInt(oneNumberAsString);
numbers.add(oneNumber);
}
}
}
CodePudding user response:
Another example. BufferedReader, try-with-resources, and stream.
File file = new File("src/defaultpackage/cars.txt");
try (BufferedReader br = new BufferedReader(new FileReader(file))) {
String line = null;
while ((line = br.readLine()) != null) {
Stream<String> stream = Arrays.stream(line.split(","));
Object[] arr = stream.toArray();
for (int i = 0; i < arr.length; i ) {
System.out.println(arr[i]);
}
}
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("end of file");