This week's book giveaway is in the Agile forum.
We're giving away four copies of Head First Agile and have Andrew Stellman & Jennifer Greene on-line!
See this thread for details.
Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem with output  RSS feed

 
Veronica Green
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi! I am currently writing a program using a multi dimensional array. It should output the name and 4 marks of a student (ex. John Smith 77 80 82 91). I have made a nested for loop for the output but when I run my program only the last mark entered is being outputted. Anyone know what's wrong?



My array is and the outputInfo is the name of my JTextArea if that helps.
 
Stefan Evans
Bartender
Posts: 1836
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well calling setText will do exactly that. Remove whatever is in the text component already, and put the new value there.

Which would explain why you only see that last printed value.

I'm pretty sure there is another method in javax.swing.JTextArea that you can use instead of setText

 
Jeanne Boyarsky
author & internet detective
Sheriff
Posts: 37261
519
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This only runs one time. Do you see why? Hint: when does it start and end?
 
Greg Charles
Sheriff
Posts: 3015
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Veronica, welcome to Java Ranch!

The main problem is that your rows loop on line 3 has a stop condition of rows <= 0. Since rows is initialized to 0, that stop condition is immediately true, and so the loop body is never run.

Also, I believe the setText() method will replace any current text in the text area, so even once you get the loop logic right, you'll just see the last grade of the last student.


ETA: whoa, doggies, double ninja'd!
 
Veronica Green
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks everyone! I changed it from setText to append and all 6 lines of text appear. I left rows at 0, because if I change it to any other number it will just output the lines of text more then once.
 
Stefan Evans
Bartender
Posts: 1836
10
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Really?

Then I would double check that you actually HAVE a two dimensional array.
Maybe the code creating it isn't quite right either?

What are the values for
grades.length?
grades[0].length?
 
Veronica Green
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stefan Evans wrote:Really?

Then I would double check that you actually HAVE a two dimensional array.
Maybe the code creating it isn't quite right either?

What are the values for
grades.length?
grades[0].length?


The code for the array is

etc. to [14][5]. The value for grades.length is 15 and grades[0].length is 6
 
Jason Bullers
Ranch Hand
Posts: 115
11
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Veronica Green wrote:
etc. to [14][5].


Ah, it seems that you are filling each of the 15 elements of the array with the exact same set of inputs (first, last, test*). That would explain why you see the same output over and over again with you increase the number of rows. Is the intent to allow the user to input 15 different names along with marks for each? You might want to take a look at using a loop to collect the input.

Also, a few general points.

You should declare your loop control variables in the loop construct, unless you have a really good reason not to. Generally, you want to restrict the visibility of variables to the smallest scope possible. Most likely, you'd want to do something like this here:

It's also a really good idea to avoid "magic numbers" in your program. In your loop, the condition should test against a constant or query the array for the length instead of a hardcoded number like 5:


Out of curiosity, do you have to use arrays for this program?
 
Veronica Green
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Yes, I have to use a multi dimensional array for the program and the user is supposed to enter 15 names along with the corresponding marks. The only problem I have with this code is that it repeats the user input 15 times. Would that be because of a problem when collecting the input? How would I go about creating a loop for user input?
 
Campbell Ritchie
Marshal
Posts: 55793
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome again

The name multi‑dimensional array sounds nice, but it is wrong. There is no such thing in Java┬«. What you have there is an array of arrays of arrays. There is a better variable to use in the middle of the for loop namely an attribute of the array itself. There is a standard form for a for loop:
for (int i = 0; i < myArray.length; i++) ...
and you should always use that as a starting point when you write a for loop. You often have to change it, but that is a starting point. If you ever replace an array with one of a different size, that .length bit will sort all size problems out.

Of course, you are iterating your array in a read‑only fashion, so you shouldn't use an ordinary for loop in the first place. Find out about the for each loop (officially called enhanced for). Also find out about the StringJoiner class. You can use a StringJoiner object to get a nice output like *{123, 456, 789}* or whatever. Beware: it is only available in Java8.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!