• Post Reply Bookmark Topic Watch Topic
  • New Topic

Compare an int with an array  RSS feed

 
manu lix
Ranch Hand
Posts: 74
Java MySQL Database Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!
I am new programmer Java, I decided to enroll in this forum because in a couple of months I will have to make the Java certification exam.
I'm Italian, I do not speak English well

I need to compare an int with an array of values generated with a for loop previously.
I have something like that for the search..


Also, I need to print the array each time I insert a value that is in the array. But, hiding the values still not "guessed".
I hope I was clear and I thank you ahead of time if someone is willing to help me!
 
Stevens Miller
Bartender
Posts: 1445
30
C++ Java Netbeans IDE Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First, I will reformat your code, so it is easier to read:



At Line 16, you need some code that records the fact that the user has guessed an array entry (and it could be several entries, if more than one equals the user's guess). One way to do that would be to replace the array of integers wth an array of objects that keep track of whether they have been guessed or not. Here is an example of that kind of object:



If you build an array of these elements, your program could look more like this:



Does any of that help you move closer to your goal?

Userò Google translate in questa parte per cercare di rendere più facile da capire, perché io non parlo italiano.

In primo luogo, voglio riformattare il codice, quindi è più facile da leggere:



Alla linea 16, è necessario un codice che registra il fatto che l'utente ha intuito una voce array (e potrebbe essere più voci, se più di uno uguale indovinare dell'utente). Un modo per farlo sarebbe quello di sostituire l'array di interi wth un array di oggetti che tengono traccia dei se sono stati indovinato o meno. Questo è un esempio di questo tipo di oggetto:



Se si crea un array di questi elementi, il programma potrebbe apparire più simile a questo:



Se uno qualsiasi di che aiuto si sposta più vicino al tuo obiettivo?
 
manu lix
Ranch Hand
Posts: 74
Java MySQL Database Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, what you said was useful but not really what I was looking...
Maybe I explained bad what is my problem!

I have an array of int length [10], the user should guess only 5 of these 10 numbers.. to do this I had thought of creating a boolean method that receives as a parameter array and the number entered by the user.
And then check the number with the for each loop.
But I can not fix everything!
Also, I need to know how to print the array with the numbers "guessed" and at the same time hide the numbers do not yet discovered (using the asterisk *).

I would be grateful for support! Thanks
 
Tony Docherty
Bartender
Posts: 3271
82
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
manu lix wrote:I have an array of int length [10], the user should guess only 5 of these 10 numbers.. to do this I had thought of creating a boolean method that receives as a parameter array and the number entered by the user.


Ok, creating a method to do the check is a good start. But you have not done that, why?

manu lix wrote:
And then check the number with the for each loop.
But I can not fix everything!
Also, I need to know how to print the array with the numbers "guessed" and at the same time hide the numbers do not yet discovered (using the asterisk *).

I would be grateful for support! Thanks

I think you are trying to do too much in one go. First of all get a pen and paper and write down, in detail (as if you were explaining it to a 5 year old), how you would do this if someone gave you the input data and told you to produce the desired output. Then, once you have fully described the solution, and only then think about converting that to code.

Now that you have your design, start with implementing one single feature eg getting the user to guess numbers. Once you have that working properly implement the next feature eg check each guessed number is the array. Build up the program in small pieces and don't be tempted to move onto the next section until you have fully tested the section you are working on.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony Docherty wrote:I think you are trying to do too much in one go. First of all get a pen and paper and write down, in detail (as if you were explaining it to a 5 year old), how you would do this if someone gave you the input data and told you to produce the desired output. Then, once you have fully described the solution, and only then think about converting that to code.

@manu: I second Tony's post, and I will go further:

UNLESS you can explain to a 5-year old (well, maybe a 10-year old) how to do this IN ITALIAN, you will NEVER write a good Java program to do it.

Programming is NOT about coding, it's about thinking; and if you cannot describe the solution to someone else in your native language, you are NOT ready to code it.

Winston
 
manu lix
Ranch Hand
Posts: 74
Java MySQL Database Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Tony Docherty, Winston Gutkowski Thank you for the hints you have provided me.
In fact, I managed to get what I was trying.

I reasoned this way:
1. First of all, I needed something that would tell me if the number it was input in the array.
To do this, in a class I have created a method boolean:

Tested and working.

2. Then, I needed to ask the user to guess only 5 out of 10 numbers.
I solved this in the main:


3. The user has three possibilities of error, so I built the following code, although I think it is better to fix:


You, what do you think?

Now, I should find a way to solve the problem of printed out the array containing the numbers you have and not guessed.
Tips? :P
 
James Boswell
Bartender
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Manu


This method is fine but ask yourself this: once the number has been found, do you need to continue searching?
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
manu lix wrote:@Tony Docherty, Winston Gutkowski Thank you for the hints you have provided me.
In fact, I managed to get what I was trying.
I reasoned this way:
1. First of all, I needed something that would tell me if the number it was input in the array.

NOW you're getting somewhere. Because you're thinking, not just coding.

To do this, in a class I have created a method boolean:

I think James has given you a good hint. Another possibility might be to return the index of the matching element if you find one; or -1 if you don't.

But the main thing is that you've put it in a method, which is great. Indeed, ANY time you have "something to do", it's probably worth thinking about writing a method for it. For example, even something as simple as:Then your main() loop might look something like:In your case it's not wildly important, but it reduces the "visual clutter" and you now have a method that you could possibly use for other things.

The user has three possibilities of error, so I built the following code, although I think it is better to fix:

I agree. Again, think about how you might put this in a method.

But you've made a very good start. WELL DONE.

HIH

Winston
 
Tony Docherty
Bartender
Posts: 3271
82
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
manu lix wrote:
Now, I should find a way to solve the problem of printed out the array containing the numbers you have and not guessed.
Tips? :P

There are lots of ways you can implement this but essentially you need to keep a list of the numbers that have been correctly guessed and then when you iterate over your array of numbers to guess check each number to see if it has already been guessed and if not print a * instead of the number.
 
manu lix
Ranch Hand
Posts: 74
Java MySQL Database Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This method is fine but ask yourself this: once the number has been found, do you need to continue searching?


Hi. I tried to put a "break", so..


But in this way, it does not work as it should.
Because the program asks once after another to enter the number.
So must check every time, I think this is the problem that causes break.
Instead of removing the new break, everything works regularly.
What do you mean? I may have misinterpreted his advice.


There are lots of ways you can implement this but essentially you need to keep a list of the numbers that have been correctly guessed and then when you iterate over your array of numbers to guess check each number to see if it has already been guessed and if not print a * instead of the number.


I thought something like that too, but I can not write it :/
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
manu lix wrote:Hi. I tried to put a "break", so..

Rarely the right thing to do, I'm afraid.

A loop written with a break can almost always be written without one. How about:?
And that's just one way...

Winston
 
manu lix
Ranch Hand
Posts: 74
Java MySQL Database Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
manu lix wrote:Hi. I tried to put a "break", so..

Rarely the right thing to do, I'm afraid.

A loop written with a break can almost always be written without one. How about:?
And that's just one way...

Winston


I do not understand, what is the difference with my code?

I noticed a bug:
I enter the first number, the program generates 10 multiples of that numberand saves them in the array, ok ..
Once the program asks me to guess the first number and I insert the number corresponding to the first index of the array, tells me that is not present in the array.
What could be the problem?
While if I enter the number corresponding to the index in the second and then the first, everything ok .. o.O

Instead, with my code there is no problem ..
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
manu lix wrote:I do not understand, what is the difference with my code?

Logically? Nothing.

Both will work, but my version returns true as soon as a match is found. Yours will go through the entire array regardless.

I only posted it to illustrate that you DON'T need to add a break to the loop to make it work. In fact, I'd advise against it.

I suspect you may have been told that you should only have one return statement in a method; and while it's not bad advice in general, this is an exception to that rule:
When you find a match, you know what you need to do, so do it.

HIH

Winston
 
manu lix
Ranch Hand
Posts: 74
Java MySQL Database Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Winston , Ah ok! I did not understand because of my bad English.
Thank you.

Would know me an example of how you could get the printout of the array with matching numbers and * ?
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
manu lix wrote:Would know me an example of how you could get the printout of the array with matching numbers and * ?

Well, I'd prefer not to write it for you; but I will advise this: Put it in a method.

Why don't you try one yourself, and show us the results? That way, we can see if you have any problems.

Winston
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
manu lix wrote:Hi. I tried to put a "break", so..


But in this way, it does not work as it should.

The reason it doesn't work as you wanted is because the 'break' is not conditional on the if statement. That is, you only want the break to be executed when s == a, whereas in your code it will execute the first time through the for loop.

This is a very good example of why you should use curly braces and why you should always indent your code properly:
1. If you had initially used curly braces with the if statement you would almost certainly have added the break statement inside the braces and it would have worked.
2. If you had indented the code properly it would have been obvious (if break was aligned with check) that the break was either supposed to be in the if statement but clearly wasn't within curly braces or (if break was aligned with if) it was going to execute the first time through the loop.
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
manu lix wrote:
Would know me an example of how you could get the printout of the array with matching numbers and * ?

I've already given some advice on a strategy to use, why not try and implement it and if you get stuck post what you have done so far.
 
manu lix
Ranch Hand
Posts: 74
Java MySQL Database Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have to work on the same array. I can not store numbers correctly guessed in another array.
Calculated that the initial array of length 10, while the other array will only be of length 5, and this is not my objective.

I should write a method that if the number had been guessed, the method returns:
[2, *, *, *, *, *, *, *, *, *]
Then, subsequently (hypothesizing that it was guessed the number of new, in this case 8):
[2, *, *, 8, *, *, *, *, *, *]
And so on...(case 20):
[2, *, *, 8, *, *, *, *, *, 20]

This is because I need to detect the number to its actual position in the array.
 
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
How about:

 
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
manu lix wrote:I have to work on the same array. I can not store numbers correctly guessed in another array.

It's hard to help when you feed us the requirements in bits and pieces.

manu lix wrote:Calculated that the initial array of length 10, while the other array will only be of length 5, and this is not my objective.

I should write a method that if the number had been guessed, the method returns:
[2, *, *, *, *, *, *, *, *, *]
Then, subsequently (hypothesizing that it was guessed the number of new, in this case 8):
[2, *, *, 8, *, *, *, *, *, *]
And so on...(case 20):
[2, *, *, 8, *, *, *, *, *, 20]

This is because I need to detect the number to its actual position in the array.

So, if you can only have one array but you need to determine for each array position whether the number had been guessed or not, I might set guessed numbers to their negative equivalent, but that will require more work in your print loop to determine whether to print the positive equivalent of the value or an asterisk. (Presumably zero is not a valid value.)

Of course, an object oriented approach would be to have an array of objects (instead of integers) that have two members: int value, and boolean guessed.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
manu lix wrote:This is because I need to detect the number to its actual position in the array.

Which goes back to my original post. Why not have your numGuessed() method return the INDEX it was found at, rather than just a boolean?

Winston
 
manu lix
Ranch Hand
Posts: 74
Java MySQL Database Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I set all the elements in array with *.
And then find out only after any check if the number is guessed or not?

However, I have an array of integers ... How can I set * in the elements?
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
manu lix wrote:If I set all the elements in array with *.
And then find out only after any check if the number is guessed or not?

However, I have an array of integers ... How can I set * in the elements?

This is not an array, it's what you're displaying to your user - NOT the same thing.

Now you might want to create a different array that contains what you want to display (although I'm not convinced that you need to).
But if so, what about '-1' as your '*'?

Winston
 
manu lix
Ranch Hand
Posts: 74
Java MySQL Database Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But how do I do this?
Why -1? What does it mean?



 
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
manu lix wrote:But how do I do this?
Why -1? What does it mean?


 
manu lix
Ranch Hand
Posts: 74
Java MySQL Database Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Carey Brown, I tried the code you wrote, but it does not work.
I do not understand why compare the array with -1, as they are not present in the array -1!
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
manu lix wrote:@Carey Brown, I tried the code you wrote, but it does not work.
I do not understand why compare the array with -1, as they are not present in the array -1!

Because you're trying to display information to a user. You don't have to do it that way, it was just a suggestion; but what you DO have to do is convert the contents of your arrays into something visual that the user can understand.

So, if the number has been "guessed", show it; otherwise, display a "*" for that "element".

Winston
 
Carey Brown
Saloon Keeper
Posts: 3309
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You've been given a number of suggestions on ways you could implement this. Forgetting the int/* display for the moment, you need a way to keep track of the numbers that the person should attempt to guess, you also need a way to track which numbers were successfully guessed. Your main difficulty seems to be based in the fact that you're trying to handle both of these problems with a single int array. Not necessarily impossible, but awkward (see one of my earlier posts about negative numbers). You could track this with an array of objects instead of ints. You could track this with two arrays, one of numbers to guess and one of the guesses where -1 is used to indicate a number that has not been guessed. So, you have a few choices, PICK ONE and move on.
 
manu lix
Ranch Hand
Posts: 74
Java MySQL Database Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thought about how could I create an array of objects.
Unfortunately I was not able to implement it.
I should to study the array of objects.
 
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
First, design your object class. It will need an int value and a boolean 'found' members. Something like this:

So, in this case, an array would look something like:
 
manu lix
Ranch Hand
Posts: 74
Java MySQL Database Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Carey Brown, a int value it would be the number guessed past to this class?
And boolean found it would be the true or false of the method numGuessed?
 
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
If you have an array of Num's then that array must be initialized with all numbers to be guessed (why else would you need an array?). Each value would be compared against a single guess and if it is found then setFound(true).
 
manu lix
Ranch Hand
Posts: 74
Java MySQL Database Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok. Therefore the array of Num will be initialized with all matching numbers, that will be passed with a loop in the main, right?
I will have something similar:


The printing of the asterisk or number should I implement here, right? but how?
 
manu lix
Ranch Hand
Posts: 74
Java MySQL Database Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok. Therefore the array of Num will be initialized with all matching numbers, that will be passed with a loop in the main, right?
I will have something similar:


The printing of the asterisk or number should I implement here, right? but how?
 
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
Not an array of guesses but an array of Num's to be guessed, e.g. size of 10, not 5.
To print array of Num's
 
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

 
manu lix
Ranch Hand
Posts: 74
Java MySQL Database Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I get a compile error, in one part of the code not directly concerned with this:
into the array are inserted of integers.
Error: required Num, found int...

I'm coming out crazy
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
manu lix wrote:I get a compile error, in one part of the code not directly concerned with this:
into the array are inserted of integers.
Error: required Num, found int...

I'm coming out crazy


Not exactly much detail provided ... so, I am assuming that you are just venting?

Henry
 
manu lix
Ranch Hand
Posts: 74
Java MySQL Database Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
Not exactly much detail provided ... so, I am assuming that you are just venting?


I was nervous because I'm "wasting time" for a while of time.
However, by applying an array of objects (Num) at compile time mi appear errors.

this part of the code is used to generate multiples of the number entered by the user.



error: incompatible types
array[i] = multipli;
^
required: Num
found: int


Why?
 
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
numArray[i] is a reference to a Num object.

If you want to create a new Num:
numArray[i] = new Num( some_int );

If you want to compare the value:
if( numArray[i].getValue() == some_int )
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!