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-
Code :
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!!
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.
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-
Code :
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;
^
Re: Trying to get Java to ask the user for a number
Quote:
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.
Quote:
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.
Quote:
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.
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?
Code :
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);
}
}
Re: Trying to get Java to ask the user for a number
Quote:
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);
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-
Code :
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.
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