0
public static void initialval()
{
    Scanner sc = new Scanner(System.in);
    
    
    
    System.out.println("Enter your:");
    
    System.out.println("i) Name ");
    String name = sc.nextLine();
    
    System.out.println("ii) Account number");
    long accno = sc.nextInt();
    System.out.println("iii) Type of account");
    String typeofacc = sc.nextLine();
    
    System.out.println("iv) Balance");
    int bal = sc.nextInt();
    sc.close();
}

the output is

Enter your:
i) Name
Dhruv
ii) Account number
123
iii) Type of account
iv) Balance
5000

As you can see the type of account doesn't execute

LW001
  • 2,452
  • 6
  • 27
  • 36
  • 1
    Instead of `scanner.nextInt()`, use `Integer.parseInt(scanner.nextLine())` and it will work. – Zabuzard Sep 13 '21 at 06:36
  • 1
    Do not close scanners tied to `System.in`. Remove the `sc.close()` statement. Otherwise you will close `System.in` itself and can not open it again anymore. – Zabuzard Sep 13 '21 at 06:36
  • I think you should look properly at my question I asked about sc.nextLine(). Not about sc.nextInt() – Dhruv Baviskar Sep 13 '21 at 06:38
  • 2
    The usage of `sc.nextInt()` causes the issue at `sc.nextLine()`... If you read the linked duplicate, it will be clear **why**. The fix is exactly what I wrote above, just try it out. – Zabuzard Sep 13 '21 at 06:43
  • Thanks bro I got it but still a question why did this happen . As in it should have not happened right? – Dhruv Baviskar Sep 13 '21 at 10:33
  • 1
    You just have a misconception of how scanner, and in particular the nextXXX methods work. The class works as expected, this is not a bug. You are just using the class wrong. Again, the linked duplicate explains it in detail - have you read it? What is unclear after having read it? – Zabuzard Sep 13 '21 at 11:48

0 Answers0