Math.pow(x,2) area of square - java

i am running a code, but i dont seem to be able to get the scanner working, anyone got an idea how?
public class verk34 {
public static void main(String[] args) {
System.out.println(Math.pow(x,2));
}
}
the thing is inserting a number on your own without altering the code.

As per my understanding, you need to add Scanner for taking input from end users and importing the java.util.Scanner statement.
Scanner in = new Scanner(System.in);
int x = in.nextInt();

First, you need to import scanner and Math
import java.util.Scanner
import java.lang.Math
Then If I am understanding correctly the problem, you need to do this in two steps :
-Instantiate a scanner and collect user input as an integer.
Scanner sc = new Scanner(System.in);
System.out.println(" Please enter the square area :");
int userValue = sc.nextInt();
The nextInt method will lock process and wait for a keyboard event before continuing. So enter a nulber with your keyboard.
-Use System.out.println with given value.
System.out.println(Math.pow(userValue,2));

Related

Check If User Inputs Specific Data [duplicate]

This question already has an answer here:
How to use java.util.Scanner to correctly read user input from System.in and act on it?
(1 answer)
Closed 1 year ago.
I would like to check if the user has input a specific word/alphabet so that I can run some code on the basis of what s/he inputs.
This is my code:
import java.util.concurrent.ThreadLocalRandom;
import java.util.Scanner;
public class EligibilityTest {
public static void main(String[] args) {
Scanner temperature = new Scanner(System.in);
System.out.println("Press the 'T' key to check your temperature. ");
double tempMeasure = ThreadLocalRandom.current().nextDouble(96, 98.9);
// Here I would like to add an if statement to check if the user has input the 'T', and if yes, print the tempMeasure
}
}
How do I construct this if statement, and how do I keep the decimal value for variable 'tempMeasure' in two digits? I find Java inputs pretty complex, since I started learning after Python, which is relatively much easier.
Yoy need to add
scan.nextLine()
to retrieve input from user. And then you can write if statement:
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Press the 'T' key to check your temperature. ");
double tempMeasure = ThreadLocalRandom.current().nextDouble(96, 98.9);
String temperature = scan.nextLine();
if(temperature.equals("T")){
System.out.println(tempMeasure);
}
}

Import these scanners into another class?

Again I am very new to Java, and I have this code here:
package Final;
import java.util.Scanner;
public class Position {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner MyPos = new Scanner(System.in);
System.out.println(" Enter Position: ");
String Pos = MyPos.nextLine();
System.out.println("Position: "+Pos);
Scanner MyMains = new Scanner(System.in);
System.out.println("Enter Mains: ");
String Mains1 = MyMains.nextLine();
System.out.println("Mains: "+Mains1);
Scanner WScore = new Scanner(System.in);
System.out.println("Enter Ward Score: ");
String score = WScore.nextLine();
System.out.println(" Average Ward Score is:"+score);
}
}
I was curious if there was a way to possibly import all 3 of my scanners to another class without moving all the actual code from this class? The class I'm trying to move it to is a class called "Player.java". There aren't any problems with my code, but additional input is always appreciate to help me understand and improve my code!
You don't need a new scanner for each variable. What you do need to do is close() a scanner when you are done using it.
You are thinking of the Scanner itself being associated with a variable - when in fact it is associated with System.in...
So, every time you want another variable, just do:
Scanner sc = new Scanner(System.in);
String firstThing = sc.nextLine();
String secondThing = sc.nextLine();
sc.close(); // Do this when you're done storing input from System.in!
Once you have declared your scanner. I'm not sure why you would want to pass the scanner to another class. What you can do easily is pass the variables that you grab from your scanner into a player object.
Just do Player p1 = new Player(stringYouScanned); assuming that you have your constructor in your Player class accept a String.
Or, look up getter and setter methods. Then you can do something like: `p1.setPosition(4);' (you could replace 4 with an int you scanned in, you get the idea).
If for some reason you want to scan input from within the Player, I'd just initialize a new Scanner, but make sure you are doing sc.close(); after you are done processing your input.

Scanner variable cannot be resolved

In my program, the user will be asked to input 3 integers. The integers will then be read using the Scanner class and listed back to the user.
This is my code:
import java.util.Scanner;
public class Echoer
{
public static void main(String[] args)
{
/* The Data Below Will Read The Numbers Input Into The Prompt*/
Scanner input = new Scanner(System.in);
System.out.println("Please Enter Three Integers: ");
int number;
number = input.nextInt();
Scan.close();
System.out.println("Thanks. The Numbers You Entered Are: " + number);
}
}
This is the error it returns:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Scan cannot be resolved
Why does it return this error? How can I fix this issue?
In your code, you never defined what Scan was. Use input.close() rather than Scan.close().
Scan cannot be resolved
means that you never defined Scan. This is because you said Scan.close(). You need to change it to input.close() because input is the name of the instance of the Scanner class.
As others pointed out, you have to close input instead of Scan as shown below.
import java.util.Scanner;
public class Echoer
{
public static void main(String[] args)
{
/* The Data Below Will Read The Numbers Input Into The Prompt*/
Scanner input = new Scanner(System.in);
System.out.println("Please Enter Three Integers: ");
int number;
number = input.nextInt();
input.close();
System.out.println("Thanks. The Numbers You Entered Are: "+number);
}
}

What is wrong with my Java coding? It is saying that it cannot find the symbol scanf. How should I recode this?

I am trying to make a Java code that enables the user to input any number and the Java makes a triangle out of that number using *
My code is not compiling, but I think I've finally almost got it down. The only problem is, that it is not recognizing scanf.
Here is my code:
import java.util.Scanner;
class triangle
{
public static void main(String[] args)
{
char print='*';
int row,col;
int noOfRows;
System.out.printf("Enter number of rows to be printed\n");
scanf("%d",noOfRows);
{
for(col=1;col<=row;col++)
{ // this brace is useless, since there is only one statement in this for loop
System.out.printf("%c",print);
} // same for this one
System.out.printf("\n");
}
}
}
How can I fix this?
scanf() and printf() are supported by C and C++ in java:
1. BufferedReader or Scanner for reading from console(System.in).
2. print() or println() for printing to console.(System.out).
You need to declare a new object of the scanner.
Also, to print, is System.out.println("");
You should use Scanner class like this: Scanner scanner = new Scanner(System.in); and do something like this:
System.out.println("Enter number of rows");
noOfRows = scanner.nextInt();
My friend, you use scanf in "C"of "C++" in java we use System.in

cannot find symbol error when creating an input object in java

I am new to java and netbeans. I am trying to write a program that requires user input. This is my code:
public class Arrays {
public static void main(String[] args){
}
private double[] readNumbers(){
final Input in = new Input();
System.out.print("How many numbers will you enter?: ");
final int count = in.nextInt();
final double[] list = new double[count];
for (int i = 0; i < count; ++i){
System.out.print("Enter next number: ");
list[i] = in.nextDouble();
}
return list;
}
}
In line final Input in - new Input(); Netbeans underlines Input saying that it cannot find symbol. However I practically copied this code from the textbook so I don't understand what the problem is. I thought maybe I needed to import java.io, but that did not solve the problem. Really sorry if this is a stupid question, but any help would be really appreciated.
Thank you!
Looks like your text book has some class definition that you forgot to import to your project.
If you like to , change your code like,
final Input in = new Input();
to
final Scanner in = new Scanner(System.in);
If you don't want to change your code, then look few pages up and down the pages from where you got this code, you should see Class named Input , some what similar to this :
class Input{
public int nextInt(){
Scanner sc=new Scanner(System.in);
return sc.nextInt();
}
public double nextDouble(){
Scanner sc=new Scanner(System.in);
return sc.nextDouble();
}
}
Which is basically, an extra unnecessary work.
Include that in your project, and it should run fine.
Your code attempts to create an instance of class Input, but you don't include code for class Input. Resolve that (try the previous page in the book!) and your code will probably work.
Input may be a wrapper class for java.util.Scanner:
You could replace:
final Input in = new Input();
with
final Scanner in = new Scanner(System.in);

Categories

Resources