I'm trying to make a simple calculator and need help with somthing
import java.util.*;
import javax.swing.*;
public class HiWorld {
public static void main(String[] args) {
System.out.println("Type 2 Numbers");
Scanner input1 = new Scanner(System.in);
Scanner input2 = new Scanner(System.in);
answer = input1 + input2;
System.out.println(answer);
}
It says that the operator + is undefined for the argument types java.util.scanner.
What you are doing there is creating two objects that read from standard input (your keyboard) and then, you are trying to add those objects together.
For start, use:
import java.util.*;
public class HiWorld {
public static void main(String[] args) {
System.out.println("Type 2 Numbers");
Scanner input = new Scanner(System.in);
int x = input.nextInt();
int y = input.nextInt();
int answer = x + y;
System.out.println(answer);
}
}
In Java you can only use the + operator for adding two numbers or concatenating two Strings. Not for doing anything with scanners.
Related
I am making a program that detects the input data type t whether it's integer or double . I have don't the following code so far. But I don't know how to store the data I input.
Code:
import java.io.*;
import java.util.Scanner;
public class HeightCalc {
public static int temp,result;
public static void main(String[] args) {
Scanner input = new Scanner (System.in);
if (input.hasNextInt()) {
System.out.println("Integer.");
}
else if (input.hasNextFloat() || input.hasNextDouble()) {
System.out.println("Double.");
}
}
}
If you use the apache commons lib you can use StringUtils and NumberUtils.
In general define a variable for the scanner input and validate its content, validating the input directly is more restrictive.
import java.io.*;
import java.util.Scanner;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math;
public class HeightCalc {
public static int temp,result;
public static void main(String[] args) {
Scanner scanner = new Scanner (System.in);
String input = scanner.nextLine();
if (StringUtils.isNumeric(input))
{
System.out.println("Integer.");
}
else if (NumberUtils.isCreatable(input))
{
System.out.println("Double.");
}
System.out.println("Input:" + input);
}
}
You can store the input in a variable like in the above example.
String myInput = scanner.nextLine();
if I get it right you need to store input number value to one variable?
I would create Number inputValue variable and into it I would store values from scanner.
Number inputValue;
Scanner input = new Scanner (System.in);
if (input.hasNextInt())
{
inputValue = input.nextInt();
System.out.println("int.");
} else if (input.hasNextFloat() || input.hasNextDouble())
{
inputValue = input.nextDouble();
System.out.println("Double.");
}
inputValue will inherit class from .nextInt() or nextDouble().
However I would consider storing input to one variable to get rid of this condition and derivate other number classes from it.
Example for int
inputValue = input.nextDouble();
public int getIntValue(){
return inputValue.intValue();
}
package booking;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
public class CarProfile {
static int year;
static int cylinders;
static int plateNum;
public static void main(String[] args) {
int reg = 1;
ArrayList <CarProfile> list = new ArrayList <CarProfile>();
#SuppressWarnings("resource")
Scanner sc = new Scanner(System.in);
while(reg == 1) {
System.out.println("Enter manf. year");
year = sc.nextInt();
System.out.println("Enter num. of cyl");
cylinders = sc.nextInt();
System.out.println("Enter plateNum");
plateNum = sc.nextInt();
System.out.println("If you wish to register another car press 1, otherwise press anything");
reg = sc.nextInt();
}
Arrays.toString(list.toArray());
}
}
This is the code. I also tried to use a for loop with System but didn't get any luck there either. So what am I missing here?
I don't know what else to type, it is a really simple question.
You aren't calling System.out.println on the code, The code doesn't knows its suppose to print it out. so replace
Arrays.toString(list.toArray());
With
System.out.println(Arrays.toString(list.toArray()));
How to console input data in java class(not main class). The input methods work in cpp but not in java. Is there a way to console from a class method. System.in.read and interger parse methods are not working.
Use Scanner with System.in
Scanner input = new Scanner(System.in);
int x = input.nextInt();
String t = input.next();
like wise
Try following code
import java.util.Scanner;
public class Demo {
public static void main (String [] args){
A a = new A();
a.getValue();
}
}
class A {
public void getValue(){
Scanner input = new Scanner(System.in);
System.out.println("Enter number : ");
int x = input.nextInt();
System.out.println(x);
}
}
This question already has answers here:
Java help: using loops to print the squares of values in between start arg and end arg
(3 answers)
Closed 5 years ago.
I want to write a program that finds the square of all numbers up to a user inputed number. For example, if the user inputs 5, then the program outputs 1,4,9,16,25.
This is what I've tried.
import java.util.Scanner;
public class PerfectSquares {
public void numberInput(){
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number");
int choice = sc.nextInt();
for(int i =0;i<choice;i++){
choice=choice*choice;
}
System.out.println("The squares are" + choice);
}
public static void main(String[] args) {
PerfectSquares input = new PerfectSquares();
input.numberInput();
}
}
The output gives me only one number which is not what I am looking for.I am new to JAVA and programming but my understanding is that you should try to create methods outside the main and then create objects to call the methods inside the main. So I am trying to create a method called numberInput and then in the main I try to call it. Please correct me if this is the wrong approach.
You are very close, the issue for your outcome is that the loop is updating the same number.
Import java.util.Scanner;
public class PerfectSquares {
public void numberInput(int max){
List<Integer> squares = new ArrayList<>();
for(int i =0;i<max;i++) {
squares.add(i^2);
}
System.out.println("The squares are: " + String.join(squares, ", ");
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number");
int choice = sc.nextInt();
PerfectSquares input = new PerfectSquares();
input.numberInput(choice);
}
I'm helping someone learn Java at the moment. I'm a senior C# developer. I understand programming in general but I do not know the Java APIs.
Most advice on the web on how to read an int from the console is complicated. It involves BufferedReader and InputStreamReader and such. This is completely incomprehensible to a beginner.
I want to provide him with a function static int readIntFromConsole() that does this. What would the body of that function be?
Try the following:
Scanner scanner = new Scanner(System.in);
System.out.print("Enter some integer:\t");
int myIntValue = scanner.nextInt();
In order to use through the lifespan of your test application, you can do the following:
public void scanInput() {
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.print("Enter some integer:\t");
int myIntValue = scanner.nextInt();
System.out.print("You entered:\t" + myInteValue);
}
}
Are you talking about reading user input from the console? You could use the Scanner class in that case.
Scanner stdin = new Scanner(System.in);
int input = stdin.nextInt();
If you want to grab different input, no need to initialize a new scanner. You can use a different method from the scanner in that case, depending on the value you want.
System.out.println("Enter a double: ");
double myDouble = stdin.nextDouble();
System.out.println("Enter a string of characters: ");
String myString = stdin.next();
System.out.println("Enter a whole line of characters: ");
String myEntireLine = stdin.nextLine();
etc.
See this example
import java.util.Scanner;
public class InputTest {
public static void main(String[] args) {
//Now using the readIntFromConsole()
int myNumber = readIntFromConsole();
System.out.println(myNumber);
}
public static int readIntFromConsole() {
System.out.print("Enter an integer value....");
Scanner s = new Scanner(System.in);
int number = s.nextInt();
return number;
}
}
//Edit: provided the required method