• Post Reply Bookmark Topic Watch Topic
  • New Topic

My program doesn't function properly  RSS feed

 
Clark James
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My program is suppose to accept up to 5 names and grades and it does that but when I try to sort them by Name or Grade and then try print them out I am only getting the last name and grade entry made. I'm pretty sure the problem is with my counter but I can't seem to figure it out. Any help would be appreciated it.
 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your code doesn't compile. Please paste code which can be compiled and also use codetagsfor the same.
 
Stefan Evans
Bartender
Posts: 1837
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi! Welcome to the ranch.

Yeah, as Tushar has pointed out, it helps if you UseCodeTags. <--- click to find out how.
I've put them in for you this time - see how much better it looks?


There are a couple of minor compile errors in there
- missing import for scanner class
- missing an opening { for your class.

Once those are sorted your program should compile and run .

You do have a problem in the code that inputs the names.
Your print method looks ok though. I would suggest calling it after you have finished entering names just to show you what the user entered before sorting.
Or maybe even printing them names after you have entered EACH name .
That should help you get a handle on what is going on in your program.

 
Clark James
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See if it compiles now. I'm using NetBeans and it does compile there but I left out the top couple lines previously.

 
Clark James
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stefan Evans wrote:Hi! Welcome to the ranch.

Yeah, as Tushar has pointed out, it helps if you UseCodeTags. <--- click to find out how.
I've put them in for you this time - see how much better it looks?


There are a couple of minor compile errors in there
- missing import for scanner class
- missing an opening { for your class.

Once those are sorted your program should compile and run .

You do have a problem in the code that inputs the names.
Your print method looks ok though. I would suggest calling it after you have finished entering names just to show you what the user entered before sorting.
Or maybe even printing them names after you have entered EACH name .
That should help you get a handle on what is going on in your program.


Stefan,

After reading your comments I started looking at my Name and Grade input and finally got it working. I simply added i++; after the Grade input line and it finally accepted and printed all five names and grades out of the parallel arrays.
Thanks for your help. I'm only a couple months into Java and this had me stumped for a few days.

 
Campbell Ritchie
Marshal
Posts: 56522
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch again

I can see lots of ways you can improve that code. One problem is that method is much too large. you should divide that up; you can probably make ten methods from it. Don't start variable names with CapitalLetters; start them with smallLetters. Don't write comments which make the lines so long and don't tell us anything we can't guess from the code anyway.

Where did you add i++; (line number)? Can you explain why that made the program work? If you can do that, you will demonstrate you learnt something useful.
 
Carey Brown
Saloon Keeper
Posts: 3309
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have a for() loop on line 23 without any braces. The result is the for() loop immediately incloses the following while() loop.
 
Liutauras Vilda
Sheriff
Posts: 4914
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure what is more bad: to put such a lot code in one method, or create such an amount of comments - which just makes code less readable.

But even that, if you decide to write so many comments, make sure they are very accurate.
For example:
Clark James wrote:

This comment is overloaded of useless information, wouldn't be clearer like:


[edit]I just realized now, probably you were thinking about array indexes, in this context, maybe that comment is not so bad[edit]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!