473,508 Members | 2,389 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

help with getting number sum

3 New Member
I wrote this program that reads a txt file named books that contains a list of books and prices. My program arranges the file with the book and price on the same line in the output,this works fine. I need it to sum the total cost of the books on the last line,this part does not work.If anyone could show me what I am doing wrong I wouls greatly appreciate it
Expand|Select|Wrap|Line Numbers
  1. package Readafile;
  2.  
  3. import java.io.File;
  4. import java.io.IOException;
  5. import java.util.Scanner;
  6.  
  7. /**
  8.  * @author carl carman
  9.  * 
  10.  */
  11. public class Readafile {
  12.  
  13.     public static void main(String args[]) throws IOException {
  14.  
  15.         File myFile = new File("books.txt");// Accesses books file
  16.         Scanner inputFile = new Scanner(myFile);
  17.         String line = inputFile.nextLine();
  18.         String line1 = inputFile.nextLine();// puts line & line1 on the same
  19.         // line
  20.         System.out.println(line + "----------------" + line1);
  21.         String line2 = inputFile.nextLine();
  22.         String line3 = inputFile.nextLine();// puts line2 & line3 on the same
  23.         // line
  24.         System.out.println(line2 + "------------" + line3);
  25.         String line4 = inputFile.nextLine();
  26.         String line5 = inputFile.nextLine();// puts line4 & line5 on the same
  27.         // line
  28.         System.out.println(line4 + "-----------" + line5);
  29.         String line6 = inputFile.nextLine();
  30.         String line7 = inputFile.nextLine();// puts line6 & line7 on the same
  31.         // page
  32.         System.out.println(line6 + "------------------" + line7);
  33.         System.out.println("");
  34.  
  35.         double sum = 0.0;
  36.         int x = 0;
  37.  
  38.         while (inputFile.hasNext()) {
  39.             if(x++ %2 !=0){
  40.                 sum += inputFile.nextDouble();
  41.             }
  42.             else{
  43.                 System.out.println(inputFile.next());
  44.             }
  45. }
  46.         System.out.println("total-----------------" + sum);
  47. }
  48.  
  49. }
Nov 17 '08 #1
5 1833
Ganon11
3,652 Recognized Expert Specialist
"I need it to sum the total cost of the books on the last line,this part does not work"

OK, how does it not work? Is it getting the wrong sum? Is it not getting anything? Is it printing in the wrong place?
Nov 17 '08 #2
Ganon11
3,652 Recognized Expert Specialist
Also, if your input file looks like this:
Expand|Select|Wrap|Line Numbers
  1. Book1
  2. 10.00
  3. Book2
  4. 20.00
  5. Book3
  6. 30.00
then as you are processing the lines to print them out on the same line (your mess of line1, line2, line3, etc.), and nothing remains in the input file to read into sum.
Nov 17 '08 #3
carlcarman
3 New Member
Thanks, you just pointed out what I wasn't seeing. I guess i will have to keep a running total for "my mess" to sum. This is my first java class and right now I am more concerned with keeping it simple and functional rather than "cool".
Nov 18 '08 #4
r035198x
13,262 MVP
After doing it that way, consider writing a Book class with the required fields from the file.
As you read the lines, create some Book objects and add them to an ArrayList.
You will find your Java programming days a lot easier if you always think classes and objects for every problem you get.
Nov 18 '08 #5
carlcarman
3 New Member
Thanks I will keep that in mind.
Carl
Nov 19 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

0
3381
by: abcd | last post by:
kutthaense Secretary Djetvedehald H. Rumsfeld legai predicted eventual vicmadhlary in Iraq mariyu Afghmadhlaistmadhla, kaani jetvedehly after "a ljetvedehg, hard slog," mariyu vede legai pressed...
4
2643
by: Orion | last post by:
Hi, This is kind of last minute, I have a day and a half left to figure this out. I'm working on a project using ms-sqlserver. We are creating a ticket sales system, as part of the system, I...
2
1889
by: WAYNEL | last post by:
Hi I am trying to re-write some of the example code that Agilent gives for VB to VB.Net. In .Net I keep getting the error 'cannot change the number of dimensions of an array'. I have paste...
15
2943
by: sara | last post by:
Hi I'm pretty new to Access here (using Access 2000), and appreciate the help and instruction. I gave myself 2.5 hours to research online and help and try to get this one, and I am not getting...
28
1859
by: Siv | last post by:
Hi, If I run the following: strSQL = "Select * FROM Clients;" da = New OleDb.OleDbDataAdapter(strSQL, Conn) 'Create data adapter cb = New OleDb.OleDbCommandBuilder(da) ...
22
2145
by: KitKat | last post by:
I need to get this to go to each folders: Cam 1, Cam 2, Cam 4, Cam 6, Cam 7, and Cam 8. Well it does that but it also needs to change the file name to the same folder where the file is being...
15
2551
by: Jay | last post by:
I have a multi threaded VB.NET application (4 threads) that I use to send text messages to many, many employees via system.timer at a 5 second interval. Basically, I look in a SQL table (queue) to...
0
5518
by: gunimpi | last post by:
http://www.vbforums.com/showthread.php?p=2745431#post2745431 ******************************************************** VB6 OR VBA & Webbrowser DOM Tiny $50 Mini Project Programmer help wanted...
47
2822
by: Jo | last post by:
Hi there, I'm Jo and it's the first time I've posted here. I'm in process of creating a database at work and have come a little unstuck.....I'm a bit of a novice and wondered if anyone could...
8
2752
by: Jrezak321 | last post by:
Hi I was wondering if anyone could help me with this homework problem of mine? I need to create a program that does this: Write a program that plays “guess the number” as follows: Your program...
0
7118
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7323
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7038
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7493
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5625
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5049
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4706
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3180
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
763
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.