Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 13 of 13

Thread: Can someone help me with this code asap

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    11
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Can someone help me with this code asap

    Can someone please help me fix this program. its missing the closing screen but i didnt want to put it in since i was getting so many errors. It doesn't compile and it doesn't accomplish what its suppose to do, please help.

    Task

    1. Present Opening Screen
    2. While user wants to play, Get user's guess
    3. Generate random number
    4. Compare guess with computer's number
    5. Add 1 to counter if correct
    6. Add 1 to games played in any case
    7. Display prompt to continue.
    8. If yes, then get number from user
    9. If no, display results screen.
    Methods Needed
    1. Opening screen
    2. Get user input for numbers
    3. Generate random number
    4. Get user response to continue
    5. Display results
    1. Random Numbers
    2. Methods that return a value
    3. Create a new Die class

    import javax.swing.*;
     
    public class Lab2c 
    {
     
    static int wins = 0;
    static int losses = 0;
    static int totalPlays = 0;
     
     
    public static void main(String args[])
    {
    runIt();
    System.exit(0);
    }
     
    public static void runIt ()
    {
    int userNum;
    int compNum;
    int value;
     
    openingMessage();
     
    do
    {
    userNum = getInt();
     
    compNum = rollTheDice();
     
    CompareNum (userNum, compNum);
     
    value = JOptionPane.showConfirmDialog(null, "Continue with the game?");
     
    } while (value == JOptionPane.YES_OPTION); // do while
     
    }
    public static void endingMessage()
    {
    String message1;
    String message2;
    String finalmessage;
     
    message1 = "Thanks for playing";
    message2 = "You have won" + wins + "You Played" + "games";
    }
    public static void compareNum (int user, int comp)
    {
     
    if(user == comp)
    {
    s = JOptionPane.showMessageDialog (null, "You Win");
    wins++;
    totalPlays++;
    System.out.println ("Wins:" + wins);
     
    }
    else
    {	s = JOptionPane.showMessageDialog (null, "You Lost");
    totalPlays++;
     
    }	
    }
    public static void openingMessage()
    {
    String openingMessage = "";
     
    openingMessage = "Welcome to the Guess the Numbers from 1 to 6 Program \n\n" // change my Message to
    + "The computer will generate a random number from 1 to 6. \n" // your Message for your program.
    + "Your task is to see if you can guess the number.\n \n"
    + "The game will keep track of how many games you played \n"
    + "and it will tell you how many wins you had \n \n"
    + "\tClick OK to Begin.\n\n\tGood Luck \n";
     
    JTextArea outputArea = new JTextArea(15,30); // create an Instance of a JTextArea - in this case the variable name is outputArea
     
    outputArea.setText ( openingMessage ); // put the Text String into the outputArea
     
    JOptionPane.showMessageDialog ( null, outputArea, // Display the Message
    "Opening Screen for Game 1 to 6", // Create a suitable Title for the Window
    JOptionPane.INFORMATION_MESSAGE );
    }
    public static void dice()
    {
    int Total;
    String output = "";
     
    //collect from user guess of 
    //die total with method guesTheDice(make if statement for not to break if val<= 1 && <=6
    Total = guessTheDice();
     
    int rolledTotal;
     
    //roll the dice in method 
    rolledTotal = rollTheDice();
     
    if(Total == rolledTotal) 
    output = "You guessed right!" +
    " The dice total is " + Total;
    else
    output = "Sorry. The dice total is: " 
    +rolledTotal;
    JOptionPane.showMessageDialog(
    null, output);
     
    System.exit(0);
    }//end main
     
    public static int guessTheDice ()
    {
    int dice1; //first value of die1
     
    int total;
     
    String firstDice;
     
    fir
    }
    {
    int x;
    x = getInt (); 
     
    System.out.println ("You entered " + x + ".");
    } // main
    public static int getInt()
    {
    int val;
     
    while (true) // a seemingly endless loop - uses break to get out 7
    {
     
    String s = JOptionPane.showInputDialog ( "Enter a number from 1 - 6:" );
     
    if (s == null)
    {
    JOptionPane.showMessageDialog(null, "Leaving program"); // exit on cancel
    System.exit (0);
    } // cancel if
    else
    if (s.equals(""))
    {
    JOptionPane.showMessageDialog
    (null, "You must make an entry in the InputBox");
     
    } // entered nothing
    try 8
    {
    val = Integer.parseInt(s);
    if (val = 0)
    break; // exit loop with valid in
    // could have had a variable change here to get out of loop


  2. #2
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Can someone help me with this code asap

    What are the errors? Copy, Paste the errors here, so that we could look over them.

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Can someone help me with this code asap

    One big problem with your code is that it is not indented properly making it very hard to read and understand. The code inside of every {} pair should be indented at least 3 spaces. Look at the many examples posted on this forum.

  4. #4
    Junior Member
    Join Date
    Oct 2011
    Posts
    11
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Can someone help me with this code asap

    import javax.swing.*;
     
    public class Lab2c
    {
     
        static int wins = 0;
        static int losses = 0;
        static int totalPlays = 0;
     
     
        public static void main(String args[])
    	{
    		runIt();
    		System.exit(0);
    	}
     
    	public static void runIt ()
    	{
    		int userNum;
    		int compNum;
    		int value;
     
    		openingMessage();
     
    		do
    		{
    		userNum = getInt();
     
    		compNum = rollTheDice();
     
    		CompareNum (userNum, compNum);
     
    		value = JOptionPane.showConfirmDialog(null, "Continue with the game?");
     
          }  while (value == JOptionPane.YES_OPTION);  // do while
     
    	}
    	public static void endingMessage()
    	{
    		String message1;
    		String message2;
    		String finalmessage;
     
    			message1 = "Thanks for playing";
    			message2 = "You have won" + wins + "You Played" + "games";
    	}
    	public static void compareNum (int user, int comp)
    	{
     
    		if(user == comp)
    		{
    			s = JOptionPane.showMessageDialog (null, "You Win");
    			wins++;
    			totalPlays++;
    			System.out.println ("Wins:" + wins);
     
    		}
    		else
    	     {	s = JOptionPane.showMessageDialog (null, "You Lost");
    			totalPlays++;
     
    		}
    	}
    	public static void openingMessage()
    	{
    			String  openingMessage = "";
     
    		  openingMessage = "Welcome to the Guess the Numbers from 1 to 6 Program \n\n"   // change my Message to
    	                       + "The computer will generate a random number from 1 to 6. \n" // your Message for your program.
    	                       + "Your task is to see if you can guess the number.\n \n"
    	                       + "The game will keep track of how many games you played \n"
    	                       + "and it will tell you how many wins you had \n \n"
    	                       + "\tClick OK to Begin.\n\n\tGood Luck \n";
     
             JTextArea outputArea = new JTextArea(15,30);  // create an Instance of a JTextArea - in this case the variable name is outputArea
     
    	     outputArea.setText ( openingMessage );  // put the Text String into the outputArea
     
    	     JOptionPane.showMessageDialog ( null, outputArea,  // Display the Message
                 "Opening Screen for Game 1 to 6",           // Create a suitable Title for the Window
    		       JOptionPane.INFORMATION_MESSAGE );
    	}
     
    	public static int getInt(String[] args)
    	{
    		int x;
    		x = getInt ();
     
     
    		System.out.println ("You entered " + x + ".");
    	} // main
     
       {
          int val;
     
          while (true)  // a seemingly endless loop - uses break to get out
          { // loop until we get a valid int
     
            String s = JOptionPane.showInputDialog ( "Enter a whole number:" );
     
            if (s == null)
            {
          JOptionPane.showMessageDialog(null, "Leaving program");  // exit on cancel
                  System.exit (0);
        } // cancel if
        else
              if (s.equals(""))
          {
        JOptionPane.showMessageDialog
         (null, "You must make an entry in the InputBox");
     
          } // entered nothing
     
            try
            {
                val = Integer.parseInt(s);
     
                break;  // exit loop with valid int
                // could have had a variable change here to get out of loop
     
            }// try
            catch (Exception e)
            {
                JOptionPane.showMessageDialog(null, "Enter a whole number");
     
            }// catch
        while (number <= times)
          {
             xFig();
             System.out.println();
             number = number++1;
          }
          } // while
     
             return val;  //return the number to the calling method
     
      }  //end getInt
    	public static void dice()
    		{
    		int Total;
    		String output = "";
     
    		//collect from user guess of
    		//die total with method guesTheDice(make if statement for not to break if val<= 1 && <=6
    		Total = guessTheDice();
     
    		int rolledTotal;
     
    		//roll the dice in method
    		rolledTotal = rollTheDice();
     
    		if(Total == rolledTotal)
    			output = "You guessed right!" +
    			"  The dice total is " + Total;
    		else
    			output = "Sorry.  The dice total is: "
    			+rolledTotal;
    		JOptionPane.showMessageDialog(
    			null, output);
     
    		System.exit(0);
    	}//end main
     
    	public static int guessTheDice ()
    	{
    		int dice1; //first value of die1
     
    		int total;
     
    		String firstDice;
     
    		firstDice=JOptionPane.showInputDialog
    			("Guess die 1 (1-6): ");
     
    		dice1=Integer.parseInt(firstDice);
     
    		total=dice1;
     
    		return total;
    	}//end guessTheDice method
     
    	public static int rollTheDice()
    	{
    		int x;
     
    		//generating random numbers
    		//between 1-6 with
    		//Math.random
    		x=1 + (int) (Math.random( ) *6);
     
    		return x;
    	}//end rollTheDice method
    }// try val = integer parseInt
     
     	public static int getInt(String[] args)
    {
    int x;
    x = getInt ();
     
     
    System.out.println ("You entered " + x + ".");
    } // main
     
     
       {
          int val;
     
          while (true)  // a seemingly endless loop - uses break to get out
          { // loop until we get a valid int
     
            String s = JOptionPane.showInputDialog ( "Enter a whole number:" );
     
            if (s == null)
            {
          JOptionPane.showMessageDialog(null, "Leaving program");  // exit on cancel
                  System.exit (0);
        } // cancel if
        else
              if (s.equals(""))
          {
        JOptionPane.showMessageDialog
         (null, "You must make an entry in the InputBox");
     
          } // entered nothing
     
            try
            {
                val = Integer.parseInt(s);
     
                break;  // exit loop with valid int
                // could have had a variable change here to get out of loop
     
            }// try
            catch (Exception e)
            {
                JOptionPane.showMessageDialog(null, "Enter a whole number");
     
            }// catch
        while (number <= times)
          {
             xFig();
             System.out.println();
             number = number++1;
     
          } // while
     
             return val;  //return the number to the calling method
          }
      }  //end getInt

    (Im getting 17 missing class,interface,enum errors. im very new to java and just kinda goin along with what the teacher says but i cant get help from him for about 2 weeks.)

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Can someone help me with this code asap

    Please post the full text of the error messages.

  6. #6
    Junior Member
    Join Date
    Oct 2011
    Posts
    11
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Can someone help me with this code asap

    --------------------Configuration: <Default>--------------------
    I:\Lab2c.java:142: ';' expected
    number = number++1;
    ^
    I:\Lab2c.java:206: class, interface, or enum expected
    public static int getInt(String[] args)
    ^
    I:\Lab2c.java:209: class, interface, or enum expected
    x = getInt ();
    ^
    I:\Lab2c.java:212: class, interface, or enum expected
    System.out.println ("You entered " + x + ".");
    ^
    I:\Lab2c.java:213: class, interface, or enum expected
    } // main
    ^
    I:\Lab2c.java:219: class, interface, or enum expected

    while (true) // a seemingly endless loop - uses break to get out
    ^
    I:\Lab2c.java:224: class, interface, or enum expected
    if (s == null)
    ^
    I:\Lab2c.java:227: class, interface, or enum expected
    System.exit (0);
    ^
    I:\Lab2c.java:228: class, interface, or enum expected
    } // cancel if
    ^
    I:\Lab2c.java:235: class, interface, or enum expected
    } // entered nothing
    ^
    I:\Lab2c.java:241: class, interface, or enum expected
    break; // exit loop with valid int
    ^
    I:\Lab2c.java:244: class, interface, or enum expected
    }// try
    ^
    I:\Lab2c.java:249: class, interface, or enum expected
    }// catch
    ^
    I:\Lab2c.java:253: class, interface, or enum expected
    System.out.println();
    ^
    I:\Lab2c.java:254: class, interface, or enum expected
    number = number++1;
    ^
    I:\Lab2c.java:256: class, interface, or enum expected
    } // while
    ^
    I:\Lab2c.java:259: class, interface, or enum expected
    }
    ^
    17 errors

    Process completed.

  7. #7
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Can someone help me with this code asap

    number = number++1;

    That is not legal syntax. If you want to increment the variable by 1 then simply use number++;

    In future do not mark your problem ASAP. You are no more important than anyone else asking for help. You will or will not get help depending upon the willingness of the volunteers coming to the forum. In my case I am less likely to help people who say ASAP or URGENT thus reducing your chances of getting help not increasing it.
    Improving the world one idiot at a time!

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Can someone help me with this code asap

    class, interface, or enum expected
    Check that all {s have a paired } before line 206
    Look at each method and check that its beginning { has an ending }

    It helps a lot if you align the source line with the beginning { vertically with the ending }

    Also if you label each ending } with what it is ending like the name of the method or class or if or while.

    Another approach is to comment out sections of your code by using /* and */ to see where this error is.
    For example comment out methods.

  9. #9
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Can someone help me with this code asap

    Well, these all 17 errors are mostly related to non-positioned closing brace '}'
    Look carefully into your code, either use some editor(Eclipse, IntelliJIDea, NetBeans, JetBrains etc) as Editors guide you with color markers of starting and ending braces.

  10. #10
    Junior Member
    Join Date
    Oct 2011
    Posts
    11
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Can someone help me with this code asap

    Thank you very much everyone for your help so far =), following your advice, i have 1 error left and its."Reached end without parsing" does anyone think they can help me find the missing brace? havin a bit of trobule.



    import javax.swing.*;
     
    public class Lab2c
    {
     
        static int wins = 0;
        static int losses = 0;
        static int totalPlays = 0;
     
     
        public static void main(String args[])
    	{
    		runIt();
    		System.exit(0);
    	}
     
    	public static void runIt ()
    	{
    		int userNum;
    		int compNum;
    		int value;
     
    		openingMessage();
     
    		do
    		{
    		userNum = getInt();
     
    		compNum = rollTheDice();
     
    		CompareNum (userNum, compNum);
     
    		value = JOptionPane.showConfirmDialog(null, "Continue with the game?");
     
          }  while (value == JOptionPane.YES_OPTION);  // do while
     
    	}
    	public static void endingMessage()
    	{
    		String message1;
    		String message2;
    		String finalmessage;
     
    			message1 = "Thanks for playing";
    			message2 = "You have won" + wins + "You Played" + "games";
    	}
    	public static void compareNum (int user, int comp)
    	{
     
    		if(user == comp)
    		{
    			s = JOptionPane.showMessageDialog (null, "You Win");
    			wins++;
    			totalPlays++;
    			System.out.println ("Wins:" + wins);
     
    		}
    		else
    	     {	s = JOptionPane.showMessageDialog (null, "You Lost");
    			totalPlays++;
     
    		}
    	}
    	public static void openingMessage()
    	{
    			String  openingMessage = "";
     
    		  openingMessage = "Welcome to the Guess the Numbers from 1 to 6 Program \n\n"   // change my Message to
    	                       + "The computer will generate a random number from 1 to 6. \n" // your Message for your program.
    	                       + "Your task is to see if you can guess the number.\n \n"
    	                       + "The game will keep track of how many games you played \n"
    	                       + "and it will tell you how many wins you had \n \n"
    	                       + "\tClick OK to Begin.\n\n\tGood Luck \n";
     
             JTextArea outputArea = new JTextArea(15,30);  // create an Instance of a JTextArea - in this case the variable name is outputArea
     
    	     outputArea.setText ( openingMessage );  // put the Text String into the outputArea
     
    	     JOptionPane.showMessageDialog ( null, outputArea,  // Display the Message
                 "Opening Screen for Game 1 to 6",           // Create a suitable Title for the Window
    		       JOptionPane.INFORMATION_MESSAGE );
    	}
     
    	public static int getInt(String[] args)
    	{
    		int x;
    		x = getInt ();
     
     
    		System.out.println ("You entered " + x + ".");
    	} // main
     
       {
          int val;
     
          while (true)  // a seemingly endless loop - uses break to get out
          { // loop until we get a valid int
     
            String s = JOptionPane.showInputDialog ( "Enter a whole number:" );
     
            if (s == null)
            {
          JOptionPane.showMessageDialog(null, "Leaving program");  // exit on cancel
                  System.exit (0);
        } // cancel if
        else
              if (s.equals(""))
          {
        JOptionPane.showMessageDialog
         (null, "You must make an entry in the InputBox");
     
          } // entered nothing
     
            try
            {
                val = Integer.parseInt(s);
     
                break;  // exit loop with valid int
                // could have had a variable change here to get out of loop
     
            }// try
            catch (Exception e)
            {
                JOptionPane.showMessageDialog(null, "Enter a whole number");
     
            }// catch
        while (number <= times)
          {
             xFig();
             System.out.println();
             number = number++;
          }
          } // while
     
             return val;  //return the number to the calling method
     
      }  //end getInt
    	public static void dice()
    		{
    		int Total;
    		String output = "";
     
    		//collect from user guess of
    		//die total with method guesTheDice(make if statement for not to break if val<= 1 && <=6
    		Total = guessTheDice();
     
    		int rolledTotal;
     
    		//roll the dice in method
    		rolledTotal = rollTheDice();
     
    		if(Total == rolledTotal)
    			output = "You guessed right!" +
    			"  The dice total is " + Total;
    		else
    			output = "Sorry.  The dice total is: "
    			+rolledTotal;
    		JOptionPane.showMessageDialog(
    			null, output);
     
    		System.exit(0);
    	}//end main
     
    	public static int guessTheDice ()
    	{
    		int dice1; //first value of die1
     
    		int total;
     
    		String firstDice;
     
    		firstDice=JOptionPane.showInputDialog
    			("Guess die 1 (1-6): ");
     
    		dice1=Integer.parseInt(firstDice);
     
    		total=dice1;
     
    		return total;
    	}//end guessTheDice method
     
    	public static int rollTheDice()
    	{
    		int x;
     
    		//generating random numbers
    		//between 1-6 with
    		//Math.random
    		x=1 + (int) (Math.random( ) *6);
     
    		return x;
    	}//end rollTheDice method
    // try val = integer parseInt
     
     	public static int getInt(String[] args)
    {
    int x;
    x = getInt ();
     
     
    System.out.println ("You entered " + x + ".");
    } // main
     
     
       {
          int val;
     
          while (true)  // a seemingly endless loop - uses break to get out
          { // loop until we get a valid int
     
            String s = JOptionPane.showInputDialog ( "Enter a whole number:" );
     
            if (s == null)
            {
          JOptionPane.showMessageDialog(null, "Leaving program");  // exit on cancel
                  System.exit (0);
        } // cancel if
        else
              if (s.equals(""))
          {
        JOptionPane.showMessageDialog
         (null, "You must make an entry in the InputBox");
     
          } // entered nothing
     
            try
            {
                val = Integer.parseInt(s);
     
                break;  // exit loop with valid int
                // could have had a variable change here to get out of loop
     
            }// try
            catch (Exception e)
            {
                JOptionPane.showMessageDialog(null, "Enter a whole number");
     
            }// catch
        while (number <= times)
          {
             xFig();
             System.out.println();
             number = number++;
     
          } // while
          {
             return val;  //return the number to the calling method
          }
          }
      }  //end getInt

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Can someone help me with this code asap

    find the missing brace?
    Many IDEs editors have a menu item to help you find the paired {}s. Place your curosor on one { press a key combination and the cursor is moved to the pairing }. On my editor I press: CTL + ]

    Otherwise print out the source file and use a pen. Circle a { and draw a line to its pairing }.

    Start at an inner most pair of {} and work out to the outer most pair.

  12. #12
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Can someone help me with this code asap

    Well, your code doesn't only have one error but actually alot of errors, although it shows only one error for now, but try removing the brace as i can't figure out your code actually, it's too bad. I have looked over the code and found it difficult to manage. Atleast, no one will spend hours to remove this brace for you. So, use any IDE to figure this out by yourself as it's your code and no one can understand it better than you.
    Your code also has problems of duplicate functions.

  13. #13
    Junior Member
    Join Date
    Nov 2011
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Can someone help me with this code asap

    Just go through each block systematically, shouldn't be to hard. I'm not one to talk though =].

Similar Threads

  1. Need some help with this code - Asap
    By Th3T3chGuy in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 10th, 2011, 07:17 AM
  2. Need help ASAP!!!!
    By Swiper in forum What's Wrong With My Code?
    Replies: 9
    Last Post: August 12th, 2011, 06:41 PM
  3. NEED HELP ASAP!!
    By JavaStudent_09 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: August 18th, 2010, 07:33 PM
  4. NEED HELP ASAP PLEASE!!!
    By mbm4ever in forum What's Wrong With My Code?
    Replies: 8
    Last Post: August 12th, 2010, 07:01 PM
  5. Need a calandar app done asap
    By gixmo in forum Paid Java Projects
    Replies: 6
    Last Post: July 10th, 2010, 08:58 PM