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

前端 未结 7 1782
春和景丽
春和景丽 2021-02-02 04:29

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

相关标签:
7条回答
  • 2021-02-02 04:38

    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();
    
        }
    }
    
    0 讨论(0)
  • 2021-02-02 04:39

    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); 
    
        }
    
    }
    
    0 讨论(0)
  • 2021-02-02 04:46

    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();
          }
        }
    }
    
    0 讨论(0)
  • 2021-02-02 04:50

    /@esprittn solution didn't work./

    my solution:

    while(scan.hasNext()){
    name+=scan.nextLine();
    }
    
    0 讨论(0)
  • 2021-02-02 04:57
    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);
        }
    }
    
    0 讨论(0)
  • 2021-02-02 05:00
    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);
        }
    }
    
    0 讨论(0)
提交回复
热议问题