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 8 of 8

Thread: What's wrong with my integers?

  1. #1
    Junior Member
    Join Date
    Jul 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default What's wrong with my integers?

    Hello, I just started using Java last week. And I've decided to try to make my own guessing game without looking up a tutorial on how to do so. My problem is that, it's saying my ints cannot be resolved as variables. So here is my code!

    import java.util.Scanner;
     
     
    public class StringVariables {
     
    	public static void main(String [] args) {
    		int answer1 = your_first_guess;
    		int answer2 = your_second_guess;
    		int answer3 = your_last_guess;
     
    		Scanner user_input = new Scanner(System.in);
     
    		String your_first_guess;
    		System.out.println("You have three guesses left");
    		System.out.println("Please enter your first guess!");
    		your_first_guess = user_input.next();
    			if (answer1 != Math.random()){
    				System.out.println("You are wrong!");
    			}else{
    				System.out.println("You are rigtht!");}
     
    		String your_second_guess;
    		System.out.println("You have two guesses left!");
    		System.out.println("Please enter your second guess!");
    		your_second_guess = user_input.next();
     
     
    		String your_last_guess;
    		System.out.println("You have one guess left!");
    		System.out.println("Please enter your last guess!");
    		your_last_guess = user_input.next();
     
    	}
     
    }

    Thank you for your help.

    P.S. Please don't point out any other mistakes I would like to find them myself!
    Last edited by helloworld922; July 26th, 2013 at 09:57 PM. Reason: please use [code] tags


  2. #2
    Member Zyrion's Avatar
    Join Date
    Feb 2013
    Location
    Iowa
    Posts
    106
    My Mood
    Angelic
    Thanks
    2
    Thanked 8 Times in 8 Posts

    Default Re: What's wrong with my integers?

    Your program code is initiated sequentially, line by line. So what do you think happens when your IDE runs your the first statement in the main method of the code.

    //You are declaring these integer elements and then assigning them values from variables that have not been defined/initialized.
     
    int answer1 = your_first_guess;      //Variable answer1(initalized to 0 depending on your IDE) *gets*
                                        //variable your_first_guess (No type has been defined and has not been initalized)
    int answer2 = your_second_guess;  
    int answer3 = your_last_guess;

  3. #3
    Member
    Join Date
    Jul 2013
    Location
    Franklin, TN
    Posts
    47
    Thanks
    3
    Thanked 4 Times in 4 Posts

    Default Re: What's wrong with my integers?

    Hi. Well first off they can't be resolved because you are trying to set them to a value that doesn't exist.

    You aren't creating space for a value named "your_first_guess" until you initialize it by declaring it a new type.
    Ex.. Bad
    int twoInt = oneInt;
    // blah blah
    // blah blah
    // blah blah
    int oneInt = 56;

    Ex.. Good
    int oneInt = 56;
    // blah blah
    // blah blah
    // blah blah
    int twoInt = oneInt;

    Post your progress and I will assist you every step of the way.

  4. #4
    Junior Member
    Join Date
    Jul 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: What's wrong with my integers?

    Well I don't exactly have a value for the integers. So how would I make the integers the numbers the person guesses?

    Edit: AlexHail if you wouldn't mind adding me on skype so we could talk easier. Just pm me your skype if want to, no need though.

  5. #5
    Member Zyrion's Avatar
    Join Date
    Feb 2013
    Location
    Iowa
    Posts
    106
    My Mood
    Angelic
    Thanks
    2
    Thanked 8 Times in 8 Posts

    Default Re: What's wrong with my integers?

    Quote Originally Posted by markch25 View Post
    Well I don't exactly have a value for the integers. So how would I make the integers the numbers the person guesses?
    Goal 1: I want to get the user's guess.
    Tip: Using I/O, get the users guess and store it inside an integer variable.

    Goal 2: I want to get a random number.
    Tip: Using the random class, using the method MATH.random() will generate a random number.

    Goal 3: I want to store that random number.
    Tip: Store it in the appropriate data type variable.

    Goal 4: Compare the users guess with the random number.
    Tip: Using an equality operator you can compare the two, then using a loop statement
    such as a do while loop, you can continue the game.


    Contact: JavaCottage@skype.com if you need any assistance as well.

  6. #6
    Member
    Join Date
    Jul 2013
    Location
    Franklin, TN
    Posts
    47
    Thanks
    3
    Thanked 4 Times in 4 Posts

    Default Re: What's wrong with my integers?

    Firstly you must understand the types of variables to solve this problem.

    When you are getting input from the user, this information is stored in a memory space specified as a String which holds a combination of chars. We can't simply use this String for our problem because we are using operators designed for testing values of Integers.

    So I hope by now your human logic has sensed something... In order to test the values our user input gives us, we must first convert the input from a String type to an Integer type.

    Also when using Math.random(), this method returns a double value from 0.0 to 1.0 and I don't think its what you're looking for

  7. #7
    Member Zyrion's Avatar
    Join Date
    Feb 2013
    Location
    Iowa
    Posts
    106
    My Mood
    Angelic
    Thanks
    2
    Thanked 8 Times in 8 Posts

    Default Re: What's wrong with my integers?

    Quote Originally Posted by AlexHail View Post
    ...Also when using Math.random(), this method returns a double value from 0.0 to 1.0 and I don't think its what you're looking for
    With a little bit a manipulation it's a perfect method for this problem.

    Just remember that when using Math.random() from the java.lang.Math class is that it returns a static double value. So remember when comparing or casting a double to an int precision will be lost.

    Java API:
    Returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0.
    But let's say you wanted a random integer between 1 and 100. Well since the method returns a value LESS than 1.0 you'll have to do three things. Multiple the return by 100, add by 1, and convert to int.

    int rnd = (int)(100 * Math.random() + 1);

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

    Default Re: What's wrong with my integers?

    Why not just use nextInt in the Random class?
    Improving the world one idiot at a time!

Similar Threads

  1. using integers
    By Kareem Mesbah in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 20th, 2012, 09:54 AM
  2. Non-Repeating Integers
    By Tecness2 in forum Java Theory & Questions
    Replies: 5
    Last Post: June 7th, 2012, 09:38 AM
  3. sequence of integers
    By einjhelclint12 in forum Java Theory & Questions
    Replies: 3
    Last Post: March 1st, 2012, 07:49 PM
  4. [SOLVED] Writing Integers to .txt File; Returning Random Characters Instead of Integers
    By verbicidalmaniac in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: March 8th, 2011, 09:42 PM
  5. Determine the two smallest integers from a set of user input integers
    By bpontin in forum Loops & Control Statements
    Replies: 4
    Last Post: October 17th, 2010, 06:38 PM