Reputation: 15
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
Upvotes: 1
Views: 628
Reputation: 21134
A hint for a nice strategy. That's valid if the total per line has to be in Dollars. Just know that with double
you might lose some precision.
Edited to comply to OP prerequisites (only ArrayList
).
try (final Scanner data = new Scanner(file)) {
final int lines = data.nextInt();
final Collection<Double> totalPerLine = new ArrayList<>(lines);
for (int i = 0; i < lines; i++) {
double sum = 0;
for (int j = 0; j < 13; j++) {
final int value = data.nextInt();
sum += getDollars(j, value);
}
totalPerLine.add(sum);
System.out.println("Line " + i + ": $" + sum);
}
}
private static double getDollars(
final int type,
final int value) {
switch (type) {
case 0: // Penny
return value / 100D;
case 1: // Nickle
return value / 20D;
case 2: // Dime
return value / 10D;
case 3: // Quarter
return value / 4D;
case 4: // Half
return value / 2D;
case 5: // Dollar coin
case 6: // Dollar bill
return value;
case 7: // Two dollars bill
return value * 2D;
case 8: // Five dollars bill
return value * 5D;
case 9: // Ten dollars bill
return value * 10D;
case 10: // Twenty dollars bill
return value * 20D;
case 11: // Fifty dollars bill
return value * 50D;
case 12: // A hundred dollars bill
return value * 100D;
default:
return 0;
}
}
Upvotes: 1