473,569 Members | 2,383 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 1837
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
3398
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 Pentagjetvedeh karuvificials madhla reachathe strategy in karkun campaign deshatinst terrorism. "mudivae maretu winning or losing karkun global...
4
2649
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 need to be able to do a search for specific tickets withing price ranges, different locations within the theaters, etc. etc. My problem is in the...
2
1893
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 the module I am having an issue with. Can anyone help!
15
2946
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 it. Simple database: I want to have a user enter Supply Orders (just for tracking purposes) by Item. The user may also enter a new item - "new"...
28
1876
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) 'Create command builder using the datadapter dt = New Data.DataTable da.Fill(dt) 'pour in the...
22
2163
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 grabbed, BUT it doesn't. I have tried and tried.....please help example: C:\Projects\Darryl\Queue Review Files\2-24\Cam 7\Cam7-20060224170000-01.jpg ...
15
2561
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 determine who needs to receive the text message then send the message to the address. Only problem is, the employee may receive up to 4 of the...
0
5542
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 ******************************************************** For this teeny job, please refer to: http://feeds.reddit.com/feed/8fu/?o=25
47
2832
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 help. I work in a library and send out dual language books to babies of dual or other nationality. The db is to be used for logging a range of book...
8
2758
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 chooses the number to be guessed by selecting an Integer at random in the range 1–1000. The program then displays the following text in a label: I...
0
7618
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8132
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7678
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7982
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5222
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3644
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2116
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1226
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
944
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.