How do I close my scanner? - java

I have this code that works but I cannot close my scanner after I'm done. scanner.close() does not work anywhere and using try(Scanner scaner etc. does not seem to work either. Can anyone tell me how to close a scanner in a code like mine?
import java.util.Random;
import java.util.Scanner;
public class GuessingGame {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Random randomGenerator = new Random();
int input;
int code = 0;
int i = 0;
int[] guesses = new int[7];
System.out.println("Secretly type the code or input -1 if you want me to choose");
input = scanner.nextInt();
if (input == -1) {
code = randomGenerator.nextInt(100);
}
else {
code = input;
}
System.out.println("Start guessing!");
while (i < 7) {
guesses[i] = scanner.nextInt();
if (guesses[i] == code) {
System.out.println("Good guess! You won.");
System.out.println((i+1) +" guesses");
i++;
for (int k=0; k<i; k++) {
for (int j=0; j<100; j++)
{
if (j == guesses[k]) {
System.out.print("X");
}
else if (j == code) {
System.out.print("|");
}
else {
System.out.print(".");
}
}
System.out.println("");
}
}
else if (code < guesses[i] && i != 6) {
System.out.println("lower");
i++;
}
else if (code > guesses[i] && i != 6) {
System.out.println("higher");
i++;
}
else {
System.out.println("No more guesses, you lost");
System.out.println((i+1) + " guesses");
for (int k=0; k<=i; k++) {
for (int j=0; j<100; j++)
{
if (j == guesses[k]) {
System.out.print("X");
}
else if (j == code) {
System.out.print("|");
}
else {
System.out.print(".");
}
}
System.out.println("");
}
}
}
}
}

When you wrap a stream with another (like you do in scanner) closing the stream closes the wrapped streams.
That means you would close System.in if you closed your scanner.
I recommend setting your scanner variable to null, and letting the garbage collector remove it from the heap. Unless you explicitly want to close the input to the program, this will likely have the desired effect.

You should close the scanner after the while loop. Else you will certainly get errors.

use
Scanner scanner = ....;
try {
while () {} ....
} catch (Exception ex) {
try {scanner.close();}catch {} // closes the scanner in case of an exception
} finally { try {scanner.close(); } catch {}} // makes sure that the scanner closes. try catch because it may fail.

I cant reproduce your error with the scanner.close() method but I think it is not working inside a loop. Here is an example with it working for me:
import java.util.Random;
import java.util.Scanner;
public class Test{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Random randomGenerator = new Random();
int input;
int code = 0;
int i = 0;
int[] guesses = new int[7];
System.out.println("Secretly type the code or input -1 if you want me to choose");
input = scanner.nextInt();
if (input == -1) {
code = randomGenerator.nextInt(100);
}
else {
code = input;
}
System.out.println("Start guessing!");
while (i < 7) {
guesses[i] = scanner.nextInt();
if (guesses[i] == code) {
System.out.println("Good guess! You won.");
System.out.println((i+1) +" guesses");
i++;
for (int k=0; k<i; k++) {
for (int j=0; j<100; j++)
{
if (j == guesses[k]) {
System.out.print("X");
}
else if (j == code) {
System.out.print("|");
}
else {
System.out.print(".");
}
}
System.out.println("");
}
}
else if (code < guesses[i] && i != 6) {
System.out.println("lower");
i++;
}
else if (code > guesses[i] && i != 6) {
System.out.println("higher");
i++;
}
else {
System.out.println("No more guesses, you lost");
System.out.println((i+1) + " guesses");
for (int k=0; k<=i; k++) {
for (int j=0; j<100; j++)
{
if (j == guesses[k]) {
System.out.print("X");
}
else if (j == code) {
System.out.print("|");
}
else {
System.out.print(".");
}
}
System.out.println("");
}
}
}
scanner.close();
}
}

Related

java.util.NoSuchElementException error on multiple while statements

Screenshot of error message
Getting this error when I run my code, note that it finds a problem at line 37, but I cannot figure out what it is. Running the first iteration of the scanner method (for input 1) worked fine, and yielded the proper output, but none of the consecutive ones have, and I've been stuck on that issue. Code is below:
import java.io.FileNotFoundException;
import java.io.File;
import java.util.Scanner;
public class Assignment2 {
public static void main(String[] args) {
int largest= 0;
int largestEven = 0;
int countOfAllPositive = 0;
int sumOfAll = 0;
try {
Scanner input1 = new Scanner(new File("input1.txt"));
while (input1.hasNextInt()) {
if (input1.nextInt() == 0)
{ break;
} else if (input1.nextInt() < largest)
{ largest = input1.nextInt();
} else if (input1.nextInt() > 0)
{ largestEven += input1.nextInt();
} else if (input1.nextInt() % 2 == 0)
{ countOfAllPositive += input1.nextInt();
} else if (input1.nextInt() < 0)
{ sumOfAll++;
}
}
Scanner input2 = new Scanner(new File("input2.txt"));
while (input2.hasNextInt()) {
if (input2.nextInt() == 0)
{ break;
} else if (input2.nextInt() < largest)
{ largest = input2.nextInt();
} else if (input2.nextInt() > 0)
{ largestEven += input2.nextInt();
} else if (input2.nextInt() % 2 == 0)
{ countOfAllPositive += input2.nextInt();
} else if (input2.nextInt() < 0)
{ sumOfAll++;
}
}
Scanner input3 = new Scanner(new File("input3.txt"));
while (input3.hasNextInt()) {
if (input3.nextInt() == 0)
{ break;
} else if (input3.nextInt() < largest)
{ largest = input3.nextInt();
} else if (input3.nextInt() > 0)
{ largestEven += input3.nextInt();
} else if (input3.nextInt() % 2 == 0)
{ countOfAllPositive += input3.nextInt();
} else if (input3.nextInt() < 0)
{ sumOfAll++;
}
}
Scanner input4 = new Scanner(new File("input4.txt"));
while (input4.hasNextInt()) {
if (input4.nextInt() == 0)
{ break;
} else if (input4.nextInt() < largest)
{ largest = input4.nextInt();
} else if (input4.nextInt() > 0)
{ largestEven += input4.nextInt();
} else if (input4.nextInt() % 2 == 0)
{ countOfAllPositive += input4.nextInt();
} else if (input4.nextInt() < 0)
{ sumOfAll++;
}
}
} catch (FileNotFoundException e) {
System.out.println("An error occurred.");
e.printStackTrace(); }
System.out.println("The largest integer in the sequence is " + largest);
System.out.println("The largest even integer in the sequence is " + largestEven);
System.out.println("The count of all positive integers in the sequence is " + countOfAllPositive);
System.out.println("The sum of all integers is " + sumOfAll);
}
}
The exception NoSuchElementException in your case is caused by trying to use nextInt() on a file/input that has no more int's left to read, see the Javadoc:
Thrown by various accessor methods to indicate that the element being
requested does not exist.
The reason you get the issue is because every time you call nextInt() in actually uses up an int and moves onto the next one.
So instead of this where you call nextInt() over and over in the if/else if:
if (input1.nextInt() == 0) //Gets the next int
{ break;
} else if (input1.nextInt() < largest) //Gets the next int (different from the previous)
{ largest = input1.nextInt(); //Gets the next int (different from the previous)
... //And so on
You need to do call nextInt() once and assign it to a value then use that value:
//Store the int as a value
int value = input1.nextInt();
//Use the value instead of calling "nextInt()" again
if (value == 0)
{ break;
} else if (value < largest)
{ largest = value;
...

.noSuchElementException when asking the user for input

I can't run the following piece of code. I would like to know why I keep getting the .noSuchElementException error. I've seen in another post is due to the fact I'm using the same Input stream for both inputs, but creating a new Scanner or using the .close method doesn't seem to fix my problem.
class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner (System.in);
int tooBig = 0; // parts too big
int tooSmall = 0; // parts too small
int perfectParts= 0; // perfect parts
int a = scanner.nextInt();
scanner.close();
for (int i = 0; i <= a ; i++) {
int j = scanner.nextInt();
if(j == 1) {
tooBig++;
} else if (j == -1) {
tooSmall++;
} else if (j == 0) {
perfectParts++;
}
scanner.close();
}
System.out.println(perfectParts + " " + tooBig
+ " " + tooSmall);
}
}
Edit after having removed the scanner.close() method. I still get the same error:
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner (System.in);
int tooBig = 0; // parts too big
int tooSmall = 0; // parts too small
int perfectParts= 0; // perfect parts
int a = scanner.nextInt();
for (int i = 0; i <= a ; i++) {
int j = scanner.nextInt();
if(j == 1) {
tooBig++;
} else if (j == -1) {
tooSmall++;
} else if (j == 0) {
perfectParts++;
}
}
System.out.println(perfectParts + " " + tooBig
+ " " + tooSmall);
}
}
class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int tooBig = 0; // parts too big
int tooSmall = 0; // parts too small
int perfectParts= 0; // perfect parts
int a = scanner.nextInt();
for (int i = 0; i <= a ; i++) {
int j = scanner.nextInt();
if(j == 1) {
tooBig++;
} else if (j == -1) {
tooSmall++;
} else if (j == 0) {
perfectParts++;
}
}
System.out.println(perfectParts + " " + tooBig
+ " " + tooSmall);
scanner.close();
}
}
scanner.close(); Should only be called at the very end. It's also not even necessary in your specific case and likely the reason why you're getting that exception.

How can I update the data inside of an array to be false instead of true?

This program is to reserve time available from three different games. So far in the program, I had printed the two different menus on that shows the game and the other one shows the available time Slots. the Program is set to be an infinite loop so other users have the chance to pick a game and reserve time as well. This is where my problem comes, the second time the program runs it stills displays the time that was previously picked.
import java.util.Scanner;
public class GameReservationSystem {
public static Scanner input = new Scanner(System.in);
static String[] timeSlots = {"1-2pm", "2-3pm", "3-4pm", "4-5pm"};
static String[] Games = {"Backganmon", "Chess", "Dominoes"};
static boolean avaliableTime[][] = new boolean[Games.length][timeSlots.length];
static int userChoice;
public static void main(String[] args) {
for (int i = 0; i < Games.length; i++) {
for (int j = 0; j < timeSlots.length; j++) {
avaliableTime[i][j] = true;
}
}
mainMenu();
}
public static void mainMenu() {
while (true) {
characterPrint('-');
for (int i = 0; i < Games.length; i++) {
System.out.print(i + ". ");
System.out.println(Games[i]);
}
characterPrint('-');
System.out.println("input a number to choose your game!");
userChoice = input.nextInt();
reserveMenu(userChoice);
}
}
public static void reserveMenu(int userChoice) {
int reserveTime = 0;
if (userChoice >= 0 && userChoice < Games.length) {
characterPrint('-');
System.out.println("This are the avaliable times to play" + Games[userChoice]);
for (int i = 0; i < timeSlots.length; i++) {
System.out.print(i + ". ");
System.out.println(timeSlots[i]);
}
characterPrint('-');
System.out.println("Input a number to reserve a time: ");
reserveTime = input.nextInt();
}
if (reserveTime >= 0 && reserveTime < timeSlots.length) {
avaliableTime[userChoice][reserveTime] = false;
System.out.println("You have succesfully reserve a time to play " + Games[userChoice] + " at" + timeSlots[reserveTime]);
}
}
public static void characterPrint(char c) {
for (int i = 0; i < 30; i++) {
System.out.print(c);
}
System.out.println();
}
}
In fact, your problem isn't that you didn't change the boolean values in your table from true to false. Instead, your problem is that you didn't use information in the table before you make reservations. What you were doing is to always set that value to false and print "succesfully reserve", without checking whether that time has already been reserved by someone else.
Try the following code. I also add some codes to handle invalid input number.
By the way, "avaliableTime" looks like a typo, so I changed it to "availableTime".
import java.util.Scanner;
public class GameReservationSystem {
public static Scanner input = new Scanner(System.in);
static String[] timeSlots = {"1-2pm", "2-3pm", "3-4pm", "4-5pm"};
static String[] Games = {"Backganmon", "Chess", "Dominoes"};
static boolean availableTime[][] = new boolean[Games.length][timeSlots.length];
static int userChoice;
public static void main(String[] args) {
for (int i = 0; i < Games.length; i++) {
for (int j = 0; j < timeSlots.length; j++) {
availableTime[i][j] = true;
}
}
mainMenu();
}
public static void mainMenu() {
while (true) {
characterPrint('-');
for (int i = 0; i < Games.length; i++) {
System.out.print(i + ". ");
System.out.println(Games[i]);
}
characterPrint('-');
System.out.println("input a number to choose your game!");
userChoice = input.nextInt();
reserveMenu(userChoice);
}
}
public static void reserveMenu(int userChoice) {
int reserveTime = 0;
if (userChoice >= 0 && userChoice < Games.length) {
characterPrint('-');
System.out.println("This are the avaliable times to play" + Games[userChoice]);
for (int i = 0; i < timeSlots.length; i++) {
if (availableTime[userChoice][i]) {
//if false, this time is not available and should not be displayed.
System.out.print(i + ". ");
System.out.println(timeSlots[i]);
}
}
characterPrint('-');
System.out.println("Input a number to reserve a time: ");
reserveTime = input.nextInt();
}else {
//handle unexpected game number.
System.out.println("Invalid game number, please try again.");
}
if (reserveTime >= 0 && reserveTime < timeSlots.length) {
//what if someone typed 0 although 0 wasn't displayed? (0 wasn't displayed, for example, because it has been reserved by someone else.)
//So you should check whether it is available by the following line.
if (availableTime[userChoice][reserveTime]) {
//if true, this time hasn't been reserved, so it is a valid input.
availableTime[userChoice][reserveTime] = false;
System.out.println("You have succesfully reserve a time to play " + Games[userChoice] + " at" + timeSlots[reserveTime]);
}else {
//if already false, this time has already been reserved by someone else,
//thus it is an invalid input.
System.out.println("This time has already been reserved, please try again.");
}
}else {
System.out.println("Invalid time number, please try again.");
}
}
public static void characterPrint(char c) {
for (int i = 0; i < 30; i++) {
System.out.print(c);
}
System.out.println();
}
}

set each array value with input

I want make like this:
input number[0][0]=201
input number[0][1]=202
input number[1][0]=203
input number[1][1]=204
input last = 203
then find if last input same with above, if true, s.o.p find, else not found
my code:
import java.util.Scanner;
public class array_input {
public static void main(String[] args) {
int a[][];
Scanner scan = new Scanner(System.in);
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
System.out.print("input number[" + i + "][" + j + "]");
int b = scan.nextInt();
a[i][j] = b;
}
}
System.out.print("input what u want");
if (a[i][j] == b) {
System.out.print("found");
} else {
System.out.print("not found");
}
}
}
Maybe you mean something like this ?
import java.util.Scanner;
public class array_input
{
public static void main(String [] args){
int a [][] = new int[2][2];
Scanner scan = new Scanner(System.in);
for(int i = 0;i < 2; i++){
for(int j = 0; j < 2; j++){
System.out.printf("input number[%d][%d]=", i, j);
int b = scan.nextInt();
a[i][j]=b;
}
}
System.out.print("input last = ");
int needle = scan.nextInt();
for (int[] row : a){
for (int col : row){
if(col == needle){
System.out.println("found");
return;
}
}
}
System.out.print("not found");
}
}
Ok, I guess that this is what you want. This checks if the last input of the array is in the array (excluding the last input).
boolean valueInArray = false;
for(int i=0;i<2;i++){
for(int j=0;j<2;j++){
if(a[i][j]==b && (i != 2 || j != 2)){
valueInArray = true;
}
}
}
if(valueInArray){
System.out.print("found");
} else {
System.out.print("not found");
}

Loop the program until 0(exit)(java)

I have this java code that basically just prints a christmas tree of X height.However, the program ask for the number, then print the tree and then just end.I would like it to loop until I enter 0,wich would end the program,and also I would like to make it print only if the number entered is from 1-40(not over 40).Im begining in the java world and I dont know how to do that.Heres my code for now:
public class xtree {
public static void main(String[] args)
{
Scanner scan = new Scanner(in);
out.print("please enter a number: ");
int temp = scan.nextInt();
int x = (temp-1)*2 +1;
int y = x/2;
int z = 1;
for(int i=0; i<temp-1; i++)
{
for(int j=0; j<=y; j++)
{
out.print(" ");
}
for(int k = 0; k<z; k++)
{
out.print("*");
}
out.println();
y--;
z+=2;
}
for(int i =0; i<=x/2; i++)
{
out.print(" ");
}
out.println("*");
}
}
Thank you, im a beginner to java so please be lenient ;)
Well, I would separate the method out into two:
A printChristmasTree method, which accepts the height as a parameter
Your main method, which just deals with taking user input and calling printChristmasTree or exiting
Most of your current main method would go into the printChristmasTree, and main would be a loop. Something like:
public static void main(String[] args) {
Scanner scan = new Scanner(in);
while (true) {
System.out.print("Please enter a number (0-40): ");
int height = scan.nextInt();
if (height == 0) {
// Exit the program
return;
} else if (height >= 1 && height <= 40) {
printChristmasTree(height);
} else {
System.out.println("Invalid input.");
}
}
}
There are other approaches you could use instead of returning from a while (true) loop, but this looks the simplest to me.
The separation of the "taking input" from the "printing the Christmas tree" aspects leads to much more readable code than keeping them combined, in my view - and it's more flexible in terms of things like writing a different program to print all valid Christmas trees.
Use a while loop:
Scanner scan = new Scanner(System.in);
System.out.print("please enter a number: ");
int temp = scan.nextInt();
while (temp>0) {
int x = (temp-1)*2 +1;
int y = x/2;
int z = 1;
for(int i=0; i<temp-1; i++)
{
for(int j=0; j<=y; j++)
{
System.out.print(" ");
}
for(int k = 0; k<z; k++)
{
System.out.print("*");
}
System.out.println();
y--;
z+=2;
}
for(int i =0; i<=x/2; i++)
{
System.out.print(" ");
}
System.out.println("*");
temp = scan.nextInt();
}
Here's the code for doing that:
public class xtree {
public static void main(String[] args)
{
int temp;
do{
Scanner scan = new Scanner(in);
out.print("please enter a number: ");
temp = scan.nextInt();
if(temp >= 1 && temp <= 40){ //don't display a tree higher than 40
int x = (temp-1)*2 +1;
int y = x/2;
int z = 1;
for(int i=0; i<temp-1; i++)
{
for(int j=0; j<=y; j++)
{
out.print(" ");
}
for(int k = 0; k<z; k++)
{
out.print("*");
}
out.println();
y--;
z+=2;
}
for(int i =0; i<=x/2; i++)
{
out.print(" ");
}
out.println("*");
}else if(temp != 0){
out.print("Please enter a number between 1 and 40!");
}
}while(temp != 0);
}
}

Categories

Resources