• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

Trouble printing numbers from a file in a loop from each line of a file.

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I am having trouble listing out the integers in my file to the println statement between "integer" and "is". For example: The average of the 3 integers 1 2 3 is 2.0.
My program only prints this so far: The average of the 3 integers "3" is 2.0. How would I print the 1 and 2 in front of "3"?


/*output:

The average of the 3 integers 3 is 2.0
The average of the 5 integers 0 is 7.2
The average of the 10 integers 10 is 5.5
The average of the 1 integers 17 is 17.0
The average of the 2 integers 80 is 85.0
5 sets of numbers processed

*/
 
/* numbers.txt:

3 1 2 3
5 12 14 6 4 0
10 1 2 3 4 5 6 7 8 9 10
1 17
2 90 80


}
 
Marshal
Posts: 65365
248
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Consider how you would count the number of items read. What you are doing is calling nextInt() in line 15, and then calling it again in line 17. That isn't going to add up, and can cause you all sorts of nasty problems unless you are very lucky about how many numbers your file contains. Don't use two loops. If the first number represents how many numbers follow it, use the inside loop. If not, only use while (sc.hasNextInt()) ... and count the numbers as you go.
As for displaying al the different numbers, experienced people would put the numbers into a Collection (most probably a List) and print out the List, but you may not have come across such classes yet.
 
Kelvin Wu
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Welcome to the Ranch

Consider how you would count the number of items read. What you are doing is calling nextInt() in line 15, and then calling it again in line 17. That isn't going to add up, and can cause you all sorts of nasty problems unless you are very lucky about how many numbers your file contains. Don't use two loops. If the first number represents how many numbers follow it, use the inside loop. If not, only use while (sc.hasNextInt()) ... and count the numbers as you go.
As for displaying al the different numbers, experienced people would put the numbers into a Collection (most probably a List) and print out the List, but you may not have come across such classes yet.



Thank you for your comments! I just want to clarify that count and number are tasked to do two separate things. Count is used to read in the first number of each line and it is used to tell how many numbers are in that line. number is to read in all the numbers after the first and add all of them together to total. Two loops are used so I could reset the total and bring in the next count.
 
Sheriff
Posts: 13675
226
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. I wouldn't use a collection if there is no need to. I don't think there's a need to.

2. You don't need to use just one display statement. You can have logic between display statements.

Read count
Display how many you're going to count
Read one number and do processing
Display the number you just processed

You just need to figure out if you use println() or just print()
 
Campbell Ritchie
Marshal
Posts: 65365
248
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Kelvin Wu wrote:. . . . Count is used to read in the first number of each line and it is used to tell how many numbers are in that line. number is to read in all the numbers . . . .

I am not convinced that those two loops are the best way to read your file. What you have described isn't reading from the whole file as I thought last night, but from each line. Your file still doesn't need a count, because it is very easy to count the ints in each line. That sounds to me like a brittle type of file reading. All you need is a line like this:-or this:-...and your counting will fail. (It would be easier if the last number in 97 were negative).
Suggestion:- Start by calculating the averages for one line only. Use a second Scanner for each line.Count the tokens in each line and verify that the count in the first token is correct. Remember that a line starting 10 should contain 11 tokens. Once you have that working for one line, then you can wrap the code I suggested in a while (myScanner.hasNext()) ... loop. Don't use while (myScanner.hasNextLine()) ... because that will fail if your file ends with an empty line. If you use nextLine(), don't call nextAnythingElse() on the same Scanner object.

Junilu is right that you can print the individual numbers as you go.
 
girl power ... turns out to be about a hundred watts. But they seriuosly don't like being connected to the grid. Tiny ad:
professionally read, modify and write PDF files from Java
https://products.aspose.com/pdf/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!