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: Trying to get Java to ask the user for a number

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Trying to get Java to ask the user for a number

    Basically, I'm trying to write a program that will ask the user for numbers, then plug those numbers into specific places into an equation.
    The equation will look something like this-

    =(LOG(60)/LOG(2))-(("First x the user gives"*(LOG("first x the user gives")/LOG(2))+"second x the user gives"*(LOG("second x the user gives")/LOG(2))+"third x the user gives"*LOG("third x the user gives")/LOG(2))/60).

    But another problem I run into is when I need more than just three x's. (The equation itself is log(base 2) of 60-[summation of n log n / 60].

    And problem number two is that I'm just trying to check and see if I can just ask for a single number from the user with my code, before I try getting into crazy equation coding stuff.

    And I can't even do that. :/ My code is below-

     public class PsychStat {
    	public static void main (String [] args) {
    		system.out.println ("Enter first trait value");
    		x = input;
    		system.out.println (x+1);
    		}
    		}

    I'm completely new to Java programming, and I'm trying to teach myself. Google isn't really helping much at all with my Java issues.

    Any help anyone would be willing to offer would be phenomenal!

    Thanks!!


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Trying to get Java to ask the user for a number

    Look at using the Scanner class's methods for reading input from users. Do a Search on the forum for many code samples.

  3. #3
    Junior Member
    Join Date
    Feb 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Trying to get Java to ask the user for a number

    So I tried to change around my code based on something I found off Google... so now my code looks like this-

     public class PsychStat {
    	public static void main (String [] args) {
    		Scanner input= new Scanner (System.in);
    		int result;
    		System.out.println();
    		System.out.println("Enter first trait value");
    		num1 = input + 1;
    		System.out.println("Result is:" + result);
     
    	}
    }

    And the compiler's flipping out. Why?



    javac PsychStat.java


    PsychStat.java:3: cannot find symbol
    symbol : class Scanner
    location: class PsychStat
    Scanner input= new Scanner (System.in);
    ^
    PsychStat.java:3: cannot find symbol
    symbol : class Scanner
    location: class PsychStat
    Scanner input= new Scanner (System.in);
    ^
    PsychStat.java:7: cannot find symbol
    symbol : variable num1
    location: class PsychStat
    num1 = input + 1;
    ^
    PsychStat.java:7: operator + cannot be applied to Scanner,int
    num1 = input + 1;
    ^

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Trying to get Java to ask the user for a number

    cannot find symbol
    symbol : class Scanner
    The compiler can not find a definition for the Scanner class. You need to add an import statement to your class to tell the compiler what package the Scanner class is in.

    cannot find symbol
    symbol : variable num1
    The compiler can not find a definition for the num1 variable. You need to add a definition for that variable.
    You have one for the result variable. Add one like that for the num1 variable.

    operator + cannot be applied to Scanner,int
    The variable: input is a Scanner object. You can not do arithmetic with it.

    Go read some more about how to use the Scanner class. Look at the code samples here on the forum.

  5. #5
    Junior Member
    Join Date
    Feb 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Trying to get Java to ask the user for a number

    I finally just got it to ask me for a number and add 1. How terrifically frustrating.

    Now, I assume to get it to actually do what I want, I'll have to define num2 as num1 * log(num 1)?
    And once I do that, I have to repeat the same code for as many variables as I need? Or can I use an if/then to tell it to keep asking for integers until I give it a 0, then calculate the equation I need it to calculate?

    import java.util.Scanner;
    public class PsychStat {
    	public static void main (String [] args) {
    		System.out.println("Input the first trait value");
    			Scanner scan = new Scanner (System.in);
    				int num1= scan.nextInt();
    				int num2= num1 + 1;
    				System.out.println("The answer:" + num2);
    	}
    }
    Last edited by SunsetSkies; February 26th, 2012 at 05:47 PM.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Trying to get Java to ask the user for a number

    PsychStat.java:7: ';' expected
    The compiler got confused and thought there were several errors.
    I think the one above is the important one. Statements should end with a ;
    The compiler didn't find one and tried to add the contents of the next line to the line without the ; and got confused.
    Add an ending ;

    The Scanner is a class that has methods that can read input from many sources. The version that you are using will read from the console: new Scanner(System.in);

  7. #7
    Junior Member
    Join Date
    Feb 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Trying to get Java to ask the user for a number

    Ugh, I'm sorry, the most coding I've ever done has pretty much been Hello World.

    I have this-

    import java.util.Scanner;
    public class PsychStat {
    	public static void main (String [] args) {
    		System.out.println("Input the first trait value");
    			Scanner scan = new Scanner (System.in);
    				int num1= scan.nextInt();
    				int num2= Math.log(num1);
    				int num3 = num2 * num1;
    				System.out.println("The answer:" + num3);
    	}
    }

    Trying to tell it to take the inputted number and multiply it by the log of that number.

    1. I'm sure there's a much cleaner way of doing this, isn't there?
    2. Compiler is telling me this -

    PsychStat.java:7: possible loss of precision
    found : double
    required: int
    int num2= Math.log(num1);
    ^
    1 error

    But if I take away Math, it keeps wanting me to endlessly add parentheses to either side. And for some reason, everywhere I'm searching keeps mentioning double, but I don't want to double my number, I just want it to be multiplied by the log of it.

    And once I get that done, I'll actually need it to be the integer multiplied by (log of the integer divided by the log of 2). Will I have to keep making variables to creep up on the equation I want it to do?

    Thanks!

    I really cannot say how much I appreciate your patience with me.

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

    Default Re: Trying to get Java to ask the user for a number

    Read the API doc for the Math class's method that you are using. What does it return? The compiler thinks it returns a double value which you are trying to assign to an int variable. That could give: possible loss of precision
    For example int x = 1.9999 gives x a value of 1

Similar Threads

  1. Please help! Noob java user here
    By New Guy_5 in forum Loops & Control Statements
    Replies: 3
    Last Post: November 14th, 2011, 09:44 PM
  2. How to display the results + repeat to the number entered by user?
    By TheBattousaixx in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 17th, 2011, 11:56 PM
  3. number formatter in java
    By ravindra.neeluri in forum Member Introductions
    Replies: 2
    Last Post: July 30th, 2011, 03:01 PM
  4. How to returned random number to original number?
    By i4ba1 in forum Algorithms & Recursion
    Replies: 2
    Last Post: March 19th, 2011, 04:35 AM
  5. New Java user ActionListener question
    By VBGuy in forum What's Wrong With My Code?
    Replies: 7
    Last Post: July 20th, 2010, 09:46 PM