943,915 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 2311
  • Java RSS
Nov 16th, 2008
0

need help getting program to sum numbers

Expand Post »
I wrote this program that reads data from a txt file named books,and then arranges the data by name and price. that part works fine ,my problem is that I want to sum up the total price of the books and display it. I've tried a couple of ways to do this but cannot get any to work.I dont seek help with my code untill I have tried for at least 2 hrs. After that I get frustrated. Any help will be greatly apprecited..Carl
[ICOpublic class Readafile {

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

File myFile = new File("books.txt");// Accesses books file
Scanner inputFile = new Scanner(myFile);
String line = inputFile.nextLine();
String line1 = inputFile.nextLine();// puts line & line1 on the same
// line
System.out.println(line + " " + line1);
String line2 = inputFile.nextLine();
String line3 = inputFile.nextLine();// puts line2 & line3 on the same
// line
System.out.println(line2 + " " + line3);
String line4 = inputFile.nextLine();
String line5 = inputFile.nextLine();// puts line4 & line5 on the same
// line
System.out.println(line4 + " " + line5);
String line6 = inputFile.nextLine();
String line7 = inputFile.nextLine();// puts line6 & line7 on the same
// page
System.out.println(line6 + " " + line7);
System.out.println("--------------------------");

double sum;
sum = 0.0;
while(inputFile.hasNext()){
double number = inputFile.nextDouble();
sum = sum + number;
System.out.println("Total " + sum);
}


}
}







DE][/ICODE]
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
carlcarman is offline Offline
22 posts
since Sep 2008
Nov 16th, 2008
0

Re: need help getting program to sum numbers

Here is something, that can help you:
http://www.java2s.com/Code/JavaAPI/j...ingpattern.htm
Reputation Points: 293
Solved Threads: 82
Posting Whiz
Antenka is offline Offline
361 posts
since Nov 2008
Nov 16th, 2008
0

Re: need help getting program to sum numbers

Thanks I will check it out
Reputation Points: 10
Solved Threads: 0
Newbie Poster
carlcarman is offline Offline
22 posts
since Sep 2008
Nov 16th, 2008
0

Re: need help getting program to sum numbers

[QUOTE=carlcarman;737385]Thanks I will check it out[/ That would work fine as a stand alone application but I am trying to get it to sum the numbers in my program. I know that I am very new to this but I dont see how I can make this work in my program. I am very gratefull for your help.]
Reputation Points: 10
Solved Threads: 0
Newbie Poster
carlcarman is offline Offline
22 posts
since Sep 2008
Nov 16th, 2008
1

Re: need help getting program to sum numbers

As I understood, you have combined information in your file "books.txt" (name -
string and price - double). To split information, that was read from your file you should use patterns (in example there is pattern to delimit using comma and different number of whitespaces " *" ). You also can use control symbols, such as "\n".
java Syntax (Toggle Plain Text)
  1. //we have cycle until we'll reach the symbol,
  2. //what hasn't next element
  3. while(src.hasNext()) {
  4. //if next value is double
  5. //(it is just checking next value, not goes to it)
  6. if(src.hasNextDouble()) {
  7. //we take this value and add to sum
  8. sum += src.nextDouble();
  9. }
  10. //if the next value is not double
  11. //in your case you should pass through this block
  12. else {
  13. //we take this element as string
  14. String str = src.next();
  15. //if this string equals("done"), we are
  16. //coercive breaking the cycle
  17. if(str.equals("done")) break;
  18. else {
  19. //if this is not "done" we have troubles :)
  20. System.out.println("File format error.");
  21. return;
  22. }
  23. }
  24. }
Did I answered on your question?
Last edited by Antenka; Nov 16th, 2008 at 9:49 pm.
Reputation Points: 293
Solved Threads: 82
Posting Whiz
Antenka is offline Offline
361 posts
since Nov 2008
Nov 18th, 2008
0

Re: need help getting program to sum numbers

Both of your replys were very helpful thanks.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
carlcarman is offline Offline
22 posts
since Sep 2008
Nov 18th, 2008
0

Re: need help getting program to sum numbers

Click to Expand / Collapse  Quote originally posted by carlcarman ...
public static void main(String args[]) throws IOException {
I would suggest you to drop this throws IOException. you can throw it there if you want, but what's the use? the main method is the last possible place to catch it
Reputation Points: 935
Solved Threads: 356
Nearly a Posting Maven
stultuske is offline Offline
2,497 posts
since Jan 2007
Nov 18th, 2008
0

Re: need help getting program to sum numbers

Interesting, I included it there because thats where the example in my text book had it.

We are using "Starting out with JAVA .. Early Objects". Some of the guys in class say there are better texts to begin with but this is what our instructor wanted.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
carlcarman is offline Offline
22 posts
since Sep 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: need working code in java of SMTP and pop3
Next Thread in Java Forum Timeline: Question about Arrays





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC