21

I need to read spaces (present before string and after String) given as input using Scanner Note : if there is no spaces given in input it should not add space in output

Please find the below code:

package practise;

import java.util.Scanner;

public class scanccls {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner scan = new Scanner(System.in);
         String name= scan.nextLine();
        name+=scan.nextLine();
         scan.close();

        System.out.println("Enter your name"+name); 

    }

}

I am expecting output like:

  1. Input :Enter Your name:Chandu Aakash
    Output:chandu Aakash

  2. Input: Enter Your name: (Space..)Chandu Aakash(Space..)
    Output: (space.. )chandu Aakash(Space..)

0

8 Answers 8

33

Your code work fine. I just add little modification:

package practise;

import java.util.Scanner;

public class scanccls {

    public static void main(String[] args) {

        System.out.println("Enter your name:"); 
        Scanner scan = new Scanner(System.in);
        String name="";

        name+=scan.nextLine();
        scan.close();

        System.out.println("Your name is :"+name); 

    }

}
Sign up to request clarification or add additional context in comments.

Comments

25

One can use the delimiter function to segregate your input as shown below.

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in).useDelimiter("\n");
        String input = scanner.next();
        System.out.println(input);
        scanner.close();

    }
}

Comments

14
import java.util.*;
public class Str{
    public static void main(String[] args) throws IOException
    {
        Scanner scan = new Scanner(System.in);
        int i = scan.nextInt();
        double d = scan.nextDouble();
        String s=" ";
        s= scan.nextLine();
        s+=scan.nextLine();
        scan.close();

        System.out.println("String: "+s);
        System.out.println("Double: "+d);
        System.out.println("Int: "+i);
    }
}

4 Comments

The solution above worked for a HackerRank exercise: hackerrank.com/challenges/java-stdin-stdout/… (HackerRank editor has some limitation on regular expression, btw. The above solution is the best approach I can find)
Can you explain how does this works? I was trying the hackrrank challenge also. I didnt understand why it works
It's reading in the int, the double, and then the string sequentially. Then output them.
How is the string with spaces is being read? Why we are doing sc.nextLine twice? Shoudnt one would suffice. Also why we have initiliazed s with " "
3

/@esprittn solution didn't work./

my solution:

while(scan.hasNext()){
name+=scan.nextLine();
}

Comments

2

I use this function below, to read from all user input format, text inclusive spaces, then parse to specific datatype after.

package practice;
import java.io.*;

public class readInputSample{
   public static void main(String[] args) {
        String strVal = getInput("Enter string value: "); // Direct as string
        Integer intVal = Integer.parseInt(getInput("Enter integer value: "));
        Double dblVal = Double.parseDouble(getInput("Enter double value: "));
        Float fltVal = Float.parseFloat(getInput("Enter float value: "));

        System.out.println("String value: " + strVal);
        System.out.println("Integer value: " + intVal);
        System.out.println("Double value: " + dblVal);
        System.out.println("Float value: " + fltVal);
   }

   // Special Function to read all user input
   private static String getInput(String prompt){
      BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));

      System.out.print(prompt);
      System.out.flush();

      try{
          return stdin.readLine();
      } catch (Exception e){
        return "Error: " + e.getMessage();
      }
    }
}

Comments

2

I have done a few changes in your code, it will run just fine for this code

package practise;

import java.util.Scanner;

public class scanccls {

    public static void main(String[] args) {
        //// TODO Auto-generated method stub

        System.out.println("Enter your name:"); 
        Scanner scan = new Scanner(System.in);
        String name="";

        name+=scan.nextLine();
        scan.close();

        System.out.println("Your name is :"+name); 

    }

}

Comments

0
package practise;
import java.util.Scanner;

public class scanccls
{
    public static void main(String[] args)
    {
        System.out.println("Enter your name:");
        Scanner scan = new Scanner(System.in);
        String name = "";
        name += scan.nextLine();

        // Can also be done like
        // String name=scan.next();
        // name+=scan.nextLine();

        // They Both Work as same

        System.out.println("Your name is :" + name);
    }
}

Comments

0
package practise;
import java.util.Scanner;
public class scanccls {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    Scanner scan = new Scanner(System.in);
    String name= scan.nextLine();
    scan.close();

    System.out.println("Enter your name"+name); 
}
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.