0

I am writing a program where the user inputs their forename and surname with space inbetween all on one line. The program will then detect the space and extract the forename and surname into different variables to be printed out.

Example: Say a user entered "simon hall" on one line There is space inbetween and I want it outpuuted like this: Forename: Simon Surname: Hall

My code(pretty much useless right now): (Input/Output Below)

import java.util.Scanner;

class Main {

  public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);

    System.out.println("Input forename and surname on one line (space included)");

    String fullName = scanner.next();

    String result = fullName;
    result = result.split(" ") [0];
    System.out.println(result);
    
   }
} 

**Input: zayaan khan (on one line)

Output:

zayaan** (I want to print Forename: Zayaan Surname: Khan )

Thank you ;)

dariosicily
  • 1,062
  • 1
  • 4
  • 9
ZKcoder
  • 51
  • 6
  • Note: you want others to spend their time to help you with your question. So you please spend the time required to properly format and indent all your input. That preview window, and the help explaining formatting exist for a reason. – GhostCat Sep 19 '20 at 07:20
  • 2
    Instead of using `scanner.next()`, use `.nextLine()` method to read the complete user input. Using `.next()` method will not return the complete user input that is separated by whitespace. See: [What's the difference between next() and nextLine() methods from Scanner class?](https://stackoverflow.com/questions/22458575/whats-the-difference-between-next-and-nextline-methods-from-scanner-class) – Yousaf Sep 19 '20 at 07:21
  • @GhostCat sorry, not really used to functions of stackoverflow ;) – ZKcoder Sep 19 '20 at 09:42

2 Answers2

2

Get the Input as line by line

String fullName = scanner.nextLine(); // line by line input getter

Split the string based on space and store it in String Array and then you can access it by using indices of that array

String result[] = fullName.split(" "); // spliting the string

System.out.println("Forename: "+result[0]+" Surname: "+result[1]);// print it 
Kaviarasu
  • 68
  • 4
  • This helped me out alot so thank you. One question, what does it mean by String result[] and result[0] and result[1] ? please explain simply. – ZKcoder Sep 19 '20 at 09:47
  • Your inputs are two words/Strings (Firstname & Lastname) separated by space in single line.So we are splitting the single line input into 2 words and storing in the array.Result[] is that array we are storing.Array index starts from 0.So, first word stored in 0th index (i.e Result[0]) and second word is stored in 1st index (i.e Result[1]).Is it clear now? – Kaviarasu Sep 19 '20 at 11:11
  • I understand about the array thank you. Lastly, does the space count as it's own word even with the split() function? – ZKcoder Sep 20 '20 at 09:36
0

Using regex:

public class Main {

    public static void main(String[] args) {
        final Pattern pattern = Pattern.compile("(\\S+)\\s+(\\S+)");

        final Scanner scanner = new Scanner(System.in);
        final Matcher matcher = pattern.matcher(scanner.nextLine());

        if (matcher.find())
            System.out.println(String.format("Forename: %s, Surname: %s",  matcher.group(1), matcher.group(2)));
    }
}
Yamahari
  • 1,570
  • 5
  • 17