0

I am having trouble running my code as it is giving me Exception in thread "main" java.util.InputMismatchException. Can you please help me out. Here is the code:

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;
    }
}

This is the error I am getting

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)

I am using this to show the output in ascending order but as soon as I press enter after writing the author, the code is giving me the error. Please help.

This is the input that I am using...

1
hello
writer1
50
2
cup
writer2
55
3
Planet
writer3
45
4
India
writer4
40
jhamon
  • 3,346
  • 3
  • 23
  • 35
  • 1
    Do you know the difference between `hasNextLine` and `nextLine`? – RealSkeptic Jul 01 '20 at 12:13
  • Does this answer your question? [Scanner is skipping nextLine() after using next() or nextFoo()?](https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-or-nextfoo) . I'm guessing there is an offset after the first loop iteration, so it tries to read a double but get a String instead – jhamon Jul 01 '20 at 12:29

1 Answers1

-1

You can use sc.next() instead of sc.nextLine() for title and author input and this will solve your issue. Apart from that in sorting decrease loop iterator to book.length-1. i.e. for(int i=0;i<book.length-1;i++){

Your program will start working.

I have pasted the working code below:

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;
    }
Ashish Karn
  • 1,074
  • 1
  • 9
  • 20
  • What happens if the file contains real writer names and titles, which happen to contain spaces? Also, if you're commenting on the sorting algorithm, the limit is not the only issue - it's a single pass sort, it won't work. – RealSkeptic Jul 01 '20 at 12:44
  • For that case, nextInt() and nextDouble() should be replaced with Integer.parseInt(sc.nextLine()) and parseDouble and other code will be as it is... – Ashish Karn Jul 01 '20 at 12:48