javajava.util.scanneruser-input

Prompt user to enter name on one line and print it out as "Last, First"


No one in our entire lab was able to get this problem, so we have to do it for homework. We are to prompt the user to enter their first and last name on one line. The main method is supposed to handle all println statements and should print the name as Last, First.

line 26 states "cannot find symbol."
line 36 states "not a statement. ';' expected.
line 39 states unreachable statement.

I have been playing with this code for an hour and have just about given up. Any help?

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package lab11;

import java.util.Scanner;

/**
 *
 * @author xxxxxx
 */
public class Lab11 {
    public static void main(String[] args) {
        System.out.println("Enter your name: ");
        Scanner user_input=new Scanner(System.in);

        changeNameFormat();
        System.out.println(lastName+", " + firstName);

        System.out.println("Enter a word. ");
        palindrome();
    }

    public static String changeNameFormat() {
        String firstName, lastName;
        firstName, lastName = user_input.nextLine;
        return lastName;
        return firstName;
    }

Solution

  • Here is a list of steps you need to do:

    Get the input [Will be Example: "John Smith"]

    Split it into two pieces [There are several ways to do this]

    firstName = input.substring(0, input.indexOf(" "));
    lastName = input.substring(input.indexOf(" ")+1, input.length());
    

    substring() returns a portion of the string.

    indexOf() finds a specific character in the string.

    It takes a start value and an end value.

    In this case, we start at the beginning, and end when we find a space.

    To get the last name, we start where we find a space, and end at the end of the string.

    You can also combine this with the .trim() method that removes whitespace from the beginning/end of strings.

    Finally, we return the concatenated value:

    return lastName + ", " + firstName;
    

    Complete code:

    package lab11;
    import java.util.Scanner;
    public class Lab11 {
        public static Scanner user_input;
        public static void main(String[] args) {
            user_input = new Scanner(System.in);
            System.out.print("Enter your name: ");
            System.out.println(changeNameFormat(user_input.nextLine()));
        }
        public static String changeNameFormat(String input) { //Pass a name
            String firstName, lastName;
            firstName = input.substring(0, input.indexOf(" "));
            lastName = input.substring(input.indexOf(" ")+1, input.length());
            return lastName + ", " + firstName;
        }
    }
    

    Tested on: http://www.compileonline.com/compile_java_online.php

    One final thing. It's a good convention to make your methods work as they are named.

    Since yours is called "changeNameFormat()", we should probably pass it the name we want to change.

    Then in our main, we can get the input, or even pass it as a parameter.