• Post Reply Bookmark Topic Watch Topic
  • New Topic

need help with scanner function being called for every item in array  RSS feed

 
Rob Miller
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I'm new to Java and am trying to pass a variable from the scanner to my Constants class so that depending on the number entered it'll sort the array differently.

I've got it where the list will sort; problem is my scanner repeats where you have to enter your selection in multiple times before the list shows up.

I know the problem has to do with the call "int c = assign_6.choice()". If I hard code in a number it's fine but it appears to be making multiple calls to the choice() function for every item in my array.

I've tried moving the function out of the main and removing the Comparable in my Constants file and also removing the quick sort and using Array and Collections. None of which worked.
I feel like it's probably a stupid mistake I'm making and missing it due to not knowing Java that well. Could use help in figuring this out.

Here's my output:


Java code:
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your problem is that choice() on line 29 is part of the default initialization which happens every time you construct a new object. I suggest having a setChoice() method that you call once from main(). In which case "int c" would need to be declared static.
 
Rob Miller
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:Your problem is that choice() on line 29 is part of the default initialization which happens every time you construct a new object. I suggest having a setChoice() method that you call once from main(). In which case "int c" would need to be declared static.

Ok. So my variable is global. Now to figure out how to have it seen in Constants(currently it doesn't see it).

 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, all class names should start with an upper case letter.
 
Rob Miller
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:


Getting an error on the "Contacts.setChoice(Choice.choice());" in my main.
Are you setting up c with a getter to make it global? I'm trying to understand your method here for future reference and to understand the error I'm getting.
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Miller wrote:
Carey Brown wrote:


Getting an error on the "Contacts.setChoice(Choice.choice());" in my main.
Are you setting up c with a getter to make it global? I'm trying to understand your method here for future reference and to understand the error I'm getting.

First off, always add the error messages to your posts.

Did you notice I changed class name 'choice' to 'Choice'? Don't know if that's what you're running into. If not, repost your complete source code again with a copy of the error message.
 
Rob Miller
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:
Rob Miller wrote:
Carey Brown wrote:


Getting an error on the "Contacts.setChoice(Choice.choice());" in my main.
Are you setting up c with a getter to make it global? I'm trying to understand your method here for future reference and to understand the error I'm getting.

First off, always add the error messages to your posts.

Did you notice I changed class name 'choice' to 'Choice'? Don't know if that's what you're running into. If not, repost your complete source code again with a copy of the error message.


Yes i did.

The error is it wants to keep changing setChoice to a static which in turn gives an error on the this. statement that it can't have that with a static.

Again if you could share how this is working it would help.
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Again, repost your complete code.
 
Rob Miller
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:Again, repost your complete code.

Again explain your thought process
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No matter how many Contacts you have, you only need one choice, therefore the choice should be static.
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, no 'this' in a static method.

 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!