guidance and or help is appreciated :-D

OK so im writing an application, PlayBullsAndBears. (its a number-gueesing game) and basically i have to use arrays. The number is to lie between 1000 and 9999, inclusive, so the # consists of 4 digits.Then the computer will giv the player feedback for them t make a second guess, third, and so forth. The computer keeps track of how many guesses it took to get the number, and displays the result as the last action of the application. And the computer lets u kno how many bulls and how many bears there were in da guess *yes i kno im confused 2 or well maybe jus me :-\* Neway a bull occurs when a digit in the number making up the users guess corresponds directly by position with a digit in the number the user must guess. a bear occurs when the users guess includes one of the digits contained in the number to guess where the digit is out of position with respect to the number to guess...i think that enough bckgrn info..so this is wat i hav and i know it might make you want to cringe and vomit but im in the "trying" process haha....yea and i kno i need to call more methods is that im just a little lost so jus need a help n thx in advance to whoever replies:

public class GuessingGame

{

private static final int TOTAL_ELEMENTS;

private static final Scanner keyboard = new Scanner(System.in);

public static void main(String[] args)

{

int guess;

int goal;

int[]targetDigits = makeArray();

int[]guessDigits = makeArray();

}

private static int[] makeArray()

{

int[] local = new int[TOTAL_ELEMENTS];

System.out.print("Enter seed value for random numbergenerator: ");

int seed = keyboard.nextInt();

keyboard.nextLine();

Random rng = new Random(seed);

for(int i = 0; i <TOTAL_ELEMENTS; i++)

local = 1 + rng.nextInt(5);

return local;

System.out.print(" Enter your guess for this turn: ");

int guess = keyboard.nextInt();

keyboard.nextLine();

}

private static int linearSearch(int searchValue, int[] array)

{

int i = 0;

boolean found = false;

while (i >< TOTAL_ELEMENTS && !found)

if(array == searchValue)

found = true;

else

i++;

if(found)

return i;

else

return -1;

}

private static boolean inequal(int [] arrayA, int [] arrayB)

{

int i =0;

boolean stillEqual = true;

while(i <TOTAL_ELEMENTS && stillEqual)

if(arrayA !=arrayB)

stillEqual = false;

else

i++;

return !stillEqual;

}

private static boolean bears(int [] array)

{

int i = 0;

boolean stillSorted= true

while (i>< TOTAL_ELEMENTS - 1 && stillSorted)

if(array > array [i+1])

stillSorted = false

else

i++;

return stillSorted:

}

}

And this is how a run should look like:

Enter a seed value to get the mystery number: 548657689

Enter your guess for this turn: 1234

With the guess 1234, you have 0 bulls and 1 bear.

Enter your guess for this turn: 5678

With the guess 5678, you have 0 bulls and 0 bears.

Enter your guess for this turn: 9001

With the guess 9001, you have 2 bulls and 0 bears.

Enter your guess for this turn: 2002

With the guess 2002, you have 2 bulls and 1 bear.

Enter your guess for this turn: 2022

With the guess 2022, you have 2 bulls and 1 bear.

Enter your guess for this turn: 2020

With the guess 2020, you have 3 bulls and 0 bear.

Enter your guess for this turn: 2222

With the guess 2222, you have 2 bulls and 0 bears.

Enter your guess for this turn: 9022

With the guess 9022, you have 4 bulls and 0 bear.

It took you 8 trials to get the value of 9022.

[3911 byte] By [hoopza] at [2007-11-26 20:01:44]
# 1

1. When posting code use code tags. Highlight the code and click the code button above the message box. This will retain the formatting. Copy your code form the source not your first post. eg:

for(int index = 0; index < MAX; index++) {

// do something

}

2. Ask a specific question. "Here's my code, fix it for me" doesn't cut it around here.

floundera at 2007-7-9 23:00:29 > top of Java-index,Java Essentials,New To Java...
# 2

public class GuessingGame

{

private static final int TOTAL_ELEMENTS;

private static final Scanner keyboard = new Scanner(System.in);

public static void main(String[] args)

{

int guess;

int goal;

int[]targetDigits = makeArray();

int[]guessDigits = makeArray();

}

private static int[] makeArray()

{

int[] local = new int[TOTAL_ELEMENTS];

System.out.print("Enter seed value for random number generator: ");

int seed = keyboard.nextInt();

keyboard.nextLine();

Random rng = new Random(seed);

for(int i = 0; i <TOTAL_ELEMENTS; i++)

local[i] = 1 + rng.nextInt(5);

return local;

System.out.print(" Enter your guess for this turn: ");

int guess = keyboard.nextInt();

keyboard.nextLine();

}

private static int linearSearch(int searchValue, int[] array)

{

int i = 0;

boolean found = false;

while (i >< TOTAL_ELEMENTS && !found)

if(array [i] == searchValue)

found = true;

else

i++;

if(found)

return i;

else

return -1;

}

private static boolean inequal(int [] arrayA, int [] arrayB)

{

int i =0;

boolean stillEqual = true;

while(i <TOTAL_ELEMENTS && stillEqual)

if(arrayA[i] !=arrayB[i])

stillEqual = false;

else

i++;

return !stillEqual;

}

private static boolean bears(int [] array)

{

int i = 0;

boolean stillSorted= true

while (i>< TOTAL_ELEMENTS - 1 && stillSorted)

if(array[i] > array [i+1])

stillSorted = false

else

i++;

return stillSorted:

}

}

Oh no i didnt mean to give the impression that i wanted people to write the code for me, I just wanted to know what i needed to add to my current code so i can make it better, not for people to do it for me I just wrote all of that down so people can get an idea of what it is im trying to do :-) but yea im still gonna try n write the application I jus need help....not a "do it for me solution" so yea..o n hope the posted code looks better now :-D

hoopza at 2007-7-9 23:00:29 > top of Java-index,Java Essentials,New To Java...
# 3
You still haven't asked a specific question. Do you compiler errors? Post them and indicate on which line they occur. Is your output wrong? Then post actual and expected output for comparison. You need to provide as much info as possible for us to help you.
floundera at 2007-7-9 23:00:29 > top of Java-index,Java Essentials,New To Java...
# 4

im so sry sweatie, but ok one question i have is how can i call a method for bulls or bears, the way i had it in the posted code was:

private static boolean bears(int [] array)

{

int i = 0;

boolean stillSorted= true ;

while (i< TOTAL_ELEMENTS - 1 && stillSorted)

if(array[i] > array [i+1])

stillSorted = false ;

else

i++;

return stillSorted;

}

did that need any tweaking?And where i wrote da keyboard.nextint it keeps on giving me "keyboard cannot be resolved" but i did put :

private static final Scanner keyboard = new Scanner(System.in);

so i dont know if thats the way it was supposed to done, do you think its because im using eclipse to write the code because it compiles as you write the code, my professor suggested it so thats why im using it, but i cant really compile nething because i havent been able to write the methods for bulls and bears, well hope i asked a ? this time hun :-)

hoopza at 2007-7-9 23:00:29 > top of Java-index,Java Essentials,New To Java...
# 5

ok im still workin on the code but i want to know if i called some of the methods correctly or not, if i didnt tell me and i will try again and elcipse keeps on sayin that "keyboard cannot be resolved" :

public static void main(String[] args)

{

int guess;

int goal;

int[]targetDigits = makeArray();

int[]guessDigits = makeArray();

int bears = 0;

int bulls = 0;

getValueToGuess(guess);

}

private static int[] makeArray()

{

int[] local = new int[TOTAL_ELEMENTS];

System.out.print("Enter seed value for random number generator: ");

int seed = keyboard.nextInt();

keyboard.nextLine();

Random rng = new Random(seed);

return 1000 + rng.nextInt(9000);

}

private static int getValueToGuess( int guess)

System.out.print(" Enter your guess for this turn: ");

int guess = keyboard.nextInt();

keyboard.nextLine();

}

hoopza at 2007-7-9 23:00:29 > top of Java-index,Java Essentials,New To Java...
# 6
hey sweaties do ya think a do while loop will help...well thats a few ?s i hav left hehe :-)
hoopza at 2007-7-9 23:00:29 > top of Java-index,Java Essentials,New To Java...
# 7
so uh no one has ne suggestions?........well yea im still stuck...waitin 4 someone 2 respond *sighs* its bcuz im a grl right j/k :-\
hoopza at 2007-7-9 23:00:29 > top of Java-index,Java Essentials,New To Java...
# 8
You're using the Scanner class so you have to import it at the top of your java file.import java.io.*;Message was edited by: lethalwire
lethalwirea at 2007-7-9 23:00:29 > top of Java-index,Java Essentials,New To Java...
# 9
well i tried that sweatie like u said but it still tells me that "keyboard cannot be resolved" :-\ do u think its bcuz im using eclipse to write the code cuz it compiles as u write the code.......
hoopza at 2007-7-9 23:00:30 > top of Java-index,Java Essentials,New To Java...
# 10
You usually get that error becuase you have declared a variable out of scope. Can you post you latest version of the code.
floundera at 2007-7-9 23:00:30 > top of Java-index,Java Essentials,New To Java...
# 11
Can you post the latest version of your code please.
lethalwirea at 2007-7-9 23:00:30 > top of Java-index,Java Essentials,New To Java...
# 12

well not much has changed, ive just been trying to figure out this problem b4 continuing with erything else....... yea :-\

import java.io.*;

public class GuessingGame

{

private static final int TOTAL_ELEMENTS;

private static final Scanner keyboard = new Scanner(System.in);

public static void main(String[] args)

{

int guess;

int goal;

int[]targetDigits = makeArray();

int[]guessDigits = makeArray();

int bears = 0;

int bulls = 0;

getValueToGuess(guess);

}

private static int[] makeArray()

{

int[] local = new int[TOTAL_ELEMENTS];

System.out.print("Enter seed value for random number generator: ");

int seed = keyboard.nextInt();

keyboard.nextLine();

Random rng = new Random(seed);

return 1000 + rng.nextInt(9000);

}

private static int getValueToGuess( int guess)

{

System.out.print(" Enter your guess for this turn: ");

int guess = keyboard.nextInt();

keyboard.nextLine();

return guess;

}

private static int linearSearch(int searchValue, int array)

{

int i = 0;

boolean found = false;

while (i < TOTAL_ELEMENTS && !found)

if(array [i] == searchValue)

found = true;

else

i++;

if(found)

return i;

else

return -1;

}

private static boolean inequal(int [] arrayA, int [] arrayB)

{

int i =0;

boolean stillEqual = true;

while(i <TOTAL_ELEMENTS && stillEqual)

if(arrayA[i] !=arrayB[i])

stillEqual = false;

else

i++;

return !stillEqual;

}

private static boolean bears(int [] array)

{

int i = 0;

boolean stillSorted= true ;

while (i>< TOTAL_ELEMENTS - 1 && stillSorted)

if(array[i] > array [i+1])

stillSorted = false ;

else

i++;

return stillSorted;

}

}

hoopza at 2007-7-9 23:00:30 > top of Java-index,Java Essentials,New To Java...
# 13
Ahh! Scanner is actually in the util package so you will need.import java.util.Scanner;
floundera at 2007-7-9 23:00:30 > top of Java-index,Java Essentials,New To Java...
# 14

First error I receive is on line 81.

You have written:

while (i>< TOTAL_ELEMENTS - 1 && stillSorted)

is "i" Less than or greater than TOTAL_ELEMENTS ?

When you fix that error, You'll recieve 6 more.

FOr the random errors, You also need to

import java.util.*;

With this error:

GuessingGame.java:32: incompatible types

found: int

required: int[]

return 1000 + rng.nextInt(9000);

You are trying to return a regular int value when the method header said you are needing to return an array of ints.

Here:

^

GuessingGame.java:39: guess is already defined in getValueToGuess(int)

int guess = keyboard.nextInt();

You are trying to declare a variable that has already been declared.

Just seen.

Scanner is util package!? My life is ruined! :) Good catch.

Message was edited by:

lethalwire

lethalwirea at 2007-7-9 23:00:30 > top of Java-index,Java Essentials,New To Java...
# 15
P.S. some people might be offended at being called sweatie. Ok snookums? ;)
floundera at 2007-7-21 17:49:14 > top of Java-index,Java Essentials,New To Java...
# 16

For the last error I received:

C:\Java>javac GuessingGame.java

GuessingGame.java:52: array required, but int found

if(array == searchValue)

You have the parameters ALMOST right.

int array

needs to become

int[] array

private static int linearSearch(int searchValue, int[] array) // fixed

{

int i = 0;

boolean found = false;

while (i < TOTAL_ELEMENTS && !found)

if(array[i] == searchValue)

found = true;

else

i++;

if(found)

return i;

else

return -1;

}

*Edit*

2 More errors after this:

GuessingGame.java:4: variable TOTAL_ELEMENTS might not have been initialized

public class GuessingGame

^

GuessingGame.java:19: variable guess might not have been initialized

getValueToGuess(guess);

I'm sure you can fix those.

Message was edited by:

lethalwire

lethalwirea at 2007-7-21 17:49:14 > top of Java-index,Java Essentials,New To Java...
# 17

ok i tried wat u said flounder n i still get an error *sighs* smh it says "the method inequal (int[], int[]) from the type GuessingGame is never used locally"

this is where it occured :

private static boolean inequal(int [] arrayA, int [] arrayB)

and

private static boolean bears(int [] array)

what is it saying that i have to declare it in main?!?!? :-\

hoopza at 2007-7-21 17:49:14 > top of Java-index,Java Essentials,New To Java...
# 18
o n sry bout da sweatie thing :-\ i was jus tryin 2 b nice and i guess its a bad habit haha :-)
hoopza at 2007-7-21 17:49:14 > top of Java-index,Java Essentials,New To Java...
# 19
I compiled your code without any error. Maybe you didn't check all the errors I posted or something? Check back over them and be sure.Then maybe post a the errors you are receiving.Message was edited by: lethalwire
lethalwirea at 2007-7-21 17:49:14 > top of Java-index,Java Essentials,New To Java...
# 20
n 2 lethalwire uh wen i put [] in that line of code i get da error of "never used locally" so i hav 2 declare it 2 :-\ ?!?!? now im strtin 2 get confused......kinda
hoopza at 2007-7-21 17:49:14 > top of Java-index,Java Essentials,New To Java...
# 21
ok i think i almost had it *sighs* but its sayin that Random cannot be resolved to a type.....i dnt understand what that means...:-\
hoopza at 2007-7-21 17:49:14 > top of Java-index,Java Essentials,New To Java...
# 22

YAY it compiled wow so im like hlf way now LOL haha smh but i wana kno uh i get 2 duplicate lines on the run.....how could i adjust this? this is the run so far:

Enter seed value for random number generator: 55555

Enter seed value for random number generator: 55555

Enter your guess for this turn: 555555

i need it 2 say Enter seed value for random number generator: only once so heres the revised code :-D

import java.util.Random;

import java.util.Scanner;

public class GuessingGame

{

private static final int TOTAL_ELEMENTS = 5;

private static final Scanner keyboard = new Scanner(System.in);

public static void main(String[] args)

{

int guess = 0;

int goal;

int[]targetDigits = makeArray();

int[]guessDigits = makeArray();

int bears = 0;

int bulls = 0;

getValueToGuess(guess);

}

private static int [] makeArray()

{

int[] local = new int[TOTAL_ELEMENTS];

System.out.print("Enter seed value for random number generator: ");

int seed = keyboard.nextInt();

keyboard.nextLine();

Random rng = new Random(seed);

seed = 1000 + rng.nextInt(9000);

return local;

}

private static int getValueToGuess( int guess)

{

System.out.print("Enter your guess for this turn: ");

int guess1 = keyboard.nextInt();

keyboard.nextLine();

return guess;

}

private static int linearSearch(int [] searchValue, int [] array)

{

int i = 0;

boolean found = false;

while (i < TOTAL_ELEMENTS && !found)

if(array [i] == searchValue)

found = true;

else

i++;

if(found)

return i;

else

return -1;

}

private static boolean inequal(int [] arrayA, int [] arrayB)

{

int i =0;

boolean stillEqual = true;

while(i <TOTAL_ELEMENTS && stillEqual)

if(arrayA[i] !=arrayB[i])

stillEqual = false;

else

i++;

return !stillEqual;

}

private static boolean targetDigits(int [] array)

{

int i = 0;

boolean stillSorted= true ;

while (i >< TOTAL_ELEMENTS - 1 && stillSorted)

if(array[i] > array [i+1])

stillSorted = false ;

else

i++;

return stillSorted;

}

}

hoopza at 2007-7-21 17:49:14 > top of Java-index,Java Essentials,New To Java...
# 23
You call makeArray twice and it asks for the seed value...Edit: Does that code really compile?if (array [i] == searchValue)These are 2 incompatible operands!Message was edited by: Peetzore
Peetzorea at 2007-7-21 17:49:14 > top of Java-index,Java Essentials,New To Java...
# 24
actually it doesnt....it is wat u said....im using eclipse and uh i would assume it ignored it not sure bout dat but...i managed 2 still run it evn though it is a incompatible operand......i jus need 4 it to say that line once....so yea :-\
hoopza at 2007-7-21 17:49:14 > top of Java-index,Java Essentials,New To Java...
# 25

int[]targetDigits = makeArray();

int[]guessDigits = makeArray();

What exactly are these 2 arrays for? I don't see you using them anywhere.

Some other remarks:

You probably have some work here. The result of this function is the same as calling new int[TOTAL_ELEMENTS]; What are you trying to accomplish in makeArray?

private static int [] makeArray()

{

int[] local = new int[TOTAL_ELEMENTS];

System.out.print("Enter seed value for random number generator: ");

int seed = keyboard.nextInt();

keyboard.nextLine();

Random rng = new Random(seed);

seed = 1000 + rng.nextInt(9000);

return local;

}

code]

And this is just an esthetic remark: why do you initialise stillEqual to true, setting it to false when there is a difference and then invert it when you return? You could initialise to false, set to true when different and return as it is...

[code]private static boolean inequal(int [] arrayA, int [] arrayB)

{

int i =0;

boolean stillEqual = true;

while(i <TOTAL_ELEMENTS && stillEqual)

if(arrayA[i] !=arrayB[i])

stillEqual = false;

else

i++;

return !stillEqual;

}

>

Peetzorea at 2007-7-21 17:49:14 > top of Java-index,Java Essentials,New To Java...
# 26

well yea i still hav 2 call more methods i just put it like that temporarily , and as for the code....i was actually following wat my professor had wrote so maybe i wasnt really supposed 2 use it....but as far as wat the program is actually supposed 2 do...its posted on da 1st pg.....but yea the code still has sum wrk 2 b done 2 it n im tryin my best *sighs* :-\

hoopza at 2007-7-21 17:49:14 > top of Java-index,Java Essentials,New To Java...