0

how to take more inputs from user using Nextline() in java if i use scn.Nextline() after reading one input it will work but giving a null in answer.

public class Mapdemo {

    public static void main(String[] args) {
        Scanner scn =new Scanner(System.in);
        System.out.println("enter the number of phoneaddress you want to store");
        int n=scn.nextInt();
        int j=0;
        System.out.println("enter the  phonenumber first and  then name of the person  phoneaddress");
        Map<String, Long> m1=new HashMap<String,Long>();
        for(int i=0;i<n;i++)
        {
            Long l=scn.nextLong();
            String s=scn.nextLine();
                m1.put(s,l);


        }

        System.out.println("enter the name to be checked");
        String s2=scn.next();
        System.out.println(m1.get(s2));
Jim Garrison
  • 81,234
  • 19
  • 144
  • 183

1 Answers1

1

After using .nextInt() for the first time do:

scanner.nextLine();

after that to clear the line, nextLine() stops at the newline character (OS dependant).

https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#nextLine()

whatamidoingwithmylife
  • 1,157
  • 1
  • 10
  • 29