Skip to content

Instantly share code, notes, and snippets.

@andymuncey
Last active January 2, 2024 16:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andymuncey/023efb435259efee8e5a71f8e012eff2 to your computer and use it in GitHub Desktop.
Save andymuncey/023efb435259efee8e5a71f8e012eff2 to your computer and use it in GitHub Desktop.
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<>();
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());
}
}
fileScanner.close();
}
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