1

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

6
  • You have 13 numbers per line. Correct me if I'm wrong, but aren't there only 12 coins/bills (penny, nickle, dime, quarter, half, $1, $2, $5, $10, $25, $50, $100)? Commented Feb 20, 2019 at 20:57
  • There is also a 1 dollar coin, and 20 dollar bill (instead of 25). Commented Feb 20, 2019 at 21:01
  • the denominations, i am using penny, nickle, dime, quarter, half dollar, dollar coin, 1 dollar , 2 dollar , 5 dollar, 10 dollar, 20 dollar, 50 dollar and 100 dollar Commented Feb 20, 2019 at 21:21
  • "I am lost at the point where I want to read the data into the program to then do math and totals." I suggest that you think about how you would do this by hand. Then get a piece of paper and a pencil and write out the steps in words. Don't worry about Java syntax. After you have a clear idea of the steps, then you can attempt to translate them into Java. Commented Feb 20, 2019 at 21:28
  • I have the idea already, but its the syntax and knowledge I lack. in words, I want to take each integer of the txt file and assign a monetary value, keeping the lines as separate sets, then find a total of those values and display them per line ( i added a sample of what my output should look like) Commented Feb 20, 2019 at 21:31

1 Answer 1

1

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;
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

That looks pretty nice, the only thing is I am unable to use some of these ideas because I am limited to information in the first 5 chapters of this book. So basically, the arraylist is the best thing I can use. or I could create a class of some sort perhaps but I'm not sure. I am sorry I did not mention that before. I will edit the main post
Thank you. I ran this and it works great. I just made a minor tweak because I want to stay at whole dollar amounts. Other than that I think this is wonderful. If it is not too much trouble, could you explain how I may have used something other than collections?
@CFuentes Tomorrow I'll edit the answer to add other ways to obtain the same result.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.