I am trying to make a program that takes the total number of different types of US coins, and adds their values up for a total. The coin info comes from a txt file. The first line is a single integer that indicates the number of data sets. Each data set is a single line with 13 integers separated by a single space. Each integer represents a number of coins or bills. The first is pennies, then nickles, all the way up to hundred dollar bills.
I am lost at the point where I want to read the data into the program to then do math and totals. I think I need to use arraylists for each data set, but so far, all I can figure out is how to load the entire txt file(after the first int) into a single array list, instead of a set of arraylists equal to the number of data sets (5 per the file). I am new to java and programming in general so any help is appreciated.
I figure once I can get the values loaded, I can start adding them up pretty easily, but I am very lost now.
Here is what the txt file shows:
5
4 0 2 3 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0
1 2 3 4 5 6 7 8 9 10 11 12 13
4 0 2 3 10 10 2 1 2 1 10 1 100
10 10 10 10 5 5 5 5 2 2 2 2 1
Here is my code so far:
import java.io.File;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class CountDollarsCF {
public static void main(String[] args) throws FileNotFoundException {
System.out.println("Enter file path.");
Scanner reader = new Scanner(System.in);
String input = reader.nextLine();
File file = new File("src/" + input);
Scanner data = new Scanner(file);
int sets = 0;
sets = data.nextInt();
ArrayList<Integer> datasets;
datasets = new ArrayList<>();
while(data.hasNextInt()){
datasets.add(data.nextInt());
}
data.close();
}
}
I should add that I am limited with the tools I can use. I am only on chapter 5 of Java Software Solutions by Lewis and Loftus. So we haven't learned maps, conversions, hashmaps or collections. We just got to array lists, and creating some classes. I will be truncating to the nearest whole dollar, and the goal is to output something like this:
Line 1: $0
Line 2: $0
Line 3: $2297
Line 4: $10289
Line 5: $296