+ 1

How can I make one input?

Input

13th Oct 2016, 9:14 PM
Bence Farkas
Bence Farkas - avatar
5 Antworten
+ 1
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); String input = scan.nextLine(); System.out.println(input); } } Here is an example ^^^ I will break it down: import java.util.Scanner; ^imports the Scanner utility, its basically a library of methods for your program to use. Scanner scan = new Scanner(System.in); ^This creates a new Scanner object called scan, which can be used to scan for input. String input = scan.nextLine(); ^This creates a String called input and it is equal to scan.nextLine(); which means it will wait until something is entered into the console and then it will store that line of data entered into the String "input". To make this work with an integer you just replace String with int and nextLine(); with nextInt(); import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int input = scan.nextInt(); System.out.println(input); } } Hope this helps and if you have any other questions just ask ;)
13th Oct 2016, 11:51 PM
Liam Keegan
Liam Keegan - avatar
+ 1
Thanks for all answer!
14th Oct 2016, 10:13 AM
Bence Farkas
Bence Farkas - avatar
+ 1
you can also take input through method e.g. void main(int num) it will take input for num
14th Oct 2016, 1:10 PM
Mankrit Singh
Mankrit Singh - avatar
0
you must use scanner, import it, then put this: if (playerAnswer.equalsIgnoreCase("apples "){ //put your code here (System.out.println(" "); /***you dont have to put player answer as your variable, you can have what ever you want as long as you declared it first***/ } //hope that helped :)
13th Oct 2016, 10:24 PM
Aquarius
Aquarius - avatar
0
int nextInt() Returns the next token as an int. If the next token is not an integer,InputMismatchException is thrown. long nextLong() Returns the next token as a long. If the next token is not an integer,InputMismatchException is thrown. float nextFloat() Returns the next token as a float. If the next token is not a float or is out of range,InputMismatchException is thrown. double nextDouble() Returns the next token as a long. If the next token is not a float or is out of range,InputMismatchException is thrown. String next() Finds and returns the next complete token from this scanner and returns it as a string; a token is usually ended by whitespace such as a blank or line break. If not token exists,NoSuchElementException is thrown. String nextLine() Returns the rest of the current line, excluding any line separator at the end. void close() Closes the scanner. you will need to apply these depending on the variable type you will be inputting.
14th Oct 2016, 2:15 AM
Evelyn
Evelyn - avatar