HELP!Going MAD with ARRAYS and random numbers
Hi everyone
I am trying to create a program that generates four numbers with ONLY RandGen and they have to be between 1 and 3 inclusive. Then I need to store each number separately (treated as a whole number) into an array. Then I need to prompt for and accept input of a single whole number consisting of four digits(I dont know if I should use a string or int for the input) and the reason for this is because (and this is the MAD part) I need to then check the RANDOM number-array against the input and if you have the SAME number in the SAME place in both variables then you award a point for each correct placement of the same number and give a total. So I need to read each number(digit) from the array and then compare against the input number digit by digit I guess.
Im going mad with this and any help will be immensely appreciated. Thank you.
1 2 3 2
2 3 2 2 -Same number, same place award 1 point and give total score
[957 byte] By [
zed77] at [2007-11-11 6:42:01]

# 1 Re: HELP!Going MAD with ARRAYS and random numbers
We're here to help YOU write code, not to write your code.
Break the problem up into small parts and write the code to solve that part.
Then put the parts together to solve the whole problem.
When you have trouble, post your code with error messages and ask questions about that.
Good luck.
Norm at 2007-11-11 22:40:27 >

# 2 Re: HELP!Going MAD with ARRAYS and random numbers
Answers given here:
http://forum.java.sun.com/thread.jspa?threadID=656830
# 3 Re: HELP!Going MAD with ARRAYS and random numbers
I apologize for that probably Im asking too much. Im a novice as you can probably tell. I have problems all over the place but for example I cant put the numbers generated by the RanGen into an int array it gives me "incompatible types" error I can only store it into an integer --
int number;
number = generator.nextInt(2);
so i can't even start to think about how am i going to compare this with the input which needs to be collected from only one prompt. But thank you anyway I appreciate the advice.
zed77 at 2007-11-11 22:42:30 >

# 4 Re: HELP!Going MAD with ARRAYS and random numbers
We don't know what kind of class RanGen is (it's not part of the core API), but it's a good bet that it returns a different type than an int. You'll need to assign the returned value to the correct type of variable.
The Java Tutorial - Trail: Learning the Java Language (http://java.sun.com/docs/books/tutorial/java/index.html)
# 5 Re: HELP!Going MAD with ARRAYS and random numbers
Regarding this problem:
cant put the numbers generated by the RanGen into an int array it gives me "incompatible types" error I can only store it into an integer
If you would post the code that is causing this error it would help.
I assume its like:
int[] arInt = new int[10];
arInt = generator.nextInt();
The problem here is that arInt is an array not an int.
To put an int into an array of ints:
arInt[x] = generator.nextInt();
would work because arInt[x] is an int.
An array is an object that can hold many instances of some type. Read your text about them and how to use them.
Norm at 2007-11-11 22:44:39 >

# 6 Re: HELP!Going MAD with ARRAYS and random numbers
Thanks a lot Norm you were absolutely right about the error. What do you suggest about the input considering the fact that I have to check digit by digit? It has been suggested to me that I should store this in a string and then convert each character into an integer and finally check digit by digit. Do you think that this is the right way to go? Thank you for the help.
zed77 at 2007-11-11 22:45:37 >

# 7 Re: HELP!Going MAD with ARRAYS and random numbers
What are you checking the digits for?
What do you mean by digits? Are they chars? Or in a string?
For example: '1' or '3' or "1a4"
Or is the data int?
There are methods in the Character class and the String class and the Integer class that will do a lot of testing.
Norm at 2007-11-11 22:46:35 >

# 8 Re: HELP!Going MAD with ARRAYS and random numbers
The data has to be taken in one go(whole number) so Im currently storing it into a string -(1234).Then I was told to convert the string (1234) by reading each character and converting each character into an int so then I can check each digit in the input number against each digit (element) in the Random Array and if there is a match -same number in the same place then award a point. I hope that this makes any sense to you it almost does make sense to me.Almost.:) Thanks Norm
zed77 at 2007-11-11 22:47:38 >

# 9 Re: HELP!Going MAD with ARRAYS and random numbers
I love how 99% of people at code forums never give people answers. Instead they type up paragraphs of useless bs that dosnt really help much, or say things like "why would you want to do that". If you dont feel like giving a concrete answer to help someone, then dont bother to post at all.
I coded this up real quick and it does what you where asking. I hope you can understand it and realize what you where doing wrong in your code.
import java.util.*;
public class RandomNumber {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
Random randGen = new Random();
int[] randomStore = new int[4]; // computer generated random numbers
int[] userStore = new int[4]; // the 4 numbers the user picks
int score = 0;
// populate the randomStore array
for ( int i = 0 ; i < randomStore.length ; i++ ) {
randomStore[i] = (int) (Math.random() * 3) + 1;
}
System.out.println("Enter four numbers from 1-3. ");
// ask user for 4 numbers between 1 and 3
for ( int i = 0 ; i < userStore.length ; ) {
System.out.println("Enter number "+ (i+1) + ": ");
int n = scan.nextInt();
// make sure the numbers are between 1 - 3
if ( n >= 1 && n <= 3 ) {
userStore[i] = n;
i++;
} else {
System.out.println("Please only enter a 1, 2, or 3.");
System.out.println("Starting over.");
System.out.println();
i = 0;
}
} // end for
// Print out the numbers the computer generated
System.out.print("randStore: ");
for ( int i = 0; i < randomStore.length ; i++ ) {
System.out.print( randomStore[i] + " " );
}
// Print out the numbers the user picked
System.out.println();
System.out.print("userStore: ");
for ( int i = 0; i < userStore.length ; i++ ) {
System.out.print( userStore[i] + " " );
}
// Find out how many matchs the user got and award points.
for ( int i = 0; i < 4 ; i++ ) {
if ( userStore[i] == randomStore[i] ){
score++;
}
} // end for
System.out.println();
System.out.println("You got " + score + " matchs!");
}
}
and if you NEED to have the user enter the 4 numbers in a row like 1234 in one shot as a string, then you could use the substring method in string to chop that one string into 4 different strings, and turn each new string into an Int with Integer.parseInt();
# 10 Re: HELP!Going MAD with ARRAYS and random numbers
I love how 99% of people at code forums never give people answers. Instead they type up paragraphs of useless bs that dosnt really help much, or say things like "why would you want to do that". If you dont feel like giving a concrete answer to help someone, then dont bother to post at all. I coded this up real quick and it does what you where asking. I hope you can understand it and realize what you where doing wrong in your code.
A lot of people can find the answer to their own question by getting pushed in the right direction. With very obvious school assignments (like this post) it's not a good idea to "spoonfeed" the answer to the OP. And especially not by saying "I hope you understand it"... What if the OP doesn't? It would have been better for the OP to figure it out by him/her self, that way people really learn something about programming. I hope you understand that.
# 11 Re: HELP!Going MAD with ARRAYS and random numbers
""and if you NEED to have the user enter the 4 numbers in a row like 1234 in one shot as a string, then you could use the substring method in string to chop that one string into 4 different strings, and turn each new string into an Int with Integer.parseInt();""
Thats exactly what I have to do. Get one input in one go and then do the comparisons with the array. So now Im working on that and its not pretty:) I can tell you that much. But thanks for the sample code it helped me understand the problem better. Thanks
zed77 at 2007-11-11 22:50:39 >

# 12 Re: HELP!Going MAD with ARRAYS and random numbers
A lot of people can find the answer to their own question by getting pushed in the right direction. With very obvious school assignments (like this post) it's not a good idea to "spoonfeed" the answer to the OP. And especially not by saying "I hope you understand it"... What if the OP doesn't? It would have been better for the OP to figure it out by him/her self, that way people really learn something about programming. I hope you understand that.
Sometimes people get stuck with code and they do need some help and advice.
It helps when you communicate the problem with other people and to learn more about it and programming in general. I wasn't asking for a complete solution but it helps when you see snippets of code to see how to approach a problem. So i can only PUSH you in one direction buddy
zed77 at 2007-11-11 22:51:42 >

# 13 Re: HELP!Going MAD with ARRAYS and random numbers
Sometimes people get stuck with code and they do need some help and advice.
It helps when you communicate the problem with other people and to learn more about it and programming in general.
I agree with you.
I wasn't asking for a complete solution
I never said you were asking for that.
but it helps when you see snippets of code to see how to approach a problem.
Snippets, yes. I agree with you again. But the user I commented gave a complete solution; that's not really educational.
So i can only PUSH you in one direction buddy
I don't know what you mean by that. I never said anything wrong to you.
Ah well, good luck with your assignment anyway.
# 14 Re: HELP!Going MAD with ARRAYS and random numbers
""and if you NEED to have the user enter the 4 numbers in a row like 1234 in one shot as a string, then you could use the substring method in string to chop that one string into 4 different strings, and turn each new string into an Int with Integer.parseInt();""
Here's another snippet for you:
public class InputExample {
public static void main(String[] args) {
String input = "10 12 35 94 55";
String[] chopItUp = input.split(" ");
int[] numbers = new int[chopItUp.length];
for(int i = 0; i < chopItUp.length; i++) {
try {
numbers[i] = Integer.parseInt(chopItUp[i]);
}
catch(Exception e) {}
}
System.out.print("Number array: ["+numbers[0]);
for(int i = 1; i < chopItUp.length; i++) {
System.out.print(","+numbers[i]);
}
System.out.print("]");
}
}
# 15 Re: HELP!Going MAD with ARRAYS and random numbers
Thats exactly what I have to do. Get one input in one go and then do the comparisons with the array. So now Im working on that and its not pretty:) I can tell you that much. But thanks for the sample code it helped me understand the problem better. Thanks
And if the numbers have no spaces in them, you can do it like this:
public class InputExample {
public static void main(String[] args) {
String input = "6789";
char[] chars = input.toCharArray();
int[] numbers = new int[chars.length];
for(int i = 0; i < chars.length; i++) {
try {
numbers[i] = Integer.parseInt(""+chars[i]);
}
catch(Exception e) {}
}
System.out.print("Number array: ["+numbers[0]);
for(int i = 1; i < numbers.length; i++) {
System.out.print(","+numbers[i]);
}
System.out.print("]");
}
}
Good luck.
# 16 Re: HELP!Going MAD with ARRAYS and random numbers
So i can only PUSH you in one direction buddy
Here's a last "snippet" to get you you started. Just no more pushing, ok?
import java.util.*;
public class Assignment {
// ...
public static void main(String[] args) {
boolean quit = false;
while(!quit) {
int[] randomList = getNewRandomArray();
System.out.println("\nNew random list generated. ("+arrayToString(randomList)+")");
boolean good = false;
while(!good && !quit) {
int[] userList = getNewUserArray();
int compare = compareLists(randomList, userList);
if(compare == 4) {
good = true;
System.out.println("\nYou got it! Press a key to play again, 'q' to quit.");
Scanner scan = new Scanner(System.in);
String line = scan.nextLine();
if(line.equalsIgnoreCase("q")) {
quit = true;
}
}
else {
System.out.println(compare+" points for attempt: "+arrayToString(userList));
}
}
}
}
// ...
private static String arrayToString(int[] a) {
String str = "";
for(int i = 0; i < a.length; i++) {
str += ""+a[i];
}
return str;
}
// ...
private static int[] getNewRandomArray() {
int[] randArr = new int[4];
for ( int i = 0 ; i < randArr.length ; i++ ) {
randArr[i] = (int)(Math.random()*3)+1;
}
return randArr;
}
// ...
private static int[] getNewUserArray() {
Scanner scan = new Scanner(System.in);
int[] userArr = new int[4];
boolean wrongInput = true;
while(wrongInput) {
System.out.print("\nEnter four numbers from 1-3: ");
try {
String line = scan.nextLine();
userArr = convertStringToArray(line);
if(userArr != null) wrongInput = false;
else {
System.out.println("\nWrong input, try again.\n");
}
}
catch(Exception e) {}
}
return userArr;
}
// ...
private static int[] convertStringToArray(String input) {
if(input.length() == 4) {
char[] chars = input.toCharArray();
int[] numbers = new int[chars.length];
for(int i = 0; i < chars.length; i++) {
try {
numbers[i] = Integer.parseInt(""+chars[i]);
}
catch(Exception e) { return null; }
}
for(int i = 0; i < numbers.length; i++) {
if(numbers[i] < 0 || numbers[i] > 3) return null;
}
return numbers;
}
return null;
}
// ...
private static int compareLists(int[] random, int[] user) {
int score = 0;
for(int i = 0; i < user.length; i++) {
if(random[i] == user[i]) score++;
}
return score;
}
}
# 17 Re: HELP!Going MAD with ARRAYS and random numbers
I don't know what you mean by that. I never said anything wrong to you.
Ah well, good luck with your assignment anyway.
Sorry pal, I guess I overreacted. I thought you were against giving people complete solutions? What happened to you? Youve changed man Thanks for the snippets though, really helpful. By the way, hows the weather in Holland these days? Hows good ol VanBasten?
zed77 at 2007-11-11 22:56:47 >

# 18 Re: HELP!Going MAD with ARRAYS and random numbers
Sorry pal, I guess I overreacted. I thought you were against giving people complete solutions? What happened to you? Youve changed man Thanks for the snippets though, really helpful. By the way, hows the weather in Holland these days? Hows good ol VanBasten?
No prob. I overreacted a bit as well.
Today the weather is perfect! And our "boys" played a moderate 2-2 against Germany with Van Basten recently. But I have confidence in him!
Good luck.