Win a copy of TensorFlow 2.0 in Action this week in the Artificial Intelligence and Machine Learning forum!
  • 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
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Trouble reading integer text file into 2d array

 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've posted the section of code below for someone to review, I'm trying to read a text file of integers in rows and columns to display in a grid format but I'm getting the wrong output. What am I doing incorrectly? The output should look like the grid in the screenshot that I've attached.

2DARRAYGRID.JPG
[Thumbnail for 2DARRAYGRID.JPG]
 
Marshal
Posts: 25961
70
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is your problem that the numbers you output don't match the numbers in the example you posted? Or is your problem that you display the numbers in one column instead of a grid like what you posted?

(Both of those things are happening in your code, by the way.)
 
Andrew Bauer
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Clapham wrote:Is your problem that the numbers you output don't match the numbers in the example you posted? Or is your problem that you display the numbers in one column instead of a grid like what you posted?

(Both of those things are happening in your code, by the way.)




Yes the output is incorrect because it's currently printing strings of letters and numbers.
 
Paul Clapham
Marshal
Posts: 25961
70
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay. But the code I see can only print numbers. So... are you running that code, or something else?
 
Marshal
Posts: 70684
288
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In which case you are probably printing your output incorrectly. Remember that arrays don't override toString(). Thre are various methods in the Arrays class to make up for deficiencies in the default implementation of arrays.
 
Andrew Bauer
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Clapham wrote:Okay. But the code I see can only print numbers. So... are you running that code, or something else?




The output is coming from the code that was posted.
 
Paul Clapham
Marshal
Posts: 25961
70
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So what do you think your next step should be?

(Hint: I don't believe you when you tell me your output contains characters other than numbers -- at least, that's how I interpreted what you said.)
 
Andrew Bauer
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Clapham wrote:So what do you think your next step should be?

(Hint: I don't believe you when you tell me your output contains characters other than numbers -- at least, that's how I interpreted what you said.)




Well I've made some edits and now the output is generating its own data instead of printing the data from the file.

GeneratedData.JPG
Code generated data instead of text file data.
Code generated data instead of text file data.
 
Paul Clapham
Marshal
Posts: 25961
70
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Much better, then!
 
Andrew Bauer
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Clapham wrote:Much better, then!




I got it working now.
 
Paul Clapham
Marshal
Posts: 25961
70
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great! Do you want to post your working code for comments or are you good to go?
 
Andrew Bauer
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Clapham wrote:Great! Do you want to post your working code for comments or are you good to go?




Yup this is the working code. Now to write a method to average each column and row...


 
Andrew Bauer
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



Am I taking the right approach to averaging a column of data in my lab average method or would I need to use a nested loop to do that?
 
Paul Clapham
Marshal
Posts: 25961
70
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think it's right, no. However... you started out with "row" and "column" as the names of the array subscripts, and then they changed to "student" and "lab", at least it looks like that.

So "student" is represented by a row, i.e. the first subscript of the array. That part looks right, and from your post it sounds like it works right too. But then "lab" is represented by a column, i.e. the second subscript of the array.

That part looks wrong. It should look very much like the "student" calculation, only using columns instead of rows. Right now it looks too much like the "student" calculation.

Perhaps you were led astray because you can find the length of a row of an array in Java, but you can't find the length of a column. But you don't have to find either of those two things in this code because you already found out what they were near the beginning.
 
Andrew Bauer
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Clapham wrote:I don't think it's right, no. However... you started out with "row" and "column" as the names of the array subscripts, and then they changed to "student" and "lab", at least it looks like that.

So "student" is represented by a row, i.e. the first subscript of the array. That part looks right, and from your post it sounds like it works right too. But then "lab" is represented by a column, i.e. the second subscript of the array.

That part looks wrong. It should look very much like the "student" calculation, only using columns instead of rows. Right now it looks too much like the "student" calculation.

Perhaps you were led astray because you can find the length of a row of an array in Java, but you can't find the length of a column. But you don't have to find either of those two things in this code because you already found out what they were near the beginning.




What do you suggest that I do then?
 
Andrew Bauer
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Actually nevermind. How would I send the calculated average for each row and column to a text file now?


 
What are you doing in my house? Get 'em tiny ad!
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic