import java.util.Scanner;
public class userInput
{
public static void main(String[]args){
try{
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();
int age = scanner.nextInt();
scanner.nextLine();
String text = scanner.nextLine();
System.out.println(name + "\n" + age + "\n" + text);
//scanner.close(); //it works here
}
finally{
scanner.close(); // does not work here"scanner cannot be resolvedJava(570425394)"
}
}
}
You have to define scanner before "try", so it's ok, now this is your code:
import java.util.Scanner;
public class userInput {
public static void main(String[]args) {
Scanner scanner = new Scanner(System.in);
try {
String name = scanner.nextLine();
int age = scanner.nextInt();
scanner.nextLine();
String text = scanner.nextLine();
System.out.println(name + "\n" + age + "\n" + text);
}
finally {
scanner.close();
}
}
}
Related
i'm new to programming and we've got an example from school to understand how Scanner works. My problem is that i do not understand why the message "Input" (System.out.println in the while loop) gets printed twice.
import java.util.Scanner;
class ScannerInput {
public static void main(String[] args) {
Scanner myScanner = new Scanner(System.in);
System.out.println("Give me a String:");
String zeichenkette = myScanner.nextLine();
System.out.println("Give me an Integer:");
int einInteger = myScanner.nextInt();
// Output input by user
System.out.println("String: " + zeichenkette);
System.out.println("Integer: " + einInteger);
boolean scannerSuccess = false;
while(!scannerSuccess) {
System.out.print("Input: ");
if(myScanner.hasNextInt()) {
einInteger=myScanner.nextInt();
myScanner.nextLine();
scannerSuccess=true;
} else if(myScanner.hasNextLine()){
myScanner.nextLine();
scannerSuccess=false;
}
}
myScanner.close();
}
}
else if(myScanner.hasNextLine()){
myScanner.next();
scannerSuccess=false;
}
try this code
i want to get all the result from another method and compile it as one
package labexer1a;
import java.util.Scanner;
public class LabExer1A {
public static void main(String[] args) {
faveNumber();
faveCartChar();
mI();
fullName();
nickName();
}
public static void faveNumber() {
Scanner s = new Scanner(System.in);
System.out.print("Type your Number: ");
int faveNumber = s.nextInt();
}
public static void faveCartChar() {
Scanner s = new Scanner(System.in);
System.out.print("Type your favorite Cartoon or Anime Character: ");
String faveCartChar = s.next();
}
public static void mI() {
Scanner s = new Scanner(System.in);
System.out.print("Type your Middle Initial: ");
String mI = s.next();
}
public static void fullName() {
Scanner s = new Scanner(System.in);
System.out.print("Type your FullName: ");
String fullName = s.next();
}
public static void nickName() {
Scanner s = new Scanner(System.in);
System.out.print("Type your Nickname: ");
String nickName = s.next();
}
public static void result() {
System.out.print(faveNumber + "is my favorite number");
System.out.print("I love" + faveCartChar);
System.out.print("My name is" + fullName);
System.out.print("You can call me" + nickName);
}
}
You can change the fuctions return type from void to String or int for example and the return the result.
Example:
public static String fullName() {
Scanner s = new Scanner(System.in);
System.out.print("Type your FullName: ");
String fullName = s.next();
return fullName;
}
public static int faveNumber() {
Scanner s = new Scanner(System.in);
System.out.print("Type your Number: ");
int faveNumber = s.nextInt();
return faveNumber;
}
If you now want to acces the data just call the fuction and it will return it.
Example:
System.out.println(nickName() + " is my favorite number");
System.out.println("You can call me " + faveNumber());
And also I would recomend naming the functions more like getNickName so anyone can direclty see that it will return the nick name!
I need to read spaces (present before string and after String) given as input using Scanner
Note : if there is no spaces given in input it should not add space in output
Please find the below code:
package practise;
import java.util.Scanner;
public class scanccls {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scan = new Scanner(System.in);
String name= scan.nextLine();
name+=scan.nextLine();
scan.close();
System.out.println("Enter your name"+name);
}
}
I am expecting output like:
Input :Enter Your name:Chandu Aakash
Output:chandu Aakash
Input: Enter Your name: (Space..)Chandu Aakash(Space..)
Output: (space.. )chandu Aakash(Space..)
Your code work fine. I just add little modification:
package practise;
import java.util.Scanner;
public class scanccls {
public static void main(String[] args) {
System.out.println("Enter your name:");
Scanner scan = new Scanner(System.in);
String name="";
name+=scan.nextLine();
scan.close();
System.out.println("Your name is :"+name);
}
}
One can use the delimiter function to segregate your input as shown below.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in).useDelimiter("\n");
String input = scanner.next();
System.out.println(input);
scanner.close();
}
}
import java.util.*;
public class Str{
public static void main(String[] args) throws IOException
{
Scanner scan = new Scanner(System.in);
int i = scan.nextInt();
double d = scan.nextDouble();
String s=" ";
s= scan.nextLine();
s+=scan.nextLine();
scan.close();
System.out.println("String: "+s);
System.out.println("Double: "+d);
System.out.println("Int: "+i);
}
}
/#esprittn solution didn't work./
my solution:
while(scan.hasNext()){
name+=scan.nextLine();
}
I use this function below, to read from all user input format, text inclusive spaces, then parse to specific datatype after.
package practice;
import java.io.*;
public class readInputSample{
public static void main(String[] args) {
String strVal = getInput("Enter string value: "); // Direct as string
Integer intVal = Integer.parseInt(getInput("Enter integer value: "));
Double dblVal = Double.parseDouble(getInput("Enter double value: "));
Float fltVal = Float.parseFloat(getInput("Enter float value: "));
System.out.println("String value: " + strVal);
System.out.println("Integer value: " + intVal);
System.out.println("Double value: " + dblVal);
System.out.println("Float value: " + fltVal);
}
// Special Function to read all user input
private static String getInput(String prompt){
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
System.out.print(prompt);
System.out.flush();
try{
return stdin.readLine();
} catch (Exception e){
return "Error: " + e.getMessage();
}
}
}
I have done a few changes in your code, it will run just fine for this code
package practise;
import java.util.Scanner;
public class scanccls {
public static void main(String[] args) {
//// TODO Auto-generated method stub
System.out.println("Enter your name:");
Scanner scan = new Scanner(System.in);
String name="";
name+=scan.nextLine();
scan.close();
System.out.println("Your name is :"+name);
}
}
package practise;
import java.util.Scanner;
public class scanccls
{
public static void main(String[] args)
{
System.out.println("Enter your name:");
Scanner scan = new Scanner(System.in);
String name = "";
name += scan.nextLine();
// Can also be done like
// String name=scan.next();
// name+=scan.nextLine();
// They Both Work as same
System.out.println("Your name is :" + name);
}
}
package practise;
import java.util.Scanner;
public class scanccls {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scan = new Scanner(System.in);
String name= scan.nextLine();
scan.close();
System.out.println("Enter your name"+name);
}
}
I'm trying to understand why inputString is still empty after executing this
public static void main(String[] args) {
// write your code here
int inputInt = 0;
double inputDouble = 0.0;
String inputString = null;
Scanner scanner3 = new Scanner(System.in);
if (scanner3.hasNext()) {
inputInt = scanner3.nextInt();
}
if (scanner3.hasNext()) {
inputDouble = scanner3.nextDouble();
}
if (scanner3.hasNext()) {
inputString = scanner3.nextLine();
} else {
throw new RuntimeException("No entries left");
}
System.out.println("String: " + inputString);
System.out.println("Double: " + inputDouble);
System.out.println("Int: " + inputInt);
}
nextLine() read new line character before read your character. Add a extra nextLine() to read that new line.
if (scanner3.hasNext()) {
scanner3.nextLine();
inputString = scanner3.nextLine();
}
I am trying to execute the following code but keep getting this error:
Error: Main method not found in class ScannerDemo, please define the main method as:
public static void main(String[] args)
import java.util.Scanner;
class ScannerDemo
public class Main {
public static void main (String [] args)
{
Scanner sc = new Scanner(System.in);
String userName;
System.out.println("Enter a number");
username = sc.nextLine();
System.out.println("your number is" + username + "enter your next number");
username2 = sc.nextline();
System.out.println("your total is" + username2 );
}
}
I think I must be importing the Scanner class wrong, I have tried different methods but nothing has worked for me yet.
You have two class declarations in a row at the top! This is wrong. Your import is fine. Try:
import java.util.Scanner;
public class ScannerDemo {
public static void main(String[] args) {
...
}
}
Your class definition is wrong. You are trying to define two classes, ScannerDemo and Main. Replace:
class ScannerDemo
public class Main
With just:
public class ScannerDemo
Also, in your main method, you should be referring to the userName variable, instead of username, and you are not defining username2. Note that Java identifiers are case sensitive:
public static void main (String [] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number");
String userName = sc.nextLine();
System.out.println("your number is" + username + "enter your next number");
String username2 = sc.nextline();
System.out.println("your total is" + username2 );
}
import java.util.Scanner;
public class ScannerDemo {
public static void main (String [] args)
{
Scanner sc = new Scanner(System.in);
String userName;
System.out.println("Enter a number");
username = sc.nextLine();
System.out.println("your number is" + username + "enter your next number");
username2 = sc.nextline();
System.out.println("your total is" + username2 );
}
}
import java.util.Scanner;
public class ScannerDemo {
public static void main (String [] args) {
Scanner sc = new Scanner(System.in);
String userName;
System.out.println("Enter a number");
int userName = sc.nextInt();
System.out.println("your number is " + userName);
System.out.println("enter your next number");
int userName2 = sc.nextInt();
System.out.println("your total is " + (userName2 + userName));
}
}
Hope this helps
import java.util.Scanner;
public class ScannerDemo {
public static void main (String [] args) {
Scanner sc = new Scanner(System.in);
String userName;
System.out.println("Enter a number");
int username = sc.nextInt();
System.out.println("your number is " + username);
System.out.println("enter your next number");
int username2 = sc.nextInt();
System.out.println("your total is " + (username2 + username));
}
}