Results 1 to 10 of 10
  1. #1
    silverq_82 is offline Member
    Join Date
    Aug 2007
    Posts
    8
    Rep Power
    0

    Default illegal start of expression & class, interface, or enum expected

    i face these 2 errors when compiling my codes. Both errors are from the codes below. Can anyone help? Thanks in advance!

    Java Code:
    private static void newGame() throws IOException
    	{
    		int size = 4;														//the length for the answer
        	char[] answer;														//answer of the game
        	char[] contents = {'0','1','2','3','4','5','6','7','8','9'};		//the characters appear in the answer
        
        	int max_trial = 10;													//max number of trial
        	int current_trial = 0;												//count the number of trial
        	int num_complete_correct = 0;										//number of correct characters in correct positions
        	int num_partial_correct = 0;										//number of correct characters in wrong positions
            
        	size = 4;															//initialize the answer
    		answer = new char[size];
    		Random ran = new Random();											//randomly generate the answer
    		for(int i=0;i<size;i++){
    	    	answer[i] = contents[ran.nextInt(contents.length)];
    			}
        
        
        
        public boolean check_input(String input){
    	
    	if (input.length() != size){											// check input length
    	    return false;
    	}
    	
    	for(int i=0;i<input.length();i++){										//check input content
    	    boolean contain = false;
    	    for(int j=0;j<contents.length&&!contain;j++){
    		if (input.charAt(i)==contents[j]){
    		    contain = true;
    		}
    	    }
    	    if (!contain){
    		return false;
    	    }
    	}
    	return true;
        }
        
        public boolean guess(String input){
    	
    	num_complete_correct = 0;												//initialize the variables
    	num_partial_correct = 0;
    	boolean[] match_input = new boolean[size];
    	boolean[] match_answer = new boolean[size];
    	for(int i=0;i<size;i++){
    	    match_input[i] = false;
    	    match_answer[i] = false;
    	}
    	int i,j;
    	
    	for(i=0;i<size;i++){													//check correct input in correct position
    	    if (input.charAt(i) == answer[i]){
    		num_complete_correct++;
    		match_input[i] = true;
    		match_answer[i] = true;
    	    }
    	}
    	
    	for(i=0;i<size;i++){													//check correct input in wrong position
    	    if (match_input[i]){
    		continue;
    	    }
    	    for(j=0;j<size;j++){
    		if (match_answer[j]){
    		    continue;
    		}
    		if (input.charAt(i)==answer[j]){
    		    match_input[i] = true;
    		    match_answer[j] = true;
    		    num_partial_correct++;
    		    break;
    		}
    	    }
    	}
    	current_trial++;
    	if (current_trial >= max_trial)
    	{
    		return false;
    	}
    	else
    	{
    		return true;
    	}
        }
        
        public int get_all_correct(){
    	return num_complete_correct;
        }
        
        public int get_partial_correct(){
    	return num_partial_correct;
        }
        
        public boolean complete(){
    	return (num_complete_correct==size);
        }
        
        public void set_answer(String input){
    	answer = input.toCharArray();
        }
        
        public String get_answer(){
    	return String.valueOf(answer);
        }
        
    	}

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    Learning to format your code will help avoid/detect this kind of thing.
    Java Code:
    	private static void newGame() throws IOException
    	{
    		int size = 4;	//the length for the answer
    	    	char[] answer;	//answer of the game
    		//the characters appear in the answer
    	    	char[] contents = {'0','1','2','3','4','5','6','7','8','9'};
        
        		int max_trial = 10;	//max number of trial
    	    	int current_trial = 0;	//count the number of trial
    		//number of correct characters in correct positions
        		int num_complete_correct = 0;
    		//number of correct characters in wrong positions
    	    	int num_partial_correct = 0;
            
     	   	size = 4;	//initialize the answer
    		answer = new char[size];
    		Random ran = new Random();											//randomly generate the answer
    		for(int i=0;i<size;i++){
    	    		answer[i] = contents[ran.nextInt(contents.length)];
    		}
    	}  // this curley brace ended up at the end (commented out)
    
        
        	public boolean check_input(String input){
    		...
        
        public String get_answer(){
    	return String.valueOf(answer);
        }
        
    //	}  // closing brace of newGame method above

  3. #3
    silverq_82 is offline Member
    Join Date
    Aug 2007
    Posts
    8
    Rep Power
    0

    Default

    Hi, thanks for the reply. I've come across another problem. For this part of the code, is there any way that i can remove the dialog box. I just want to remove the JOption.showInputDialog box and create a simple output without the dialog box. Thanks in advance...

    Java Code:
    import javax.swing.JOptionPane;
    
    public class GameInterface {
    
        // create a main() method that start the game
        // while not reach the max trial and not win the game
        //	    get input
        //	    process input
        //	    display result
        // display final result
        public static void main(String[] ags) {
    	MasterMind mind = new MasterMind();
    	String input;
    	boolean finish = false;
    	System.out.println("answer is " + mind.get_answer());
    	while (!finish && !mind.complete()){
    	    do{
    		input = JOptionPane.showInputDialog("Guess?(size is "+mind.get_answer().length()+")");
    	    }while(!mind.check_input(input));
    	    finish = !mind.guess(input);
    	    System.out.println("your input is "+input);
    	    System.out.println("you have "+mind.get_all_correct()+" in correct position");
    	    System.out.println("you have "+mind.get_partial_correct()+ " in wrong position");
    	}
    	if (mind.complete()){
    	    System.out.println("Congratulation.");
    	}else{
    	    System.out.println("Game Over! Please try again");
    	    System.out.println("The answer is " + mind.get_answer());
    	}
        }
    }
    Last edited by silverq_82; 08-08-2007 at 10:58 AM.

  4. #4
    levent Guest

    Default

    You can obtain user input as follows if you want:

    Java Code:
    BufferedReader console = new BufferedReader (new InputStreamReader(System.in));
    String input;
    
    input = console.readLine();

  5. #5
    silverq_82 is offline Member
    Join Date
    Aug 2007
    Posts
    8
    Rep Power
    0

    Default

    Hey, thanks for the guidance... Btw, i have another question. ;) I have these set of codes and i want to compare the random generated set of 5 numbers with user's input of 5 numbers. Not sure how to start. I'm stuck after prompting user to key in the 5 numbers. Below are the codes:-

    Java Code:
    private static void newGame() throws IOException
    	{
    		//Random generator
        	Random gen = new Random();
        	int num = gen.nextInt(9) + 1;
        	   	
    		//Generate 5 non-duplicate numbers within an array
    	   	int mixcount=1;
    		int setcount=5;					
    		int[] numbers = new int[5];		//declare and create array
    
    			Random mix = new Random();
    			for(int i = 0 ; i< numbers.length; i++)
    		{
    		boolean diff = false;		//generate non-duplicate numbers
    
    			while (!diff)
    			{
    				numbers[i] = mix.nextInt(9);
    				diff=true;
    				for (int j=0 ; j < i ; j++)
    					{
    						if (numbers[i]==numbers[j])
    						{
    							diff = false;
    							break;
    						}
    					}
    			}
    			System.out.print(numbers[i]+" "); //remove this after all is complete 
    		}
    		
    		InputStreamReader isr = new InputStreamReader( System.in );		//Create an InputStreamReader using the standard input stream
        	BufferedReader stdin = new BufferedReader( isr );				//Create a BufferedReader using the InputStreamReader created
        		System.out.print( "Enter 5 numbers (1 to 9): " );
        		String input = stdin.readLine();							// Read a line of text from the user

  6. #6
    levent Guest

    Default

    silverq_82, at least you can try to develop the game logic. As far as i see, you already collected the code segments needed. I think you should try combining them all yourself at least at this stage if you want to learn Java, programming .. whatever the course objective is.

    Good luck.

  7. #7
    silverq_82 is offline Member
    Join Date
    Aug 2007
    Posts
    8
    Rep Power
    0

    Default

    erm... i tried a few ways of getting the codes to work but keep getting errors. Not sure if you could help me a little? Thanks. If you don't mind...

    P.S. The part where i need to compare with user's input & random generated number.

  8. #8
    levent Guest

    Default

    Keep the random numbers in an array. Keep user input in another array. Write a loop to compare each element from first array to the second one. If the purpose of the game is to print number of mismatches between these two lists, then define an integer variable and count the number of mismatches with this variable inside your loop.

  9. #9
    silverq_82 is offline Member
    Join Date
    Aug 2007
    Posts
    8
    Rep Power
    0

    Default

    Hi, once again, thanks for the info.

  10. #10
    levent Guest

Similar Threads

  1. Replies: 0
    Last Post: 04-02-2008, 07:04 AM
  2. class or interface expected -compile err
    By ravi503 in forum Java Servlet
    Replies: 1
    Last Post: 03-26-2008, 11:45 AM
  3. Illegal Start of an Expression
    By David55 in forum CLDC and MIDP
    Replies: 8
    Last Post: 11-02-2007, 09:11 PM
  4. Illegal start of expression
    By gabriel in forum New To Java
    Replies: 2
    Last Post: 08-01-2007, 05:09 PM
  5. Illegal Start of an Expression
    By David55 in forum CLDC and MIDP
    Replies: 0
    Last Post: 04-20-2007, 05:59 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •