我的代码中出现 NameFormatException 并且跳过了代码的特定部分?
I am getting NameFormatException in my code and a certain part of code is being skipped?
我已经编写了一个程序来接受两个学生的数据,但是当我 运行 这段代码时, 'name' 部分第二次被跳过并且它从主题开始并给出 NameformatException?
第一个 运行 代码是正常的,但是当它进入第二个 运行 时,名称部分被跳过,我必须直接输入整数值,如果我不输入,我会得到 NumberFormatException。
import java.util.Scanner;
@SuppressWarnings("serial")
class NegativeValueException extends Exception {
public NegativeValueException() {
}
}
@SuppressWarnings("serial")
class ValueOutOfRange extends Exception{
public ValueOutOfRange() {
System.out.println("Value out of range Exception");
}
}
public class Student1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
for(int i=0;i<2;i++) {
String[] name = new String[2];
int sub1=0;
int sub2=0;
int sub3=0;
double avg;
try {
System.out.println("Enter the data of Student "+(i+1)+" : ");
name[i] = sc.nextLine();
if(name[i].equals(null))
break;
else
if(sc.hasNextInt())
sub1=sc.nextInt();
else
throw new NumberFormatException();
if(sc.hasNextInt())
sub2=sc.nextInt();
else
throw new NumberFormatException();
if(sc.hasNextInt())
sub3=sc.nextInt();
else
throw new NumberFormatException();
if(sub1<0)throw new NegativeValueException();
if(sub1>100)throw new ValueOutOfRange();
if(sub2<0)throw new NegativeValueException();
if(sub2>100)throw new ValueOutOfRange();
if(sub3<0)throw new NegativeValueException();
if(sub3>100)throw new ValueOutOfRange();
avg = (sub1+sub2+sub3)/3;
System.out.println("Name : "+name[i]);
System.out.println("Marks in subject 1 : "+sub1);
System.out.println("Marks in subject 2 : "+sub2);
System.out.println("Marks in subject 3 : "+sub3);
System.out.println("Average Marks : "+avg);
}catch(NumberFormatException e) {
System.out.println(e);
}catch(NegativeValueException e) {
System.out.println(e);
}catch(ValueOutOfRange e) {
System.out.println(e);
}
}
sc.close();
}
}
问题出现在读取输入的第一个循环之后,因为在扫描仪 class 的最后一个 scan.nextInt() 方法之后,光标停留在同一条线。因此,当您使用 nextLine() 作为名称时,您的输入将被视为输入,当您输入名称时,它会显示 NFE。 [
要解决这个问题,您可以做的是在完成最后一个整数输入后,使用 nextLine() 处理剩余的行,
那么你的代码可以正常工作,
import java.util.Scanner;
@SuppressWarnings("serial")
class NegativeValueException extends Exception {
public NegativeValueException() {
}
}
@SuppressWarnings("serial")
class ValueOutOfRange extends Exception {
public ValueOutOfRange() {
System.out.println("Value out of range Exception");
}
}
public class Student1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
for (int i = 0; i < 2; i++) {
String[] name = new String[2];
int sub1 = 0;
int sub2 = 0;
int sub3 = 0;
double avg;
try {
System.out.println("Enter the data of Student " + (i + 1) + " : ");
name[i] = sc.nextLine();
// sc.next();
if (name[i].equals(null))
break;
if (sc.hasNextInt())
sub1 = sc.nextInt();
else
throw new NumberFormatException();
if (sc.hasNextInt())
sub2 = sc.nextInt();
else
throw new NumberFormatException();
if (sc.hasNextInt())
sub3 = sc.nextInt();
else
throw new NumberFormatException();
if (sub1 < 0)
throw new NegativeValueException();
if (sub1 > 100)
throw new ValueOutOfRange();
if (sub2 < 0)
throw new NegativeValueException();
if (sub2 > 100)
throw new ValueOutOfRange();
if (sub3 < 0)
throw new NegativeValueException();
if (sub3 > 100)
throw new ValueOutOfRange();
avg = (sub1 + sub2 + sub3) / 3;
System.out.println("Name : " + name[i]);
System.out.println("Marks in subject 1 : " + sub1);
System.out.println("Marks in subject 2 : " + sub2);
System.out.println("Marks in subject 3 : " + sub3);
System.out.println("Average Marks : " + avg);
sc.nextLine(); // add this
} catch (NumberFormatException e) {
System.out.println(e);
} catch (NegativeValueException e) {
System.out.println(e);
} catch (ValueOutOfRange e) {
System.out.println(e);
}
}
sc.close();
}
}
输出为
Enter the data of Student 1 :
john
12
32
41
Name : john
Marks in subject 1 : 12
Marks in subject 2 : 32
Marks in subject 3 : 41
Average Marks : 28.0
Enter the data of Student 2 :
jack
12
43
55
Name : jack
Marks in subject 1 : 12
Marks in subject 2 : 43
Marks in subject 3 : 55
Average Marks : 36.0
由于@Gurkirat 已经指出了错误,我只是添加我的答案作为对您代码的改进。
import java.util.Scanner;
@SuppressWarnings("serial")
class NegativeValueException extends Exception {
public NegativeValueException() {
System.out.println("Negative Value Exception");
}
}
@SuppressWarnings("serial")
class ValueOutOfRange extends Exception {
public ValueOutOfRange() {
System.out.println("Value out of range Exception");
}
}
public class Student1 {
public static void main(String[] args) {
String[] name = new String[2];
for(int i=0; i<2; i++) {
int sub1, sub2, sub3;
double avg;
try (Scanner sc = new Scanner(System.in)) {
System.out.println("Enter the data of Student "+ (i+1) +" : ");
name[i] = sc.nextLine();
sub1 = sc.nextInt();
sub2 = sc.nextInt();
sub3 = sc.nextInt();
sc.next(); //Added this to solve your problem.
if(sub1 < 0 || sub2 < 0 || sub3 < 0)
throw new NegativeValueException();
if(sub1 > 100 || sub2 > 100 || sub3 > 100)
throw new ValueOutOfRange();
avg = (sub1+sub2+sub3)/3;
System.out.println("Name : " + name[i] +
"\nMarks in subject 1 : " + sub1 +
"\nMarks in subject 2 : " + sub2 +
"\nMarks in subject 3 : "+sub3 +
"\nAverage Marks : " + avg);
} catch(ValueOutOfRange | NegativeValueException e) {
System.out.println(e);
}
}
}
}
我已经编写了一个程序来接受两个学生的数据,但是当我 运行 这段代码时, 'name' 部分第二次被跳过并且它从主题开始并给出 NameformatException? 第一个 运行 代码是正常的,但是当它进入第二个 运行 时,名称部分被跳过,我必须直接输入整数值,如果我不输入,我会得到 NumberFormatException。
import java.util.Scanner;
@SuppressWarnings("serial")
class NegativeValueException extends Exception {
public NegativeValueException() {
}
}
@SuppressWarnings("serial")
class ValueOutOfRange extends Exception{
public ValueOutOfRange() {
System.out.println("Value out of range Exception");
}
}
public class Student1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
for(int i=0;i<2;i++) {
String[] name = new String[2];
int sub1=0;
int sub2=0;
int sub3=0;
double avg;
try {
System.out.println("Enter the data of Student "+(i+1)+" : ");
name[i] = sc.nextLine();
if(name[i].equals(null))
break;
else
if(sc.hasNextInt())
sub1=sc.nextInt();
else
throw new NumberFormatException();
if(sc.hasNextInt())
sub2=sc.nextInt();
else
throw new NumberFormatException();
if(sc.hasNextInt())
sub3=sc.nextInt();
else
throw new NumberFormatException();
if(sub1<0)throw new NegativeValueException();
if(sub1>100)throw new ValueOutOfRange();
if(sub2<0)throw new NegativeValueException();
if(sub2>100)throw new ValueOutOfRange();
if(sub3<0)throw new NegativeValueException();
if(sub3>100)throw new ValueOutOfRange();
avg = (sub1+sub2+sub3)/3;
System.out.println("Name : "+name[i]);
System.out.println("Marks in subject 1 : "+sub1);
System.out.println("Marks in subject 2 : "+sub2);
System.out.println("Marks in subject 3 : "+sub3);
System.out.println("Average Marks : "+avg);
}catch(NumberFormatException e) {
System.out.println(e);
}catch(NegativeValueException e) {
System.out.println(e);
}catch(ValueOutOfRange e) {
System.out.println(e);
}
}
sc.close();
}
}
问题出现在读取输入的第一个循环之后,因为在扫描仪 class 的最后一个 scan.nextInt() 方法之后,光标停留在同一条线。因此,当您使用 nextLine() 作为名称时,您的输入将被视为输入,当您输入名称时,它会显示 NFE。 [
要解决这个问题,您可以做的是在完成最后一个整数输入后,使用 nextLine() 处理剩余的行, 那么你的代码可以正常工作,
import java.util.Scanner;
@SuppressWarnings("serial")
class NegativeValueException extends Exception {
public NegativeValueException() {
}
}
@SuppressWarnings("serial")
class ValueOutOfRange extends Exception {
public ValueOutOfRange() {
System.out.println("Value out of range Exception");
}
}
public class Student1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
for (int i = 0; i < 2; i++) {
String[] name = new String[2];
int sub1 = 0;
int sub2 = 0;
int sub3 = 0;
double avg;
try {
System.out.println("Enter the data of Student " + (i + 1) + " : ");
name[i] = sc.nextLine();
// sc.next();
if (name[i].equals(null))
break;
if (sc.hasNextInt())
sub1 = sc.nextInt();
else
throw new NumberFormatException();
if (sc.hasNextInt())
sub2 = sc.nextInt();
else
throw new NumberFormatException();
if (sc.hasNextInt())
sub3 = sc.nextInt();
else
throw new NumberFormatException();
if (sub1 < 0)
throw new NegativeValueException();
if (sub1 > 100)
throw new ValueOutOfRange();
if (sub2 < 0)
throw new NegativeValueException();
if (sub2 > 100)
throw new ValueOutOfRange();
if (sub3 < 0)
throw new NegativeValueException();
if (sub3 > 100)
throw new ValueOutOfRange();
avg = (sub1 + sub2 + sub3) / 3;
System.out.println("Name : " + name[i]);
System.out.println("Marks in subject 1 : " + sub1);
System.out.println("Marks in subject 2 : " + sub2);
System.out.println("Marks in subject 3 : " + sub3);
System.out.println("Average Marks : " + avg);
sc.nextLine(); // add this
} catch (NumberFormatException e) {
System.out.println(e);
} catch (NegativeValueException e) {
System.out.println(e);
} catch (ValueOutOfRange e) {
System.out.println(e);
}
}
sc.close();
}
}
输出为
Enter the data of Student 1 :
john
12
32
41
Name : john
Marks in subject 1 : 12
Marks in subject 2 : 32
Marks in subject 3 : 41
Average Marks : 28.0
Enter the data of Student 2 :
jack
12
43
55
Name : jack
Marks in subject 1 : 12
Marks in subject 2 : 43
Marks in subject 3 : 55
Average Marks : 36.0
由于@Gurkirat 已经指出了错误,我只是添加我的答案作为对您代码的改进。
import java.util.Scanner;
@SuppressWarnings("serial")
class NegativeValueException extends Exception {
public NegativeValueException() {
System.out.println("Negative Value Exception");
}
}
@SuppressWarnings("serial")
class ValueOutOfRange extends Exception {
public ValueOutOfRange() {
System.out.println("Value out of range Exception");
}
}
public class Student1 {
public static void main(String[] args) {
String[] name = new String[2];
for(int i=0; i<2; i++) {
int sub1, sub2, sub3;
double avg;
try (Scanner sc = new Scanner(System.in)) {
System.out.println("Enter the data of Student "+ (i+1) +" : ");
name[i] = sc.nextLine();
sub1 = sc.nextInt();
sub2 = sc.nextInt();
sub3 = sc.nextInt();
sc.next(); //Added this to solve your problem.
if(sub1 < 0 || sub2 < 0 || sub3 < 0)
throw new NegativeValueException();
if(sub1 > 100 || sub2 > 100 || sub3 > 100)
throw new ValueOutOfRange();
avg = (sub1+sub2+sub3)/3;
System.out.println("Name : " + name[i] +
"\nMarks in subject 1 : " + sub1 +
"\nMarks in subject 2 : " + sub2 +
"\nMarks in subject 3 : "+sub3 +
"\nAverage Marks : " + avg);
} catch(ValueOutOfRange | NegativeValueException e) {
System.out.println(e);
}
}
}
}