Close

Results 1 to 9 of 9

Thread: Java Code Help

  1. #1
    DF Probation Ryan5262's Avatar
    Join Date
    Nov 2002
    Location
    Scotland
    Posts
    680
    Thanks
    0
    Thanked:        0
    Karma Level
    306

    Default Java Code Help

    Being asked to wright a java program for Uni guess the Random Number Game getting this Error Message
    H:\Java\RandomGame.java:24: cannot Resolve symbol
    symbol : method parseInt (int)
    location:22: class java.lang.Integer
    Dont get to see ma lectuer till next week and its due friday so could and one shed some light would be great

    Heres the program code
    PHP Code:
    import javax.swing.JOptionPane;
    public class 
    RandomGame {
    public static 
    boolean checkGuess(int userGuess int RandomNumber)  {
        
    boolean match false;
        if (
    userGuess == RandomNumber)
        
    match true;
        return 
    match;
            }    
    //end Boolean    
    public static void main (String args[]) {
        
    String userNumber;
        
    int RandomNumber,userGuess;
        final 
    int ARRAY_SIZE 3;
        
    int [] myArray = new int [ARRAY_SIZE];
        
    String output="";
        
    boolean match;
        
        
    // generate a random number between 1 and 10
                
    RandomNumber = (int) (Math.random() * 9) + 1;              
                                          
                                                                     
        for (
    int x 0myArray.lengthx++) {
          
    // read string, convert to int and store in array
          
    userNumber JOptionPane.showInputDialog("please enter a number");
              
    userNumber Integer.parseInt(userGuess);
                                   
            
                    
    // You have Won Output
                        
    match checkGuess(userGuess,RandomNumber);
                      if (
    match == true) {
                          
    // construct output 
                      
    output "You Have Won! "+RandomNumber;
                            break;
                      }
    //End If 
                      
    else{
                      
    // prompt user for Guess
                      
    userNumber JOptionPane.showInputDialog("Please enter another Guess");
                                          
    match checkGuess(userGuess,RandomNumber);
                      if (
    match == true) {
                          
    // construct output 
                      
    output "You Have Won! "+RandomNumber;
                            break;
                            }
                 }
    // End Else
          
    }    //End For
          
          
          
        // print message
                   
    JOptionPane.showMessageDialog(nulloutput"Win"JOptionPane.INFORMATION_MESSAGE);
        
            
    System.exit(0);
        }
    //End Main


    [FONT=Arial Black]Life is a waste of time, Time is a waste of life so get wasted all the time and have the time of your life!:biggrin: [/FONT]

  2. #2
    DF VIP Member blacksheep's Avatar
    Join Date
    Jun 2006
    Location
    Manchester
    Posts
    3,877
    Thanks
    87
    Thanked:        265
    Karma Level
    546

    Default Re: Java Code Help

    if you want to put the integer that is in userGuess into the String that is userNumber the easy way that I'd do it would be userNumber = "" + userGuess

    its there that it seems to be stalling (line 24) as it doesn't seem to recognise the method parseInt also the parseInt method expects a string not an int like you're passing it - which may be why it doesn't recognise it.

  3. #3
    DF VIP Member salvadorescobar's Avatar
    Join Date
    Dec 2000
    Location
    Huddersfield
    Posts
    951
    Thanks
    0
    Thanked:        0
    Karma Level
    346

    Default Re: Java Code Help

    I haven't read through your code in much detail (just the first few lines and line 24) and the problem you are seeing is a compiler error that has been reported due to the fact that during compilation the compiler hasn't been able to find the method Integer.parseInt anywhere within the hierarchy of your imports.

    There could be one of two issues here. Either you need to include Integer on your imports (by adding import java.lang.Integer in your top section); or Integer.parseInt(int) does not exist as a method within the java.lang.Integer class.

    Looking at the API (http://java.sun.com/j2se/1.4.2/docs/...g/Integer.html) I can see that Integer.parseInt accepts either a string or a string and int parameter. There is not method that accepts only an int. This is after all a method for parsing a string into an integer representation so this kind of makes sense.

    I think that line 24 should read:

    PHP Code:
    userGuess Integer.parseInt(userNumber); 
    Instead of

    PHP Code:
    userNumber Integer.parseInt(userGuess); 
    And you have just made a mistake in selecting the correct variables from your code.

    This has probably happened due to bad variable naming on your part.

    Redefining userNumber as an int and userGuess as a string would make things clearer.

    Sal.
    'If we aren't meant to eat animals, then why are they made out of meat?'
    Anon.

  4. #4
    DF VIP Member blacksheep's Avatar
    Join Date
    Jun 2006
    Location
    Manchester
    Posts
    3,877
    Thanks
    87
    Thanked:        265
    Karma Level
    546

    Default Re: Java Code Help

    you don't have to import/include Integer - its included as standard.

  5. #5
    DF Probation Ryan5262's Avatar
    Join Date
    Nov 2002
    Location
    Scotland
    Posts
    680
    Thanks
    0
    Thanked:        0
    Karma Level
    306

    Default Re: Java Code Help

    Thanks for the help !!!! Could you help me with this last bit ? i cant do java! just confuses me. just due in tomorrow and ive got no clue what to do and i dont see my lectuer till next week. dont want to fail i was a late start to the college and misees a few weeks due to work issues.

    Add a further method to your program which gives the user a hint as to whether the guess they made is close to the correct answer. The method signature is given below. The method should print "cold" if the users guess is 3 or more away from the correct answer. It should print "warm" if the guess if 2 away from the correct answer. It should print "hot" is the guess is only 1 away than the correct answer. Modify your program from (1) so that this method is called after every attempt by the user to guess the number.
    public static String howClose(int userGuess, int randomNumber)

    Heres what ive done so far with it where would i add the message ? and how would i fix the howClose Class?

    PHP Code:
        return match;
            }    
    //end Boolean    
            
    public static String howClose(int userGuessint RandomNumber){
            for (
    userGuess || RandomNumber 2){
                 
    cold true;
                    return 
    cold;
                
                    if (
    userGuess || RandomNumber 2
                        
    warm true;
                        return 
    warm;
                    }
                        else {(
    userGuess || RandomNumber 1
                        
    hot true;
                        return 
    hot;
                        }
            }


        
    public static 
    void main (String args[]) {
        
    String userGuess;
        
    int RandomNumber,userNumber;
        
    String output="";
        
    boolean match;
        
    boolean hot,cold,warm;
            

        
    // generate a random number between 1 and 10
                
    RandomNumber = (int) (Math.random() * 9) + 1;                              
            
                   
        
    // Progam to run 3 Times before Exit
            
    for (int counter 0counter <= 3counter++) {
              if (
    counter == 3) {
                          
                    
    // If User is wrong 3 times then they are lost
                   
    output ="You have Lost Try Again The Random Number Was  "RandomNumber;
      
        
                
    // print message
                   
    JOptionPane.showMessageDialog(nulloutput"Lost"JOptionPane.INFORMATION_MESSAGE);       
                   
                    }
    //End If   
                
    else{                 
                        
    // prompt user for Guess
                      
    userGuess JOptionPane.showInputDialog("Please enter a Guess");    
                   
                
    // convert string to integer
                    
    userNumber Integer.parseInt(userGuess);
                
                    
    // You have Won Output
                      
    match checkGuess(userNumber,RandomNumber);
                      if (
    match == true) {
                          
    // construct output 
                      
    output "You Have Won! "+RandomNumber;
                            break;
                      }
    //End If 
                 
    }// End Else
          
    }    //End For
           
        // print message
                   
    JOptionPane.showMessageDialog(nulloutput"Win"JOptionPane.INFORMATION_MESSAGE);
        
            
    System.exit(0);
        }
    //End Main

    Last edited by Ryan5262; 22nd November 2006 at 04:08 PM.
    [FONT=Arial Black]Life is a waste of time, Time is a waste of life so get wasted all the time and have the time of your life!:biggrin: [/FONT]

  6. #6
    DF VIP Member blacksheep's Avatar
    Join Date
    Jun 2006
    Location
    Manchester
    Posts
    3,877
    Thanks
    87
    Thanked:        265
    Karma Level
    546

    Default Re: Java Code Help

    ok the how close class I'd get (value to guess - the guessed value) and make sure thats positive, either by squaring and square rooting or seeing if its less than 0 and multiplying by -1 then put that in a switch statement

    so you'd do something like

    Code:
    int difference = (guessValue - randomNumber);
    if (difference < 0)
    difference = difference * -1;
    switch(difference)
    {  
      case(1)
      Systme.out.println("hot");
      break;
    
      case(2)
      System.out.println("warm")
      break;
    
      default
      system.out.println("cold")
      break;
    }
    obviously that assumes you don't go into this with a correct guess.

  7. #7
    DF VIP Member
    canardo's Avatar
    Join Date
    Jul 2001
    Location
    uk
    Posts
    840
    Thanks
    0
    Thanked:        0
    Karma Level
    321

    Default Re: Java Code Help

    Ryan5262 as a programmer and I am sure salvadorescobar will agree as he helped me when I was at uni, one word of advice start your assignments the night before they are due in and then ask for help, as if you dont learn these fundamentals you will never pass your final year!

  8. #8
    DF Probation Ryan5262's Avatar
    Join Date
    Nov 2002
    Location
    Scotland
    Posts
    680
    Thanks
    0
    Thanked:        0
    Karma Level
    306

    Default Re: Java Code Help

    Quote Originally Posted by canardo View Post
    Ryan5262 as a programmer and I am sure salvadorescobar will agree as he helped me when I was at uni, one word of advice start your assignments the night before they are due in and then ask for help, as if you dont learn these fundamentals you will never pass your final year!

    as i sayed above i was a late start to college i started two weeks ago and had to catch up on 5 weeks worth of work and try and meet all dead lines so was a bit of a struggle thats why i asked for help and i am so greatfull for it!!! thanks every one see how i get on today with it
    [FONT=Arial Black]Life is a waste of time, Time is a waste of life so get wasted all the time and have the time of your life!:biggrin: [/FONT]

  9. #9
    DF Probation Ryan5262's Avatar
    Join Date
    Nov 2002
    Location
    Scotland
    Posts
    680
    Thanks
    0
    Thanked:        0
    Karma Level
    306

    Default Re: Java Code Help

    Got it up and going working a treat !!! thanks for the help you saved me so much there !!!!


    Thanks Again!!!
    [FONT=Arial Black]Life is a waste of time, Time is a waste of life so get wasted all the time and have the time of your life!:biggrin: [/FONT]

Similar Threads

  1. Code for Blaupunkt Car 300 Radio?
    By fais in forum Radio Decoding
    Replies: 8
    Last Post: 30th March 2004, 03:38 PM
  2. Java Idea?
    By Roo in forum Unlocking Questions & Solutions
    Replies: 0
    Last Post: 8th September 2002, 06:55 PM
  3. sony z5:security code
    By Danger Mouse in forum Unlocking Questions & Solutions
    Replies: 1
    Last Post: 6th September 2002, 05:41 PM
  4. Eurotunnel cheap fare or discount code - priviledge member?
    By shedsounds in forum Cheapskates Corner
    Replies: 2
    Last Post: 5th September 2002, 10:21 AM

Social Networking Bookmarks

Posting Permissions

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