0

I want to create id (int), title (String), category (String) and cost (Float) for a class in Java. I used an Scanner object to read them from keyboard. But I can only read the id, after that the console do not allow me to read the title, it goes to category and then cost. Even if I type a float to cost, it throws an exception. Please help me.

package ChuoiTrongJava;
import java.util.Scanner;
public class IndexOf {

    public static void main(String[] args) {
    
        Scanner kb = new Scanner(System.in);
        System.out.print("ID: ");
        int id = kb.nextInt();
        System.out.print("\nTitle: ");
        String title = kb.nextLine();
        System.out.print("\nCategory: ");
        String category = kb.nextLine();
        System.out.print("\nCost: ");
        Float cost = kb.nextFloat();
    
    }
}

Class main Problem

Comet
  • 13
  • 3
  • 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) – Tim Moore Dec 27 '22 at 10:00
  • I know you're excited but please provide code instead of image. Thank you. – knoxgon Dec 29 '22 at 08:54

0 Answers0