0

The code I have so far is:

public class Project4 {

    public static void main (String[] args) throws IOException {

        final double OUNCES_PER_POUND = 16.0;
        double pricePerPound; 
        double weightOunces; 
        double weightPounds; 
        double totalPrice; 
        String itemName; 

        Scanner fileScan = new Scanner(new File(args[0]));

        NumberFormat money = NumberFormat.getCurrencyInstance();
        DecimalFormat fmt = new DecimalFormat("0.00");

        // Missing code that reads variables from text file goes here

        weightPounds = weightOunces / OUNCES_PER_POUND;
        totalPrice = weightPounds * pricePerPound;


        System.out.println("Item: " + itemName);
        System.out.println("Unit Price: " + money.format(pricePerPound));
        System.out.println("Weight: " + fmt.format(weightPounds) + " pounds");
        System.out.println("TOTAL: " + money.format(totalPrice));
    }

}

What I'm trying to do is figure out how to pull the variables from a text file. The file must be declared as an argument in the command line which is why the header is set up as it is. The text file is basically the three variables I need, each on a separate line.

I was hoping that someone give me a hint or point me to some info on what I need to do to set up the variables so that I can declare each line from the text file as it's own separate variable.

3
  • 1
    Take a look at docs.oracle.com/javase/1.5.0/docs/api/java/util/Scanner.html Commented Sep 27, 2012 at 22:51
  • This is useful info and I thank you, but I don't think it would work for me since there is white space in the first line. I could actually use this if I knew that the item name would be the same number of words every time and just group them, but I was hoping that there would be a way to have the code pull everything in the line. Commented Sep 27, 2012 at 23:06
  • can't you just parse the whole file into a string array using a for-loop and docs.oracle.com/javase/1.5.0/docs/api/java/util/… ? Then you can just parse the string array from there? Commented Sep 28, 2012 at 0:23

2 Answers 2

1

If the variables appear in the the simplest format possible, for example:

3.5
5.2
My Item

then you could read in the values with:

weightOunces = fileScan.nextDouble();
fileScan.nextLine();
weightPounds = fileScan.nextDouble();
fileScan.nextLine();
itemName = fileScan.nextLine();

fileScan.nextLine() is needed after the nextDouble() statements to consume the newline character.

Sign up to request clarification or add additional context in comments.

Comments

0

You can treat your variables.txt as a properties file. It need not be named .properties, you may use .txt just fine. See this link on how to read/write properties file Reading and Writing a Properties File

Comments

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.