Scanner in = new Scanner(System.in); [duplicate] - java

This question already has answers here:
Where is the keyboard specified the System.in method?
(2 answers)
Closed 7 years ago.
public class Adder
{
public static void main(String arr[])
{
//System.in represents Standard Input Device (Keyboard)
//Explain this next line, please:
Scanner in = new Scanner(System.in);
System.out.println("Enter First No.");
int a = in.nextInt();
System.out.println("Enter Second No.");
int b = in.nextInt();
int c = a+b;
System.out.println("Sum is: "+c);
}
}
I just started learning JAVA and i came across this code.Can someone explain me what does the marked line signify ?

Scanner s=new Scanner(System.in);
The System.in in the above code tells the compiler to get the data typed in the Keyboard, Which is a input device
Thanks,
For more basic things in java Please checkout:
Tutorialspoint.com

Related

next() vs nextLine() in JAVA [duplicate]

This question already has answers here:
Scanner is skipping nextLine() after using next() or nextFoo()?
(24 answers)
Closed 4 years ago.
I was trying to take input for the number of names to be stored in an array from user and then using that i was taking names from the user ,first i tried to take names from the user using next() method and all the things were fine but when i tried to take input using nextLine() method the output was as shown below
package learningJava;
import java.util.Scanner;
public class practice
{
public static void main(String[] args)
{
int n;
Scanner obj = new Scanner(System.in);
System.out.println("Enter the number of names you are gonna enter");
n = obj.nextInt();
String names[] = new String[n];
for(int i=0;i<n;i++)
{
System.out.println("Enter the name of friend "+(i+1));
names[i]=obj.nextLine();
}
obj.close();
System.out.println("Names of your friends are");
for(int i=0;i<names.length;i++)
{
System.out.println(names[i]);
}
}
}
Output for the nextLine() method
Enter the number of names you are gonna enter
5
Enter the name of friend 1
Enter the name of friend 2
It is not prompting me to enter the name of friend 1 and directly skipping it and coming to the friend 2 line.
I am beginner in Java , i know the basic difference in next and nextLine() that next() doesn't take input after a space but nextLine() takes complete input , So what is happening here ??
just in for loop, just change "println" to "print" because nextLine() consumes new line character.
import java.util.Scanner;
public class practice
{
public static void main(String[] args)
{
int n;
Scanner obj = new Scanner(System.in);
System.out.println("Enter the number of names you are gonna enter");
n = obj.nextInt();
String names[] = new String[n];
for(int i=0;i<n;i++)
{
System.out.print("Enter the name of friend "+(i+1));
names[i]=obj.nextLine();
}
obj.close();
System.out.println("Names of your friends are");
for(int i=0;i<names.length;i++)
{
System.out.println(names[i]);
}
}
}
check this answer: Java String Scanner input does not wait for info, moves directly to next statement. How to wait for info?

Java resource leak not closed [duplicate]

This question already has answers here:
Close a Scanner linked to System.in
(5 answers)
Closed 6 years ago.
I am writing a program where I want the console to output the remainder of the user inputted number. However, every time I compile the code the console prints this out and I get this console error:
1 problem (1 warning)
Compiler is using classPath = '[C:\Users\Darien Springer\Documents\Java, C:\Users\Darien Springer\Desktop\drjava-beta-20160913-225446.exe]'; bootClassPath = 'null'
----------
1. WARNING in C:\Users\Darien Springer\Documents\Java\PrintDigits.java (at line 5)
Scanner scnr= new Scanner(System.in);
^^^^
Resource leak: 'scnr' is never closed
----------
1 problem (1 warning)
I am not sure what the console means by a "resource leak". I have looked it up in several different places (including the API and other Stack Overflow questions) and I am not sure why nothing prints to the console. I am using the program DrJava in case anyone is wondering.
Here is my code for reference:
import java.util.Scanner;
public class PrintDigits {
public static void main(String [] args) {
Scanner scnr= new Scanner(System.in);
int userInput = 0;
int positiveInt = 0;
System.out.println("enter a positive integer:");
userInput = scnr.nextInt();
positiveInt = userInput % 10;
System.out.println(positiveInt);
return;
}
}
All that that warning says is tha you never call scnr.close(); anywhere in your code. To get it to go away, just call scnr.close(); after you are done using the scanner.
import java.util.Scanner;
public class PrintDigits {
public static void main(String [] args) {
Scanner scnr= new Scanner(System.in);
int userInput = 0;
int positiveInt = 0;
System.out.println("enter a positive integer:");
userInput = scnr.nextInt();
positiveInt = userInput % 10;
System.out.println(positiveInt);
scnr.close();
return;
}
}

The code runs again no matter what. [duplicate]

This question already has answers here:
Scanner is skipping nextLine() after using next() or nextFoo()?
(24 answers)
Closed 7 years ago.
I don't know why, but the below code makes the user run the code again, whether they choose to or not. I've tried many things, but it doesn't work correctly.
Thanks!
public static void main (String [ ] args)
{
boolean a = true;
while (a)
{
Scanner scan = new Scanner(System.in);
System.out.print("Enter an integer: ");
int x = scan.nextInt();
System.out.print("\n\nEnter a second integer: ");
int z = scan.nextInt();
System.out.println();
System.out.println();
binaryConvert1(x, z);
System.out.println("\n\nWould you like to run this code again? Enter \"Y\" or \"N\".");
System.out.print("Enter your response here: ");
String RUN = scan.nextLine();
String run = RUN.toLowerCase();
if (run.equals("n"))
{
a = false;
}
System.out.println();
System.out.println();
}
System.out.println("Goodbye.");
}
Scanner.nextInt() doesn't consume the line ending characters from the buffer, which is why when you read the value of the "yes/no" question with scan.nextLine(), you'll receive an empty string instead of the value the user entered.
A simple way to fix this is to explicitly parse the integer from raw lines using Integer.parseInt():
System.out.print("Enter an integer: ");
int x = Integer.parseInt(scan.nextLine());
System.out.print("\n\nEnter a second integer: ");
int z = Integer.parseInt(scan.nextLine());

error with inputting string using sc.nextLine [duplicate]

This question already has answers here:
Scanner issue when using nextLine after nextXXX [duplicate]
Using scanner.nextLine() [duplicate]
(5 answers)
Closed 8 years ago.
import java.util.Scanner;
public class GpaConverterTester
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
GpaConverter g = new GpaConverter();
System.out.println("How many classes are you taking? ");
int classAmount = sc.nextInt();
while(classAmount > 0)
{
System.out.println("Enter Grade: ");
String grade = sc.nextLine();
g.setGpaValue(grade);
classAmount--;
}
System.out.println("Average: " + g.getAverage());
}
}
My basic problem is that it wont let me enter in the grade string. This is what happens...
Output:
"How many classes are you taking?
2
Enter Grade:
Enter Grade:
"
It does not let me enter in the grade string.
Thank you for helping!
I figured it out.
Use sc.next() instead of line.

scan.nextLine does not work [duplicate]

This question already has answers here:
Scanner is skipping nextLine() after using next() or nextFoo()?
(24 answers)
Why can't I enter a string in Scanner(System.in), when calling nextLine()-method?
(13 answers)
Closed 9 years ago.
I used Java and I tried to write program that reads a number of patients and then let the employee enter their names and ages.
and I want the program reads full name such as (Maha Saeed) so I wrote the code like this but I don't know why it does not work
name = scan.nextLine();
and this is the full code
import java.util.*;
public class Answer1
{
static Scanner scan = new Scanner (System.in);
public static void main (String[] args)
{
int age ;
int PatientNumber ;
int PatientNO;
String name ;
System.out.print("Enter number of patients :");
PatientNumber = scan.nextInt();
PatientNO = 1;
while ( PatientNO <= PatientNumber)
{
System.out.println("Patient #" +PatientNO);
System.out.print("Enter patient's Name: ");
name = scan.nextLine(); //<- here is the problem if I write scan.next it works but it reads only the first name
System.out.print("Enter patient's Age: ");
age= scan.nextInt();
PatientNO = PatientNO + 1;
}
}
}
thanks all
See Why can't I enter a string in Scanner(System.in), when calling nextLine()-method? and Scanner is skipping nextLine() after using next(), nextInt() or other nextFoo() methods
Simple solution, you can consume the \n character:
scan.nextLine();
name = scan.nextLine();

Categories

Resources