• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Program not executing properly

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey everybody,

New to the java scene and having major issues, it would be appreciated if someone could have a look at my coding and see where i'm going wrong. I have a few

1. When i press the 'start new game' button the random number resets but not the program doesn't, as it just comes up with an exception error after i enter an input.
2. The program wont work out the median of a sorted array, i think because if the other values are zero that affects it .. what do i need to do to fix it ??
3. I've tried to work out the postion of the random number once the array has been sorted .. i think maybe its the same as the median issue !!
4. I need to work out how the user can only put values in ranging from 7 - 100, if outside those values than an error message and re-entering of an input must occur ... no idea !!
5. I need to work out how to exit a program by entering ' -1 ' .. no idea



 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Joel Mackay wrote:1. When i press the 'start new game' button the random number resets but not the program doesn't, as it just comes up with an exception error after i enter an input.


Almost certainly because you're not resetting your arrays.

2. The program wont work out the median of a sorted array, i think because if the other values are zero that affects it .. what do i need to do to fix it ??


Again, almost certainly because you're including those 0's in your calculation. You keep track of the number of guesses, don't you?


3. I've tried to work out the postion of the random number once the array has been sorted .. i think maybe its the same as the median issue !!


Probably. My suggestion would be to copy it to an array of the correct length before sorting.

4. I need to work out how the user can only put values in ranging from 7 - 100, if outside those values than an error message and re-entering of an input must occur ... no idea !!


if (!(numberEntered >= 7 && numberEntered <= 99))
    throw new IllegalArgumentException("invalid guess");

or just force them to guess again (eg, by putting it in a while loop).

5. I need to work out how to exit a program by entering ' -1 ' .. no idea


With something very similar to 4. The usual way to exit a program normally is to call System.exit(0).

Just a couple of other points:
  • if ((guesses % 1) == 0)... will never execute the else block.
  • You'll find Random.nextInt(limit) is a lot easier to use, and you don't have to worry about casting the result.
  • You'd be much better off separating your game logic from your display logic entirely. Right now, your code is kind of a mish-mash of the two, so if you run into problems, it's difficult to know which aspect is causing them. Another advantage of doing things that way is that you can decide later on how you want to display it (console, GUI, dialog boxes, or even (eventually) via a browser).

  • HIH

    Winston
     
    Joel Mackay
    Greenhorn
    Posts: 3
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hello again everyone,

    I've tried to sort my coding out like Winston suggested .. hopefully its ok

    1. I added an array reset to the generateNumber() method hopeing that this would solve the issue but it continues to throw an error '"AWT-EventQueue-0" java.lang.NumberFormatException: For input string: " 57"
    after pressing the 'Start new game' button.

    2. I added the ranging loop for 7 - 100, but the inputed value if below 7 or above 100 gets added to the array ... how can i stop this from happening.

    3. I still dont have a clue on removing the unwanted zeros from the array to work out the median an position of the random number



     
    Joel Mackay
    Greenhorn
    Posts: 3
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    bump ... anyone could they help please !!
     
    Winston Gutkowski
    Bartender
    Posts: 10780
    71
    Hibernate Eclipse IDE Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Joel Mackay wrote:I've tried to sort my coding out like Winston suggested .. hopefully its ok

    1. I added an array reset to the generateNumber() method hopeing that this would solve the issue but it continues to throw an error '"AWT-EventQueue-0" java.lang.NumberFormatException: For input string: " 57"
    after pressing the 'Start new game' button.


    You do realize that " 57" is not a number? You might try trim()ming the String before you convert.

    2. I added the ranging loop for 7 - 100, but the inputed value if below 7 or above 100 gets added to the array ... how can i stop this from happening.


    You added the check, but as far as I can see you don't force the user to keep inputting until they get it right. The normal pattern for something like this would be:I also notice that you add to your guess count before you've made sure that it's OK.

    3. I still dont have a clue on removing the unwanted zeros from the array to work out the median an position of the random number


    I believe I already suggested one way: copy your array of guesses to a new array of the correct size before you sort/search. Have a look at Arrays.copyOf().

    Winston
     
    There are no more "hours", it's centi-days. They say it's better, but this tiny ad says it's stupid:
    a bit of art, as a gift, that will fit in a stocking
    https://gardener-gift.com
    reply
      Bookmark Topic Watch Topic
    • New Topic