Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 08-08-2007, 08:14 AM
Member
 
Join Date: Aug 2007
Posts: 8
silverq_82 is on a distinguished road
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!

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); } }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 08-08-2007, 10:16 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,191
hardwired is on a distinguished road
Learning to format your code will help avoid/detect this kind of thing.
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
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 08-08-2007, 11:40 AM
Member
 
Join Date: Aug 2007
Posts: 8
silverq_82 is on a distinguished road
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...

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 11:58 AM.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 08-08-2007, 03:42 PM
Senior Member
 
Join Date: Dec 2006
Posts: 748
levent is on a distinguished road
You can obtain user input as follows if you want:

Code:
BufferedReader console = new BufferedReader (new InputStreamReader(System.in)); String input; input = console.readLine();
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 08-08-2007, 06:04 PM
Member
 
Join Date: Aug 2007
Posts: 8
silverq_82 is on a distinguished road
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:-

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
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 08-08-2007, 06:10 PM
Senior Member
 
Join Date: Dec 2006
Posts: 748
levent is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 08-08-2007, 06:26 PM
Member
 
Join Date: Aug 2007
Posts: 8
silverq_82 is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 08-08-2007, 06:36 PM
Senior Member
 
Join Date: Dec 2006
Posts: 748
levent is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 08-08-2007, 07:05 PM
Member
 
Join Date: Aug 2007
Posts: 8
silverq_82 is on a distinguished road
Hi, once again, thanks for the info.
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 08-08-2007, 08:16 PM
Senior Member
 
Join Date: Dec 2006
Posts: 748
levent is on a distinguished road
No problem. Let us know if you cant solve an error..
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
what is the Priority for execution of Interface class and a Abstract class Santoshbk Advanced Java 0 04-02-2008 08:04 AM
class or interface expected -compile err ravi503 Java Servlet 1 03-26-2008 12:45 PM
Illegal Start of an Expression David55 CLDC and MIDP 8 11-02-2007 10:11 PM
Illegal start of expression gabriel New To Java 2 08-01-2007 06:09 PM
Illegal Start of an Expression David55 CLDC and MIDP 0 04-20-2007 06:59 AM


All times are GMT +3. The time now is 10:44 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org