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;
}
}
Related
so I'm a beginner I just started like 3 days ago and I'm trying to make a While statement in java and I can't seem to find a way to make a loop without not having a user input again in the while block, My idea in this code is to ask the user for the wanted operation and if it's empty or non of the operations available, the program will give him an error message then loop the program
import java.util.*;
public class calc{
public static void main(String[] args) {
Scanner sc = new Scanner (System.in);
System.out.println("1.Sum\n2.Subtraction\n3.Multiplication\n4.Division");
int oper = sc.nextInt();
while (oper > 4 || oper < 1) {
System.out.println("Please enter a valid number");
System.out.println("1.Sum\n2.Subtraction\n3.Multiplication\n4.Division");
int oper = sc.nextInt();
}
}
}
The only thing really wrong is the second int oper = sc.nextInt();. You've already got a variable oper in scope, you can't declare another.
Remove the int.
You might instead want to consider restructuring the loop, so you don't have to repeat the messages and the reading from the scanner:
int oper;
while (true) {
System.out.println("1.Sum\n2.Subtraction\n3.Multiplication\n4.Division");
oper = sc.nextInt();
if (oper >= 1 && oper <= 4) {
break;
}
System.out.println("Please enter a valid number");
}
This question already has answers here:
Scanner is skipping nextLine() after using next() or nextFoo()?
(24 answers)
Closed 2 years ago.
I want to make a program that will read user inputs and will printout them gradually. But when I am running the code, in the Console area, the first line is automatically skipping. But when I am taking input as Integer, all is running well. Where is my fault?
import java.util.*;
public class MainClass {
static Scanner input = new Scanner(System.in);
public static void main(String[] args) {
int limit, i, j;
System.out.print("How many names you want to take: ");
limit = input.nextInt();
String[] name = new String[limit];
for (i = 0; i < name.length; i++) {
System.out.print("Enter your name: ");
name[i] = input.nextLine();
}
for (String output : name) {
System.out.println("Names are: " + output);
}
}
}
Console area:
How many names you want to take: 3
Enter your name: Enter your name: Saon
Enter your name: Srabon
Names are:
Names are: Saon
Names are: Srabon
Invoke input.nextLine() after the input.nextInt() in order to clear the new line character produced by pressing Enter key (when you enter int number);
Alternatively, you can read your int as Integer.valueOf(input.nextLine()).
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());
This question already has answers here:
Scanner is skipping nextLine() after using next() or nextFoo()?
(25 answers)
Closed 7 years ago.
My code below has a exception error for:
YN = input.nextLine().charAt(0); //line 13
when it is run. The assignment is to make an array that is assigned 10 numbers from console, when I put in 10 numbers the run completes automatically and gives an error for line 13. Is there something wrong with line 13 or is the issue with something else?
(The array must be a normal array and not an arrayList)
import java.util.*;
import java.util.Arrays;
public class CountOccurrences {
static Scanner input = new Scanner(System.in);
static int[][] temp = new int[10][1];
public static void main(String[] args) {
char YN = 'y';
while (YN == 'y') {
run();
System.out.print("Continue? (y or n)\t");
YN = input.nextLine().charAt(0); // Line 13
}
}
public static void run() {
System.out.print("Enter the integers between 1 and 100: ");
int[] numbersArray = new int[10];
for (int i = 0; i < numbersArray.length; i++) {
numbersArray[i] = input.nextInt();
}
for (int i = 0; i < numbersArray.length; i++) {
Arrays.sort(numbersArray);
System.out.println(numbersArray[i]);
}
}
}
input.nextLine() could be an empty String, in which case input.nextLine().charAt(0) will give you that exception. You should check the length() of the String before calling charAt.
Now that I see you are using input.nextInt() to read the inputs in your run() method, what you have to do is add a input.nextLine() at the end of the run() method, to consume the end of the line that contains the final int.
You still have to check the length of the String you get in input.nextLine(), though, since the user may hit enter instead of hitting Y or N, which will result in the same exception.
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