943,735 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 581
  • Java RSS
Dec 11th, 2008
0

Array dismay!

Expand Post »
I'm working on a simple matching game, whereby my array includes 3 numbers:{ 5,9,0 } and the user tries to guess them.

Instead, I want to use the Random class to generate 3 different numbers from 0-9, but I'm not sure how or where to start. I came across info on the Random class; however, there are no good examples using it with arrays. But, then I'm awful at arrays and need improvement in this area.

Can anyone help me out in this area? Thanks in advance.

Java Syntax (Toggle Plain Text)
  1. import java.io.*;
  2. import javax.swing.JOptionPane;
  3.  
  4. public class MatchGame
  5. {
  6. public static int[] guess = {5,9,0};
  7. public static void main(String[] args) throws IOException
  8.  
  9. {
  10. MatchGame();
  11. }
  12.  
  13. public static void MatchGame()
  14. {
  15. int o = 3;
  16. boolean arraysMatch = true;
  17. String input;
  18. int i = 0;
  19. int[] x = new int[o];
  20. for (i = 0; i < o; i++)
  21. {
  22. input = JOptionPane.showInputDialog("Input your 3 guesses: "+ (i + 1));
  23. x[i] = Integer.parseInt(input);
  24. }
  25. i = 0;
  26. if (guess.length != x.length)
  27. arraysMatch = false;
  28.  
  29. while (arraysMatch && i < guess.length)
  30. {
  31. if (guess[i] != x[i])
  32. arraysMatch = false;
  33. i++;
  34. }
  35. if (arraysMatch)
  36. JOptionPane.showMessageDialog(null, "Hooray! You matched all 3 numbers.");
  37. else
  38. JOptionPane.showMessageDialog(null, "Sorry, you didn't match!");
  39.  
  40. results();
  41. }
  42. public static void results()
  43. {
  44. JOptionPane.showMessageDialog(null, "The Correct Numbers: " + guess[0] +
  45. guess[1] + guess[2]);
  46. }
  47.  
  48. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
clueless101 is offline Offline
52 posts
since Mar 2008
Dec 11th, 2008
0

Re: Array dismay!

To use Random, you essentially:
(1) Construct a single Random object at the start.
(2) Each time you need a random number, call nextInt() on the Random object you created, specifying the upper bound on the random numbers you want. For example, to get a random number between 0 and 9 inclusive, call nextInt(10) (you add 1, because the upper bound you pass in to nextInt() is exclusive).
Always always pass in the upper bound to nextInt() -- sometimes you'll see people suggesting you call "nextInt() % 10", but this is INCORRECT.
I'm not sure what your mental block with arrays is, but in terms of using them with random numbers there's no other special thing to know. Put the first random integer in array position 0, the second in position 1 etc.
Reputation Points: 120
Solved Threads: 7
Junior Poster in Training
neilcoffey is offline Offline
53 posts
since Dec 2008
Dec 11th, 2008
0

Re: Array dismay!

Click to Expand / Collapse  Quote originally posted by neilcoffey ...
To use Random, you essentially:
(1) Construct a single Random object at the start.
(2) Each time you need a random number, call nextInt() on the Random object you created, specifying the upper bound on the random numbers you want. For example, to get a random number between 0 and 9 inclusive, call nextInt(10) (you add 1, because the upper bound you pass in to nextInt() is exclusive).
Always always pass in the upper bound to nextInt() -- sometimes you'll see people suggesting you call "nextInt() % 10", but this is INCORRECT.
I'm not sure what your mental block with arrays is, but in terms of using them with random numbers there's no other special thing to know. Put the first random integer in array position 0, the second in position 1 etc.
Quote ...
To construct a single random object at the start, etc., do you mean replace:
Java Syntax (Toggle Plain Text)
  1. public static int[] guess = {5,9,0};
with:
Java Syntax (Toggle Plain Text)
  1. int[] guess = new int[10];
  2. for (int i = 0; i < 10; i++)
  3. guess = i;
Last edited by clueless101; Dec 11th, 2008 at 8:08 pm.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
clueless101 is offline Offline
52 posts
since Mar 2008
Dec 11th, 2008
0

Re: Array dismay!

Java Syntax (Toggle Plain Text)
  1. public static int[] guess = {5,9,0};
with:
Java Syntax (Toggle Plain Text)
  1. int[] guess = new int[10];
  2. for (int i = 0; i < 10; i++)
  3. guess = i;
There's nothing random about this. Three questions you need to decide. One, how many elements should be in the array? Two, what is the range of these random numbers? Three, can there be any repeats in the array?

I interpreted the answers to questions 1 and 2 as 3 and 10, respectively, from post 1, so change the loop from:


Java Syntax (Toggle Plain Text)
  1. int[] guess = new int[10];
  2. for (int i = 0; i < 10; i++)
  3. guess = i;

to

 int[] guess = new int[3];
for (int i = 0; i < 3; i++)
{
    // code
}

You definitely don't want the guess = i; line, since that's not random.

Create a Random object before the loop.
Java Syntax (Toggle Plain Text)
  1. Random rand = new Random ();

Inside the loop, create the random numbers and fill the array.

Java Syntax (Toggle Plain Text)
  1. Random rand = new Random ();
  2. int[] guess = new int[3];
  3. for (int i = 0; i < 3; i++)
  4. {
  5. guess[i] = rand.nextInt (10);
  6. }

Depending on your answer to whether you allow repeats, the code could be a little more involved, but if you don't care, the above is the idea if I'm reading your problem correctly.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,372 posts
since Jan 2008
Dec 11th, 2008
0

Re: Array dismay!

I appreciate the help, but you lost me.

1st ? - Do I remove:
Java Syntax (Toggle Plain Text)
  1. public static int[] guess = {5,9,0};

2nd ? - Where do I insert Random object and related loop?

I'm getting errors out the bazoo, no matter what or where I try.

Thanks for any suggestions.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
clueless101 is offline Offline
52 posts
since Mar 2008
Dec 11th, 2008
0

Re: Array dismay!

I appreciate the help, but you lost me.

1st ? - Do I remove:
Java Syntax (Toggle Plain Text)
  1. public static int[] guess = {5,9,0};
Yes. You are declaring guess here in my example:
Java Syntax (Toggle Plain Text)
  1. int[] guess = new int[3];

so you can't declare it twice.

2nd ? - Where do I insert Random object and related loop?
Probably make both of them non-static class variables and instantiate them in your constructor:

Java Syntax (Toggle Plain Text)
  1. import java.util.Random;
  2.  
  3. public class MatchGame
  4. {
  5. Random rand;
  6. int[] guess;
  7.  
  8. public MatchGame()
  9. {
  10. rand = new Random ();
  11. guess = new int[3];
  12. for (int i = 0; i < 3; i++)
  13. {
  14. guess[i] = rand.nextInt (10);
  15. }
  16.  
  17. // more code
  18. }
  19.  
  20. // more MatchGame class functions here
  21.  
  22.  
  23. public static void main (String args[])
  24. {
  25. new MatchGame ();
  26. }
  27. }
Last edited by VernonDozier; Dec 11th, 2008 at 10:11 pm.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,372 posts
since Jan 2008
Dec 12th, 2008
0

Re: Array dismay!

Quote ...
I end up with 11 errors after I revise my code. Did I miss something? Thanks in advance.
Java Syntax (Toggle Plain Text)
  1. import java.util.Random;
  2. public class MatchGame
  3. {
  4. Random rand;
  5. int[] guess;
  6.  
  7. public MatchGame()
  8. {
  9. rand = new Random();
  10. guess = new int[3];
  11. for (int i = 0; i < 3; i++)
  12. {
  13. guess[i] = rand.nextInt (10);
  14. }
  15.  
  16. {
  17. MatchGame();
  18. }
  19.  
  20. public static void MatchGame()
  21. {
  22. int o = 3;
  23. boolean arraysMatch = true;
  24. String input;
  25. int i = 0;
  26. int[] x = new int[o];
  27. for (i = 0; i < o; i++)
  28. {
  29. input = JOptionPane.showInputDialog("Input your 3 guesses: "+ (i + 1));
  30. x[i] = Integer.parseInt(input);
  31. }
  32. i = 0;
  33. if (guess.length != x.length)
  34. arraysMatch = false;
  35.  
  36. while (arraysMatch && i < guess.length)
  37. {
  38. if (guess[i] != x[i])
  39. arraysMatch = false;
  40. i++;
  41. }
  42. if (arraysMatch)
  43. JOptionPane.showMessageDialog(null, "Hooray! You matched all 3 numbers.");
  44. else
  45. JOptionPane.showMessageDialog(null, "Sorry, you didn't match!");
  46.  
  47. results();
  48. }
  49. public static void results()
  50. {
  51. JOptionPane.showMessageDialog(null, "The Correct Numbers: " + guess[0] +
  52. guess[1] + guess[2]);
  53. }
  54.  
  55. public static void main (String[]args)
  56. {
  57. new MatchGame ();
  58. }
  59. }
  60. }






ÏÏÏ ----jGRASP exec: javac -g C:\Program Files\MatchGame.java
ÏÏÏ
ÏÏMatchGame.java:34: illegal start of expression
ÏÏÏ public MatchGame()
ÏÏÏ ^
ÏÏMatchGame.java:34: ';' expected
ÏÏÏ public MatchGame()
ÏÏÏ ^
ÏÏMatchGame.java:63: illegal start of expression
ÏÏÏ public static void results()
ÏÏÏ ^
ÏÏMatchGame.java:63: illegal start of expression
ÏÏÏ public static void results()
ÏÏÏ ^
ÏÏMatchGame.java:63: ';' expected
Ï Ï public static void results()
ÏÏÏ ^
ÏÏMatchGame.java:63: ';' expected
ÏÏÏ public static void results()
ÏÏÏ ^
ÏÏMatchGame.java:69: illegal start of expression
ÏÏÏ public static void main (String[]args)
ÏÏÏ ^
ÏÏMatchGame.java:69: illegal start of expression
ÏÏÏ public static void main (String[]args)
ÏÏÏ ^
Ï MatchGame.java:69: ';' expected
ÏÏÏ public static void main (String[]args)
ÏÏÏ ^
ÏÏMatchGame.java:69: '.class' expected
ÏÏÏ public static void main (String[]args)
ÏÏÏ ^
ÏÏMatchGame.java:69: ';' expected
ÏÏÏ public static void main (String[]args)
ÏÏÏ ^
ÏÏÏ11 errors
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
clueless101 is offline Offline
52 posts
since Mar 2008
Dec 12th, 2008
0

Re: Array dismay!

Start with my skeleton, compile it, run it without errors, then put in your code a little bit at a time. Look at your brackets, look at your main function, look at your indentation. Remove any brackets that don't serve a purpose, indent consistently so you know what code is part of which section of brackets. Look at anything in your code that says "static" and if you don't know why you are making it static, don't make it static. main has to be static, but for everything else, if you don't need to make it static, don't. If you have two functions with the same name, rename one. If a function is not called, comment it out.

You cannot debug code until you have consistent indentation. It will really help you see what's wrong.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,372 posts
since Jan 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: how to add smack packge to API
Next Thread in Java Forum Timeline: Getting integer values from a JTable





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC