-
Bug
-
Resolution: Not an Issue
-
P4
-
None
-
8, 11, 16, 18
ADDITIONAL SYSTEM INFORMATION :
Windows 10/JDK 16
A DESCRIPTION OF THE PROBLEM :
When I open a Scanner object after I have close another Scanner object declared in another function, method nextInt throws NoSuchElementException
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Run the functions
static void k5_14() {
int i;
int sum=0;
Scanner in=new Scanner(System.in);
do {
System.out.print("Enter int (0 terminates input):");
i=in.nextInt();
sum+=i;
} while (i!=0);
in.close();
System.out.println("Sum = "+sum);
}
static void k5_22() {
int sum=0;
Scanner input=new Scanner(System.in);
for (int i=1;i!=0;) {
System.out.print("Enter int (0 terminates input):");
i=input.nextInt();
sum+=i;
}
input.close();
System.out.println("Sum = "+sum);
}
like this
k5_22();
System.out.println("-----------------------------------");
k5_14();
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
The sum of a series of integers given by the user
ACTUAL -
NoSuchElementException
---------- BEGIN SOURCE ----------
import java.util.Scanner;
public class Tmp {
static void k5_14() {
int i;
int sum=0;
Scanner in=new Scanner(System.in);
do {
System.out.print("Enter int (0 terminates input):");
i=in.nextInt();
sum+=i;
} while (i!=0);
in.close();
System.out.println("Sum = "+sum);
}
static void k5_22() {
int sum=0;
Scanner input=new Scanner(System.in);
for (int i=1;i!=0;) {
System.out.print("Enter int (0 terminates input):");
i=input.nextInt();
sum+=i;
}
input.close();
System.out.println("Sum = "+sum);
}
public static void main(String[] args) {
k5_14();
k5_22();
}
}
---------- END SOURCE ----------
FREQUENCY : always
Windows 10/JDK 16
A DESCRIPTION OF THE PROBLEM :
When I open a Scanner object after I have close another Scanner object declared in another function, method nextInt throws NoSuchElementException
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Run the functions
static void k5_14() {
int i;
int sum=0;
Scanner in=new Scanner(System.in);
do {
System.out.print("Enter int (0 terminates input):");
i=in.nextInt();
sum+=i;
} while (i!=0);
in.close();
System.out.println("Sum = "+sum);
}
static void k5_22() {
int sum=0;
Scanner input=new Scanner(System.in);
for (int i=1;i!=0;) {
System.out.print("Enter int (0 terminates input):");
i=input.nextInt();
sum+=i;
}
input.close();
System.out.println("Sum = "+sum);
}
like this
k5_22();
System.out.println("-----------------------------------");
k5_14();
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
The sum of a series of integers given by the user
ACTUAL -
NoSuchElementException
---------- BEGIN SOURCE ----------
import java.util.Scanner;
public class Tmp {
static void k5_14() {
int i;
int sum=0;
Scanner in=new Scanner(System.in);
do {
System.out.print("Enter int (0 terminates input):");
i=in.nextInt();
sum+=i;
} while (i!=0);
in.close();
System.out.println("Sum = "+sum);
}
static void k5_22() {
int sum=0;
Scanner input=new Scanner(System.in);
for (int i=1;i!=0;) {
System.out.print("Enter int (0 terminates input):");
i=input.nextInt();
sum+=i;
}
input.close();
System.out.println("Sum = "+sum);
}
public static void main(String[] args) {
k5_14();
k5_22();
}
}
---------- END SOURCE ----------
FREQUENCY : always