How do I save some text from a file to an array using scanner?
John
import java.util.regex.*; import java.util.*; import java.io.File; public class Program { public static void main(String[] args) { List<String> lstNames = new ArrayList<>(); List<String> lstValues = new ArrayList<>(); try { File file = new File("data.txt"); Scanner input = new Scanner(file); Pattern pattern = Pattern.compile( "^(.*)\\s(\\d*)$"); while(input.hasNextLine()) { String line = input.nextLine(); Matcher matcher = pattern.matcher(line); if (matcher.find()) { lstNames.add(matcher.group(1)); lstValues.add(matcher.group(2)); } } input.close(); } catch (Exception ignore) { System.err.println(ignore.getMessage()); } String[] array1 = lstNames.toArray( new String[ lstNames.size()]); String[] array2 = lstValues.toArray( new String[ lstValues.size()]); } }