-1

Error java.lang.NumberFormatException if i try to insert 2 words into a single string (Java) !!

ERROR shown is:

Enter item Chicken rice Enter amount(Dinar)

Exception in thread "main" java.lang.NumberFormatException: For input string: "rice" at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:67) at java.base/java.lang.Integer.parseInt(Integer.java:668) at java.base/java.lang.Integer.valueOf(Integer.java:999) at Sampleproject1/t4.Main.main(Main.java:65)

import java.util.*;

class Expense implements Comparable<Expense>{
    
    String expenseCategory;
    Integer amount;
    
    
    public String getExpenseCategory() {
        return expenseCategory;
    }
    public void setExpenseCategory(String expenseCategory) {
        this.expenseCategory = expenseCategory;
    }
    public Integer getAmount() {
        return amount;
    }
    public void setAmount(Integer amount) {
        this.amount = amount;
    }   
    
    public Expense(String expC , Integer amount) {
        this.expenseCategory=expC;
        this.amount = amount;
    }
        
    public String toString() {
        return expenseCategory+ " " +amount;
    }
        
    public int compareTo(Expense e) {
        if(amount>e.amount){    
            return 1;    
        }else if(amount<e.amount){    
            return -1;    
        }else{    
        return 0;    
        } 
    }
}

public class Main {
    
    public static void main(String[] args) {
        
        Scanner sc = new Scanner(System.in);
        
        TreeSet<Expense> set1 = new TreeSet<Expense>();

        int tamount=0;
        
        while(true) {
            System.out.println("Enter item");
            String expC = sc.next();
            System.out.println("Enter amount(Dinar)");
            String amt = sc.next();
            tamount = tamount + Integer.valueOf(amt);
            set1.add(new Expense(expC,Integer.valueOf(amt)));
            System.out.println("Do you want to continue(yes/no):");
            String cond = sc.next();
            if(cond.equals("no")) {
                break;
            }else {continue;}
        }
        System.out.println("Total dinar required is:");
        System.out.println("Category    Amount(Dinar)");
        Iterator x = set1.descendingIterator();
            
        while(x.hasNext()) {
            System.out.println(x.next());
        }
                
        System.out.println("Total amount needed in Dinar:"+ tamount);
        System.out.println("Total amount needed in Rupees:"+ tamount*18.565);   
    }       
}
Output should be like :
Enter item
Fried Rice
Enter amount(Dinar)
20
Do you want to continue(yes/no):
yes
Enter item
Chicken Lollipop
Enter amount(Dinar)
25
Do you want to continue(yes/no):
yes
Enter item
Coffee
Enter amount(Dinar)
5
Do you want to continue(yes/no):
no
Total dinar required is:
Item           Amount(Dinar)  
Chicken Lollipop25             
Fried Rice     20             
Coffee         5              
Total amount needed in Dinar: 50
Total amount needed in Rupees: 928.25

IF I USE nextLine(); some inputs are SKIPPED !!

*I

Arun Sudhakaran
  • 2,167
  • 4
  • 27
  • 52
Tharun S M
  • 47
  • 1
  • 2
  • 9
  • 1
    Please refer to the documentation. [`next`](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/Scanner.html#next()) reads *tokens* separated by space. If you want to read the whole line `Fried Rice` then you need to use [`nextLine`](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/Scanner.html#nextLine()) instead – QBrute Apr 01 '23 at 09:11
  • but if i use nextLine() the while loop skips some inputs – Tharun S M Apr 01 '23 at 09:21
  • Then you need to read this: [Scanner is skipping nextLine() after using next() or nextFoo()?](https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-or-nextfoo) – QBrute Apr 01 '23 at 09:39

2 Answers2

0

It is clear from your examples that you intend for the 'enter' key to be the separator between input. However, out of the box, scanner treats any whitespace as the separator. Simply tell it you only want newlines. Immediately after new Scanner(System.in):

scanner.useDelimiter("\\R");

\\R is regexp-ese for "any newline variant".

rzwitserloot
  • 85,357
  • 5
  • 51
  • 72
-2

Fixed it by adding nextLine() for all the statements

Tharun S M
  • 47
  • 1
  • 2
  • 9