chandrakanth B
chandrakanth B

Reputation: 389

How to take input as String with spaces in java using scanner

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..)

Upvotes: 21

Views: 184151

Answers (8)

PravinMahandule
PravinMahandule

Reputation: 21

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); 
}
}

Upvotes: 0

Anubhav Mishra
Anubhav Mishra

Reputation: 125

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); 

    }

}

Upvotes: 2

Arvind Bakshi
Arvind Bakshi

Reputation: 65

/@esprittn solution didn't work./

my solution:

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

Upvotes: 3

Vishal Bhola
Vishal Bhola

Reputation: 1

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);
    }
}

Upvotes: 0

Rezza Prayogi
Rezza Prayogi

Reputation: 21

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();
      }
    }
}

Upvotes: 2

Anam
Anam

Reputation: 131

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);
    }
}

Upvotes: 13

Rajesh
Rajesh

Reputation: 4769

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();

    }
}

Upvotes: 25

JHDev
JHDev

Reputation: 995

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); 

    }

}

Upvotes: 32

Related Questions