SOOPAH256 Posted October 6, 2009 Share Posted October 6, 2009 (edited) I'm writing a simple expense program for class. It can take args in the form of "java program category1 expense1 category2 expense2 category3 expense3" One of the issues i'm trying to tackle is parsing the expense strings to doubles. I've tried looking around for difference solutions, but mainly I'm only finding how to turn doubles into strings. java expense Food $12.26 Hotel $140.99 Food 26.28 and it should output like this: List of Expenses: Expense: Food Total: $38.54 Expense: Hotel Total: $140.99 Total Expenses: $179.53 I currently have it working if you don't enter a dollar sign ($) with the expense, and without the duplicate expense consolidation. Do you guys have any ideas how I can solve either of these? Here's my code: import java.util.*; import java.lang.*; import java.text.*; public class expenseProg { public static void main (String args[]) { //Declarations int numExp; //Number of Expenses int numExpF; //Final Number of Expenses String[] cat; //Category Names double[] amt; //Amounts //Initializations Scanner scn = new Scanner(System.in); int count = 0; //Counts number of double total = 0; //Total Expense Cost if (args.length > 0) { numExp = args.length / 2; cat = new String[numExp]; amt = new double[numExp]; for (int i=0; i < args.length; i++) { System.out.println("Count" + count); if (i%2 == 0) { //Category cat[count] = args[i]; System.out.println("Cat" + cat[count]); } if (i%2 != 0) { //Amount amt[count] = Double.parseDouble(args[i]); System.out.println("Amt" + amt[count]); count++; } } } //Output System.out.println("List of Expenses:"); for (int j=0; j < numExp; j++) { total += amt[j]; System.out.printf(" Expense: %s Total: $%.2f\n", cat[j], amt[j]); } System.out.printf("Total Cost: $%.2f\n", total); } } Edited October 6, 2009 by SOOPAH256 Link to comment Share on other sites More sharing options...
0 Solid Knight Posted October 6, 2009 Share Posted October 6, 2009 Look at the Java API for Scanner. Use it to search for string patterns (regular expression) matching a double and do the conversion on the matched string. The benefit of using scanner is that it pretty much cannot be broken. Link to comment Share on other sites More sharing options...
0 Laurë Veteran Posted October 6, 2009 Veteran Share Posted October 6, 2009 Strings can be treated like arrays, which makes it easy to strip out things like extra symbols at the start or end. If you look in the Java API for Strings you should be able to find a bunch of methods you can use to do work with them. If you are just learning Java now is the the time to learn to find your way around the API, I find it essential. http://java.sun.com/j2se/1.5.0/docs/api/ja...ang/String.html Presumably you will need to figure out a way to test whether there is a $ sign there and if so, another method to produce a string without. if( stringName[0] == '$') might be helpful assuming that the $ is the first character in that string. As for turning a String into a double... I think the method you want is parseDouble(String s) but review all of them first in case I have that wrong. http://java.sun.com/j2se/1.5.0/docs/api/ja...ang/Double.html Edit: well if scanner can do all that then even better, it's a new one to me! Link to comment Share on other sites More sharing options...
0 Solid Knight Posted October 6, 2009 Share Posted October 6, 2009 Scanner allows you to tokenize a string based on regular expressions. I used it in my CLI spreadsheet program in my first semester of Java. It's awesome. Check out the example: http://java.sun.com/j2se/1.5.0/docs/api/ja...il/Scanner.html I noticed it has "nextDouble" built-in meaning you can just search for the next instance of a double in your string. No need to use a pattern at all. Link to comment Share on other sites More sharing options...
0 SOOPAH256 Posted October 6, 2009 Author Share Posted October 6, 2009 Thanks for your quick response Solid Knight! I actually was trying to use "nextDouble()" in line 51 of my code, but maybe I don't have it used correctly. for (int x=0; x < numExp; x++) { scn.nextLine(); System.out.print("Expense Name: "); cat[x] = scn.nextLine(); System.out.print("Enter Cost: "); amt[x] = scn.nextDouble(); } I originally was thinking to do something like what Laura was talking about, but it got kind of confusing how I would go about doing it. Link to comment Share on other sites More sharing options...
0 Solid Knight Posted October 6, 2009 Share Posted October 6, 2009 (edited) I don't have the JDK installed on my machine to test this. However, I notice that you keep telling your scanner to move to the next line every though there is no next line. Next line is looking for line breaks. Does your program hang or crash? If so, when? Also note this: "nextLine() Advances this scanner past the current line and returns the input that was skipped." So when you do cat[x].nextLine(); the scanner will return the name. Then when you have the user enter the double you tell the scanner to search line with the inputted name for a double but there is no double because the double is on the next line. Of course I'm assuming nextLine() will actually handle each individual use input as an actual new line. There might be more problems. I'll dig up my old code and see how I handled it. Looking at some of my old code I noticed that once scanner passes parts of a string you cannot go back (unless you feed scanner the string again). Think of scanner as something that is only forward looking. Edited October 6, 2009 by Solid Knight Link to comment Share on other sites More sharing options...
0 SOOPAH256 Posted October 6, 2009 Author Share Posted October 6, 2009 Here's how it runs when I use command line args: $ java expenseProg Food 5 Hotel 6 Count: 0 //These next 8 lines are checking how the args are processed Cat: Food Count: 0 Amt: 5.0 Count: 1 Cat: Hotel Count: 1 Amt: 6.0 List of Expenses: Expense: Food Total: $5.00 Expense: Hotel Total: $6.00 Total Cost: $11.00 And here's how it runs w/o args: $ java expenseProg Enter the number of expenses: 2 Expenses: 2 // This is just to check that the expenses was entered correctly Expense Name: Food Enter Cost: 5 Expense Name: Hotel Enter Cost: 6 List of Expenses: Expense: Food Total: $5.00 Expense: Hotel Total: $6.00 Total Cost: $11.00 Link to comment Share on other sites More sharing options...
0 Solid Knight Posted October 6, 2009 Share Posted October 6, 2009 (edited) Did you update the argument list to actually use Scanner? You have it in your no-arguments section only. Should be just a matter of setting your array container to equal Scanner.nextDouble(); rather than Double.parseDouble(); Of course you have to feed your Scanner the string within the array first. Though you may find it easier to just use the String.matches method and search for a double pattern (\\d*.?\\d*) then convert what is returned if anything is returned at all. Scanner would do the same thing. It's about the same amount of work. Edited October 6, 2009 by Solid Knight Link to comment Share on other sites More sharing options...
0 SOOPAH256 Posted October 6, 2009 Author Share Posted October 6, 2009 My friend helped me out with the code using .startsWith() to check for the first char being a $, then storing the substring. if (args[i].startsWith("$")) { args[i] = args[i].substring(1); } Link to comment Share on other sites More sharing options...
0 Solid Knight Posted October 6, 2009 Share Posted October 6, 2009 What are you going to do in the event they type something that isn't a number or starts with the British pounds symbol. Link to comment Share on other sites More sharing options...
0 kjordan2001 Posted October 9, 2009 Share Posted October 9, 2009 NumberFormat.getCurrencyInstance().parse("$10.15"); would be the easiest/best way I'd say. It has locale support too in case it's used in a place where commas and periods are used in reverse and should support most any currency. Link to comment Share on other sites More sharing options...
0 tiagosilva29 Posted October 10, 2009 Share Posted October 10, 2009 Floats or doubles messing with money? Use BigDecimal! Link to comment Share on other sites More sharing options...
Question
SOOPAH256
I'm writing a simple expense program for class.
It can take args in the form of "java program category1 expense1 category2 expense2 category3 expense3"
One of the issues i'm trying to tackle is parsing the expense strings to doubles.
I've tried looking around for difference solutions, but mainly I'm only finding how to turn doubles into strings.
java expense Food $12.26 Hotel $140.99 Food 26.28
and it should output like this:
List of Expenses:
Expense: Food Total: $38.54
Expense: Hotel Total: $140.99
Total Expenses: $179.53
I currently have it working if you don't enter a dollar sign ($) with the expense, and without the duplicate expense consolidation.
Do you guys have any ideas how I can solve either of these?
Here's my code:
import java.util.*; import java.lang.*; import java.text.*; public class expenseProg { public static void main (String args[]) { //Declarations int numExp; //Number of Expenses int numExpF; //Final Number of Expenses String[] cat; //Category Names double[] amt; //Amounts //Initializations Scanner scn = new Scanner(System.in); int count = 0; //Counts number of double total = 0; //Total Expense Cost if (args.length > 0) { numExp = args.length / 2; cat = new String[numExp]; amt = new double[numExp]; for (int i=0; i < args.length; i++) { System.out.println("Count" + count); if (i%2 == 0) { //Category cat[count] = args[i]; System.out.println("Cat" + cat[count]); } if (i%2 != 0) { //Amount amt[count] = Double.parseDouble(args[i]); System.out.println("Amt" + amt[count]); count++; } } } //Output System.out.println("List of Expenses:"); for (int j=0; j < numExp; j++) { total += amt[j]; System.out.printf(" Expense: %s Total: $%.2f\n", cat[j], amt[j]); } System.out.printf("Total Cost: $%.2f\n", total); } }Edited by SOOPAH256Link to comment
Share on other sites
11 answers to this question
Recommended Posts