Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
package uk.ac.chester;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
ArrayList<String> strings = new ArrayList<String>();
ArrayList<Integer> integers = new ArrayList<>();
File dataFile = new File("Data.txt");
try
{
Scanner fileScanner = new Scanner(dataFile);
while (fileScanner.hasNext()){
if (fileScanner.hasNextInt()){
integers.add(fileScanner.nextInt());
fileScanner.nextLine();
} else
{
strings.add(fileScanner.nextLine());
}
}
}
catch (FileNotFoundException e) {
System.out.println("Cannot read from file");
}
System.out.println("Strings:");
for (String string:strings)
{
System.out.println(string);
}
System.out.println(System.lineSeparator() + "Integers:");
for (Integer integer: integers){
System.out.println(integer);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment