Array dismay!

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Mar 2008
Posts: 52
Reputation: clueless101 is an unknown quantity at this point 
Solved Threads: 0
clueless101 clueless101 is offline Offline
Junior Poster in Training

Array dismay!

 
0
  #1
Dec 11th, 2008
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.

  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. }
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 53
Reputation: neilcoffey will become famous soon enough neilcoffey will become famous soon enough 
Solved Threads: 6
neilcoffey neilcoffey is offline Offline
Junior Poster in Training

Re: Array dismay!

 
0
  #2
Dec 11th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 52
Reputation: clueless101 is an unknown quantity at this point 
Solved Threads: 0
clueless101 clueless101 is offline Offline
Junior Poster in Training

Re: Array dismay!

 
0
  #3
Dec 11th, 2008
Originally Posted by neilcoffey View Post
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.
To construct a single random object at the start, etc., do you mean replace:
  1. public static int[] guess = {5,9,0};
with:
  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.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,819
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Array dismay!

 
0
  #4
Dec 11th, 2008
Originally Posted by clueless101 View Post
  1. public static int[] guess = {5,9,0};
with:
  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:


  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.
  1. Random rand = new Random ();

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

  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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 52
Reputation: clueless101 is an unknown quantity at this point 
Solved Threads: 0
clueless101 clueless101 is offline Offline
Junior Poster in Training

Re: Array dismay!

 
0
  #5
Dec 11th, 2008
I appreciate the help, but you lost me.

1st ? - Do I remove:
  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.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,819
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Array dismay!

 
0
  #6
Dec 11th, 2008
Originally Posted by clueless101 View Post
I appreciate the help, but you lost me.

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

so you can't declare it twice.

Originally Posted by clueless101 View Post
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:

  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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 52
Reputation: clueless101 is an unknown quantity at this point 
Solved Threads: 0
clueless101 clueless101 is offline Offline
Junior Poster in Training

Re: Array dismay!

 
0
  #7
Dec 12th, 2008
I end up with 11 errors after I revise my code. Did I miss something? Thanks in advance.
  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
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,819
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Array dismay!

 
0
  #8
Dec 12th, 2008
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC