Finding the sum of the file input

  • (2 Pages)
  • +
  • 1
  • 2

22 Replies - 1126 Views - Last Post: 10 October 2011 - 08:43 PM Rate Topic: -----

#1 Lemonade4sale  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 10-October 11

Finding the sum of the file input

Posted 10 October 2011 - 07:06 PM

Okay, so I am suppose to find the sum of the numbers given in the file input. I do not know how to find the sum without directly putting in the numbers given. Any help would be appreciated, because right now I am stuck.
Is This A Good Question/Topic? 0
  • +

Replies To: Finding the sum of the file input

#2 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7744
  • View blog
  • Posts: 13,083
  • Joined: 19-March 11

Re: Finding the sum of the file input

Posted 10 October 2011 - 07:13 PM

I'm going to assume that you're not looking for someone to write the code for you, and I'm going to assume that nobody's going to write the code for you. If either of those assumptions is wrong, I'll close this thread before you can say Jack Robinson.

That being said, I want you to start by telling us where you're starting from.

How do you think this problem gets solved? Break it down into steps, so you can solve it (with a little help, but you're going to be doing the solving)

This post has been edited by jon.kiparsky: 10 October 2011 - 07:14 PM

Was This Post Helpful? 0
  • +
  • -

#3 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10563
  • View blog
  • Posts: 39,087
  • Joined: 27-December 08

Re: Finding the sum of the file input

Posted 10 October 2011 - 07:21 PM

Are you familiar with the Scanner class? That's a good place to start.
Was This Post Helpful? 1
  • +
  • -

#4 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7744
  • View blog
  • Posts: 13,083
  • Joined: 19-March 11

Re: Finding the sum of the file input

Posted 10 October 2011 - 07:24 PM

Scanner will be very useful here, yes.
Was This Post Helpful? 0
  • +
  • -

#5 Lemonade4sale  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 10-October 11

Re: Finding the sum of the file input

Posted 10 October 2011 - 07:24 PM

View PostLemonade4sale, on 10 October 2011 - 07:06 PM, said:

Okay, so I am suppose to find the sum of the numbers given in the file input. I do not know how to find the sum without directly putting in the numbers given. Any help would be appreciated, because right now I am stuck.


Here is the program description

Find the sum and average for a set of uncounted set of test scores. The last number in the data will be zero and should not be processed. Print the scores in a column as they are read in. Print the sum, number of scores and average using appropriate labels.

This program requires reading an external file. First you must download the file to the same subdirectory where you are going to save your file. You can also see a sample program on how to read and external file under the button “How do you do this?”.

and..


Here is the code I have so far


import java.util.Scanner;
import java.io.*;
public class SumAvg
{
    public static void main(String args[])
    {
    Scanner inFile = null;
    try
    {
    inFile = new Scanner (new File ("Data Tester.txt"));
    }
    catch (FileNotFoundException e)
    {
        System.out.println("File not found!");
        System.exit(0);
    }
    
   String text[] = new String[100];
    
   System.out.println("Scores"); 
   int numb = inFile.nextInt();
   while (numb !=0 )
   {
    System.out.println(numb); 
    numb = inFile.nextInt();
   }
    System.out.println();
    inFile.close();
    
}

   
   }

This post has been edited by macosxnerd101: 10 October 2011 - 07:25 PM
Reason for edit:: Please use code tags

Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10563
  • View blog
  • Posts: 39,087
  • Joined: 27-December 08

Re: Finding the sum of the file input

Posted 10 October 2011 - 07:26 PM

You'll need a sum variable to add numb to on each iteration of the loop, as well as a counter variable to track the number of iterations of the loop.
Was This Post Helpful? 0
  • +
  • -

#7 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7744
  • View blog
  • Posts: 13,083
  • Joined: 19-March 11

Re: Finding the sum of the file input

Posted 10 October 2011 - 07:35 PM

Okay, I see that you've got the file open, that looks good.

Now, what do you want to do next? I see that you declare an array, but I'm not sure what it's for. The loop looks more promising. You've got the loop condition correct, so you're doing quite well so far.
Now what happens in the loop?

Imagine that you're trying to solve this problem on paper: you have a sheet of scratch paper and a list of numbers. You take each number one at a time, and if it's zero, you stop. What do you do if it's not zero?
Was This Post Helpful? 0
  • +
  • -

#8 Lemonade4sale  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 10-October 11

Re: Finding the sum of the file input

Posted 10 October 2011 - 07:42 PM

View Postjon.kiparsky, on 10 October 2011 - 07:35 PM, said:

Okay, I see that you've got the file open, that looks good.

Now, what do you want to do next? I see that you declare an array, but I'm not sure what it's for. The loop looks more promising. You've got the loop condition correct, so you're doing quite well so far.
Now what happens in the loop?

Imagine that you're trying to solve this problem on paper: you have a sheet of scratch paper and a list of numbers. You take each number one at a time, and if it's zero, you stop. What do you do if it's not zero?


If the numbers are not zero you continue to loop.
Was This Post Helpful? 0
  • +
  • -

#9 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7744
  • View blog
  • Posts: 13,083
  • Joined: 19-March 11

Re: Finding the sum of the file input

Posted 10 October 2011 - 07:43 PM

True. And what do you do in the loop? You can loop all day without getting anywhere, what you want to do is to handle each of the items once and end up with the sum of the numbers.
Was This Post Helpful? 0
  • +
  • -

#10 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Finding the sum of the file input

Posted 10 October 2011 - 07:45 PM

your code

int numb = inFile.nextInt();
   while (numb !=0 )
   {
    System.out.println(numb); 
    numb = inFile.nextInt();
   }


assumes that the file ends with a 0 as the last integer in the file
A better approach would be

   while (inFile.hasNextInt())
   {
      int numb = inFile.nextInt();
      System.out.println(numb); 
   }


And you can cumulate the numb into a sum inside the while()loop as macosxnerd101 suggested
Was This Post Helpful? 0
  • +
  • -

#11 Lemonade4sale  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 10-October 11

Re: Finding the sum of the file input

Posted 10 October 2011 - 07:50 PM

View Postpbl, on 10 October 2011 - 07:45 PM, said:

your code

int numb = inFile.nextInt();
   while (numb !=0 )
   {
    System.out.println(numb); 
    numb = inFile.nextInt();
   }


assumes that the file ends with a 0 as the last integer in the file
A better approach would be

   while (inFile.hasNextInt())
   {
      int numb = inFile.nextInt();
      System.out.println(numb); 
   }


And you can cumulate the numb into a sum inside the while()loop as macosxnerd101 suggested


The file does end with a 0 as the last integer.
Was This Post Helpful? 0
  • +
  • -

#12 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Finding the sum of the file input

Posted 10 October 2011 - 07:53 PM

So your while() will work
Was This Post Helpful? 0
  • +
  • -

#13 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7744
  • View blog
  • Posts: 13,083
  • Joined: 19-March 11

Re: Finding the sum of the file input

Posted 10 October 2011 - 07:54 PM

@pbl - that's part of the program spec. He's got that part right.

Quote

The last number in the data will be zero and should not be processed.


If he used your method, he'd then have to remove the zero from his tally, or his numbers would be off...

(ninja'd...)

This post has been edited by jon.kiparsky: 10 October 2011 - 07:54 PM

Was This Post Helpful? 0
  • +
  • -

#14 Lemonade4sale  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 10-October 11

Re: Finding the sum of the file input

Posted 10 October 2011 - 07:56 PM

View Postjon.kiparsky, on 10 October 2011 - 07:43 PM, said:

True. And what do you do in the loop? You can loop all day without getting anywhere, what you want to do is to handle each of the items once and end up with the sum of the numbers.


I am sorry I just do not understand what you are saying, or what I am suppose to do.
Was This Post Helpful? 0
  • +
  • -

#15 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Finding the sum of the file input

Posted 10 October 2011 - 08:00 PM

Re-read macosxnerd101 post #6
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2