1

I’m trying to create a “secret” number finder without using a loop that counts from 1 to 10. Now what I am trying is to random out 10 numbers that if they are higher than the secret number in this case number 5 the loop will only look for number between 5 – 10 or if its lower 0-5.

Sometimes the function gives me an output that can look like this Higher: 0 Lower : 8 And then the error

Exception in thread "main" java.lang.IllegalArgumentException: n must be positive
    at java.util.Random.nextInt(Unknown Source)
    at test1.testvoid$TestVoidMethod.main(testvoid.java:31)

Otherwise I can get the output

Higher : 1
The secret number is: 3

Without an error.

This is as far I get. I know this is a wot, but its hard to point out where the problem could be.

import java.util.Random;
    public class testvoid {
        public static class TestVoidMethod {
            static int secretNumber = 3;
            public static void main(String[] args) {

                Random randomGenerator = new Random();
                String result = "";

                int[] myIntArray = new int[10];
                int i = 0;
                int lastGuess = 0;

                result = checkWhichNumberThisIs(0);

                 while (!result.equals("Equal")) 
                  {
                    if(result.equals("Higher"))
                    { 
        int randomInt = randomGenerator.nextInt(10 - (lastGuess - 1)) + lastGuess;
                        lastGuess = randomInt;
                        result = checkWhichNumberThisIs(randomInt);
                    }
                        else if(result.equals("Lower"))
                    {
                        int randomInt = randomGenerator.nextInt(lastGuess);

                        lastGuess = randomInt;
                            if(myIntArray.equals(randomInt)){
                        result = checkWhichNumberThisIs(randomInt);
                        }
                    }

                    myIntArray[i] = lastGuess;
                    i++;
                }
            }

            public static String checkWhichNumberThisIs(int numberGuess) {

                String result = "";

                if (secretNumber < numberGuess) {
                    result = "Lower";

                     System.out.println("Lower " + numberGuess);
                  }
                  else if (secretNumber > numberGuess) {
                    result = "Higher";
                     System.out.println("Higher " + numberGuess);

                  }
                  else if (secretNumber == numberGuess) {
                    result = "Equal";
                     System.out.println("The secret number is: " + numberGuess);
                  }

                return result;

               }
            }

    }
Dymond
  • 2,158
  • 7
  • 45
  • 80
  • 12
    Don't compare String values using `==`. Use `equals()`. – Alexis C. Jan 26 '14 at 21:51
  • 1
    String comparison people: while you are all right in your suggestions, this has nothing to do with the problem. Since the OP is only using literals `==` comparison will work here. – Radiodef Jan 26 '14 at 22:02

2 Answers2

4

As the others have said your string comparison is faulty, but your exception is coming from a different fault.

The problem is that you're calling nextInt with a 0 or negative argument.

From the Random class documentation:

Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive), drawn from this random number generator's sequence.

Given your wrong string comparison, and since lastGuess is initialized with 0,
in the second loop 10 - (lastGuess - 1) could be 0, if the first random operation resulted in a 10.

If lastGuess is 0 infact [10 - (0 - 1)] = 10 - (-1) = 11. That's why your algorithm is failing.

exexzian
  • 7,782
  • 6
  • 41
  • 52
elbuild
  • 4,869
  • 4
  • 24
  • 31
  • Hey. thank you. I'll get what the problem is, but need to read it slow since im not all familiar with this :) – Dymond Jan 26 '14 at 22:15
  • 1
    There's another problem having to do with the line `if(myIntArray.length == randomInt)` which will rarely be true and (after the edit) `if(myIntArray.equals(randomInt)){` which will never be true such that `result` never changes once "Lower" is hit. But in any case there are several places where `nextInt(0)` can be called. – Radiodef Jan 26 '14 at 22:21
  • @Radiodef I updated the code earlier and edited to if(myIntArray.equals(randomInt)){ – Dymond Jan 26 '14 at 22:24
  • @Dymond Yes I saw. In both cases I am not sure what the line is intended for and it's erroneous. In the case of the edit, an array will never equal an int. In the case of the pre-edit version, I'm pretty sure it will also never be true but for a more complicated reason. – Radiodef Jan 26 '14 at 22:26
2

change result != "Equal" to !result.equals("Equal") and result == "Higher" to result.equals("Higher")

when you are using == on non primitives it just compares the adresses in the memory and not the object. in strings in java, sometimes it can be "smart" and put 2 same strings in the same address but thats not always, so sometimes "hello"=="hello" will return true and sometimes false

Dima
  • 8,586
  • 4
  • 28
  • 57
  • 1
    but exception is not 'coz of this. – exexzian Jan 26 '14 at 22:02
  • Hey. Thank you. I just changed all the == to .equals. But the problem with the exception is still there. Dont think it could be because of that. – Dymond Jan 26 '14 at 22:03
  • 1
    @Dymond debug your code or run it step by step with few random number test cases - am pretty sure that there are some numbers that passes `negative` number to `nextInt()` – exexzian Jan 26 '14 at 22:05
  • 1
    @Dymond, if the first iteration random call returns 10, the second loop will call nextInt() with 0 as arg. Have a look to my answer. – elbuild Jan 26 '14 at 22:06
  • This explanation is wrong plain and simple. `"hello" == "hello"` will **always** return true because both are literals and that's part of the JLS. `"hello" == new String("hello")` will return false. – Radiodef Jan 26 '14 at 22:24
  • i meant "hello"==SomeVarThatValueIsHello :) – Dima Jan 26 '14 at 22:35
  • Doesn't compare addresses, but to a C programmer this would convey the concept. In java, `==` compares object identity. – Bohemian Jan 26 '14 at 23:22
  • == compares object references, thats like address in javanese – Dima Jan 26 '14 at 23:28