473,387 Members | 1,582 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

help with getting number sum

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 1829
Ganon11
3,652 Expert 2GB
"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 Expert 2GB
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
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 8TB
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
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
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
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
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
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
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
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
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
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
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
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
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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
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,...
0
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...

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.