线程“main”中的异常 java.util.InputMismatchException
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21143028/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Exception in thread "main" java.util.InputMismatchException
提问by tonyhlav
i need help with one exercise in java, i'm stuck on this error 2 hours maybe. Any help would be great.
我需要帮助进行一项 Java 练习,我可能在这个错误上停留了 2 小时。任何帮助都会很棒。
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:909)
at java.util.Scanner.next(Scanner.java:1530)
at java.util.Scanner.nextInt(Scanner.java:2160)
at java.util.Scanner.nextInt(Scanner.java:2119)
at prodavnica.Prodavnica.main(Prodavnica.java:60)
Java Result: 1
package prodavnica;
public class Proizvod {
private String ime_proizvod;
private static int cena;
public Proizvod(String ime_proizvod, int cena) {
this.ime_proizvod = ime_proizvod;
this.cena=cena;
}
public String getIme_proizvod() {
return ime_proizvod;
}
public void setIme_proizvod(String ime_proizvod) {
this.ime_proizvod = ime_proizvod;
}
public static int getCena() {
return cena;
}
public static void setCena(int cena) {
Proizvod.cena = cena;
}
public void pecatiPodatoci(){
System.out.println("Ime: "+ime_proizvod+" Cena: "+cena);
}
}
AND:
和:
package prodavnica;
import java.util.Scanner;
public class Prodavnica {
private String ime_prodavnica;
private Proizvod proizvodi[]=new Proizvod[20];
public Prodavnica(String ime_prodavnica) {
this.ime_prodavnica = ime_prodavnica;
}
int br=0;
public void dodadiProizvod(Proizvod p){
proizvodi[br]=p;
br++;
}
public Proizvod najskapProizvod(){
Proizvod max=proizvodi[0];
for(int r=0;r<proizvodi.length;r++){
if(max.getCena()<proizvodi[r+1].getCena()){
max=proizvodi[r+1];
}
}
return max;
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Prodavnica pro1=new Prodavnica("Tinex");
int n;
System.out.println("Vnesete kolku proizvodi ke stavite: ");
n=input.nextInt();
String imer = input.nextLine();
int cenar = input.nextInt();
pro1.dodadiProizvod(new Proizvod(imer, cenar));
System.out.println("Ime-pr: "+pro1.proizvodi[0].getIme_proizvod()+" Cena= "+pro1.proizvodi[0].getCena());
}
}
I can't enter The string "imer" or the int "cenar" on the variable "proizvodi" from the class Proizvod.
我无法在 Proizvod 类中的变量“proizvodi”上输入字符串“imer”或 int“cenar”。
Any Help? why i get this error? Thanks!
任何帮助?为什么我收到这个错误?谢谢!
采纳答案by Raju Rathore
This Exception Thrown by a Scanner to indicate that the token retrieved does not match the pattern for the expected type, or that the token is out of range for the expected type.
扫描仪抛出的此异常表示检索到的令牌与预期类型的模式不匹配,或者令牌超出预期类型的范围。
String imer = input.next();// Use for String Input
input.nextLine();//Use for next line of input
int cenar = input.nextInt();
回答by M21B8
You need to put an int in before you get to imer or cenar:
在进入 imer 或 cenar 之前,您需要输入一个 int:
n=input.nextInt();
This line doesn't appear to be doing anything, either remove it, or put a number in before you put your imer or cenar values in.
在您输入 imer 或 cenar 值之前,这条线似乎没有做任何事情,要么删除它,要么输入一个数字。