Another java question - please help!

For all coding issues - MODers and programmers, HTML and more.

Moderators: Jeff250, fliptw

Post Reply
Darktalyn1
DBB Admiral
DBB Admiral
Posts: 1699
Joined: Thu Mar 23, 2000 3:01 am

Another java question - please help!

Post by Darktalyn1 »

Okay here is the deal, another project for my Java class... It's due tomorrow at midnight and I have been sitting here trying to get this thing working but no luck. Here is the problem, hopefully I am missing something simple :

The assignment is to make it so that the solutioncode for the game mastermind is created randomly at the start of the project. So, I want to create a 4-index character array that is randomized, with the possibilities being R G B Y W K for Red, Green, Blue, Yellow, White or Black, respectively.

The instructions say this:

* public static char[] makeCode(int numberOfPegs) - creates and returns a new solution sequence based on the number of pegs provided as a parameter.


Here is the snippet of code that I added to my program. It seemed to be working by itself in a test file but when I put it into the main code I've been doing over the semester it doesn't work.

Code: Select all


class P3 {
    int numberOfPegs = 4;
	 public static char[] makeCode(int numberOfPegs){
	     char[] solution = new char[numberOfPegs];
		  int i = 0;
		  for(i = 0; i < solution.length; i++){
		      int r = (int)(Math.random()*6);
				if(r == 0){
				    solution[i] = 'R';
					 }else if (r == 1){
					 solution[i] = 'G';
					 }else if (r == 2){
					 solution[i] = 'B';
					 }else if (r == 3){
					 solution[i] = 'Y';
					 }else if (r == 4){
					 solution[i] = 'W';
					 }else if (r == 5){
					 solution[i] = 'B';
					 }
				}//close for loop
	 }//close makeCode

When I try to compile I get the error "cannot resolve symbol : variable solution" This occurs when I try to compare the solution to pegs. wtf? I am doing something wrong here.

Please help!
User avatar
MehYam
DBB Head Flapper
DBB Head Flapper
Posts: 2184
Joined: Thu Nov 05, 1998 12:01 pm
Location: Mountain View, CA, USA
Contact:

Post by MehYam »

Could it be because you commented out the declaration of 'solution' in the second method? :D
User avatar
MehYam
DBB Head Flapper
DBB Head Flapper
Posts: 2184
Joined: Thu Nov 05, 1998 12:01 pm
Location: Mountain View, CA, USA
Contact:

Post by MehYam »

Suggestion #1. Instead of:

Code: Select all

           if(r == 0){
                solution[i] = 'R';
                }else if (r == 1){
                solution[i] = 'G';
                }else if (r == 2){
                solution[i] = 'B';
                }else if (r == 3){
                solution[i] = 'Y';
                }else if (r == 4){
                solution[i] = 'W';
                }else if (r == 5){
                solution[i] = 'B';
                }//close if statements
...prefer a switch statement or table lookup. For instance:

Code: Select all

       lookup = however_you_declare_a_char_array_in_Java[] ={'R', 'G', 'B', 'Y'};

       //... in your loop you just have one line:
       solution[i] = lookup[r];
By the way, you map to 'B' twice.
User avatar
MehYam
DBB Head Flapper
DBB Head Flapper
Posts: 2184
Joined: Thu Nov 05, 1998 12:01 pm
Location: Mountain View, CA, USA
Contact:

Post by MehYam »

Suggestion #2. Instead of:

Code: Select all

switch (pegs[i]) {

                        case 'r':

                        case 'R': pegs[i] = 'R';

                                  break;

Why not replace the entire switch statement with a single line:

Code: Select all

   pegs[i] = Java's.Version.Of.to_uppercase(pegs[i]);
Darktalyn1
DBB Admiral
DBB Admiral
Posts: 1699
Joined: Thu Mar 23, 2000 3:01 am

Post by Darktalyn1 »

Good thing you caught I mapped to B twice! Heh!! K is supposed to be for black - wooops

Thanks for the help guys... I also found out late last night the reason it was not compiling is because I did not call the makeCode method within the main method!

Btw the reason the original declaration of solution was commented out was because it was the old code from the previous assignment. Originally we just input the solution directly, but in this iteration our code is supposed to randomly generate the solution sequence. Sorry for not clarifying that...
Darktalyn1
DBB Admiral
DBB Admiral
Posts: 1699
Joined: Thu Mar 23, 2000 3:01 am

Post by Darktalyn1 »

Second method I have to write for this project... It is supposed to use a bubble sort to sort the indices of an array lexicographically. This is what I have

Code: Select all


public static void sort(char[] chars){
	    char temp;
		 int i;
	    boolean sort = true;
	    while (sort){
	        sort = false; //assume this is the last pass over array
		     for(i = 0; i < chars.length-1; i++){
		         if( String(chars[i]).compareTo( String(chars[i+1])) > 0){
			          //exchange elements
			          temp = chars[i]; chars[i] = chars[i+1]; chars[i+1] = temp;
			          sort = true; //after an exhange, we look again
			      }//close if statement
	        }//close for loop
		 }//close while loop
	}//close void sort method

Getting an error however, says cannot resolve symbol, symbol : method String (char)

I'm trying to typecast the array incides as String because compareTo only works with Strings (as far as I know from reading the book).

Must be doing something wrong here however... any ideas?
User avatar
fliptw
DBB DemiGod
DBB DemiGod
Posts: 6458
Joined: Sat Oct 24, 1998 2:01 am
Location: Calgary Alberta Canada

Post by fliptw »

because you don't need to use compareTo with chars, you can use a simple if statement.

Code: Select all

char i;
char b;

if (i==b)
however, if you want to use the compareTo, you need to create a new String.
Darktalyn1
DBB Admiral
DBB Admiral
Posts: 1699
Joined: Thu Mar 23, 2000 3:01 am

Post by Darktalyn1 »

Oops, found out today in the teacher's office hours I don't even need to use compareTo, just < or >

I was just making it all harder on myself. Made that one change and everything runs
Post Reply