簡體   English   中英

我在 Java 中遇到 InputMismatchException 問題

[英]I am having trouble with InputMismatchException in Java

我無法運行我的代碼,因為它在線程“主”java.util.InputMismatchException 中給了我異常。 你能幫幫我嗎? 這是代碼:

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution1 {
    public static void main(String args[] ) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT */
        int id;
        String title;
        String author;
        double price;
        Scanner sc=new Scanner(System.in);
        Book [] book =new Book[4];
       // try{
        for(int i=0;i<book.length;i++){
            id=sc.nextInt();
            sc.hasNextLine();
            title=sc.nextLine();
            author=sc.nextLine();
            price=sc.nextDouble();
            book[i]=new Book(id,title,author,price);
        }
        sc.close();
        Book[] res=sortBookByPrice(book);
        if(res!=null){
            for(int i=0;i<res.length;i++){
                System.out.println(res[i].getId()+res[i].getTitle()+res[i].getAuthor()+res[i].getPrice());
            }
        }//}
        //catch(InputMismatchException e){
        //    System.out.println(e);
        //}
    }
    public static Book[] sortBookByPrice(Book[] book){
        for(int i=0;i<book.length;i++){
            if(book[i].getPrice()>book[i+1].getPrice()){
                Book temp=book[i];
                book[i]=book[i+1];
                book[i+1]=temp;
            }
        }
        return book;
    }
}

class Book
{
    private int id;
    private String title;
    private String author;
    private double price;
    Book(int id,String title,String author,double price){
        this.id=id;
        this.title=title;
        this.author=author;
        this.price=price;
    }
    public int getId(){
        return id;
    }
    public void setId(int id){
        this.id=id;
    }
    public String getTitle(){
        return title;
    }
    public void setTitle(String title){
        this.title=title;
    }
    public String getAuthor(){
        return author;
    }
    public void setAuthor(String author){
        this.author=author;
    }
    public double getPrice(){
        return price;
    }
    public void setPrice(double price){
        this.price=price;
    }
}

這是我得到的錯誤

Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Unknown Source)
    at java.util.Scanner.next(Unknown Source)
    at java.util.Scanner.nextDouble(Unknown Source)
    at Solution1.main(Solution1.java:23)

我用它來按升序顯示 output 但是在寫完作者后按下回車鍵,代碼給了我錯誤。 請幫忙。

這是我正在使用的輸入...

1
hello
writer1
50
2
cup
writer2
55
3
Planet
writer3
45
4
India
writer4
40

您可以使用 sc.next( sc.next()而不是sc.nextLine()來輸入標題和作者,這將解決您的問題。 除了將減少循環迭代器排序為book.length-1 for(int i=0;i<book.length-1;i++){

您的程序將開始工作。

我在下面粘貼了工作代碼:

public static void main(String args[] ) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT */
        int id;
        String title;
        String author;
        double price;
        Scanner sc=new Scanner(System.in);
        Book [] book =new Book[4];
        // try{
        for(int i=0;i<book.length;i++){
            id=sc.nextInt();
            sc.nextLine();//to discard line containing int value
            sc.hasNextLine();
            title=sc.nextLine();
            author=sc.nextLine();
            price=sc.nextDouble();
            sc.nextLine();//to discard line containing double value
            book[i]=new Book(id,title,author,price);
        }
        sc.close();
        Book[] res=sortBookByPrice(book);
        if(res!=null){
            for(int i=0;i<res.length;i++){
                System.out.println(res[i].getId()+res[i].getTitle()+res[i].getAuthor()+res[i].getPrice());
            }
        }//}
        //catch(InputMismatchException e){
        //    System.out.println(e);
        //}
    }
    public static Book[] sortBookByPrice(Book[] book){
        Arrays.sort(book, Comparator.comparing(Book::getPrice));//Java 8
        return book;
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM