# Finding the sum of the file input

• (2 Pages)
• 1
• 2

## 22 Replies - 3918 Views - Last Post: 10 October 2011 - 08:43 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=250734&amp;s=bd621388114f8f0257233176d38d4f61&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

Reputation: 0
• 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

• Beginner

Reputation: 12023
• Posts: 20,377
• 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

### #3 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12772
• Posts: 45,965
• 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.

### #4 jon.kiparsky

• Beginner

Reputation: 12023
• Posts: 20,377
• 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.

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

## Re: Finding the sum of the file input

Posted 10 October 2011 - 07:24 PM

Lemonade4sale, 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.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

### #6 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12772
• Posts: 45,965
• 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.

### #7 jon.kiparsky

• Beginner

Reputation: 12023
• Posts: 20,377
• 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?

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

## Re: Finding the sum of the file input

Posted 10 October 2011 - 07:42 PM

jon.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.

### #9 jon.kiparsky

• Beginner

Reputation: 12023
• Posts: 20,377
• 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.

### #10 pbl

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

Reputation: 8381
• Posts: 31,956
• Joined: 06-March 08

## Re: Finding the sum of the file input

Posted 10 October 2011 - 07:45 PM

```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

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

## Re: Finding the sum of the file input

Posted 10 October 2011 - 07:50 PM

pbl, on 10 October 2011 - 07:45 PM, said:

```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.

### #12 pbl

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

Reputation: 8381
• Posts: 31,956
• Joined: 06-March 08

## Re: Finding the sum of the file input

Posted 10 October 2011 - 07:53 PM

### #13 jon.kiparsky

• Beginner

Reputation: 12023
• Posts: 20,377
• 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

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

## Re: Finding the sum of the file input

Posted 10 October 2011 - 07:56 PM

jon.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.

### #15 pbl

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

Reputation: 8381
• Posts: 31,956
• Joined: 06-March 08

## Re: Finding the sum of the file input

Posted 10 October 2011 - 08:00 PM