Scanner doesn't read whole sentence - difference between next() and nextLine() of scanner class

后端 未结 22 1090
北荒
北荒 2020-12-03 01:14

I\'m writing a program which allows the user to input his data then outputs it. Its 3/4 correct but when it arrives at outputting the address it only prints a word lets say

相关标签:
22条回答
  • 2020-12-03 01:48

    Below is the sample code to read a line in Standard input using Java Scanner class.

    import java.util.Scanner;
    
    public class Solution {
    
        public static void main(String[] args) {
            Scanner scan = new Scanner(System.in);
            String s = scan.nextLine();
            System.out.println(s);
            scan.close();
        }
    }
    

    Input:

    Hello World

    Output:

    Hello World

    0 讨论(0)
  • 2020-12-03 01:50

    We can easily done using scan.nextLine .It will read the rest of the input till the end. Then assign it to your variable. Entire sentence can be printed easily . Here is the example for your better understanding.

        String s = "HackerRank ";
    
        Scanner scan = new Scanner(System.in);
    
    
        String s2;
    
    
    
        scan.nextLine(); // read the rest of the line of input (newline character after the double token).
        s2 = scan.nextLine();
    
    
    
        /* Concatenate and print the String variables on a new line integer variables on a new line; 
    
        System.out.println(s + s2);
    
        scan.close();
    

    } }

    0 讨论(0)
  • 2020-12-03 01:50

    You can do this:

    class MyStudentDetails{
        public static void main (String args[]){
             Scanner s = new Scanner(System.in);
             System.out.println("Enter Your Name: ");
             String name = s.nextLine();
             System.out.println("Enter Your Age: ");
             String age = s.nextLine();
             System.out.println("Enter Your E-mail: ");
             String email = s.nextLine();
             System.out.println("Enter Your Address: ");
             String address = s.nextLine();
    
    
             System.out.println("Name: "+name);
             System.out.println("Age: "+age);
             System.out.println("E-mail: "+email);
             System.out.println("Address: "+address);
    
        }
    }
    
    0 讨论(0)
  • 2020-12-03 01:52

    next() can read the input only till the space. It can't read two words separated by space. Also, next() places the cursor in the same line after reading the input.

    nextLine() reads input including space between the words (that is, it reads till the end of line \n). Once the input is read, nextLine() positions the cursor in the next line.

    for reading the entire line you can use nextLine()

    0 讨论(0)
  • 2020-12-03 01:52

    I had the same question. This should work for you:

    s.nextLine();
    
    0 讨论(0)
  • 2020-12-03 01:53

    This approach is working, but I don't how, can anyone explain, how does it works..

    String s = sc.next();
    s += sc.nextLine();
    
    0 讨论(0)
提交回复
热议问题