Prompt user to enter name on one line and print it

2019-03-04 07:40发布

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

4条回答
beautiful°
2楼-- · 2019-03-04 08:16

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.

查看更多
The star\"
3楼-- · 2019-03-04 08:26

I have the following solution for your problem, the first name and last name must be separated using a space

import java.util.Scanner;

public class LastFirst {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Please Enter your name");
        String name = scanner.nextLine();
        name = name.substring(name.indexOf(" ")+1, name.length())+" "+name.substring(0, name.indexOf(" "));
        System.out.println(name);
    }
}
查看更多
神经病院院长
4楼-- · 2019-03-04 08:29

Java does not support that syntax. Instead, you will need to use something like String.split(String) and you might combine that with formatted output like

Scanner input = new Scanner(System.in);
System.out.println("Enter your name: ");
if (input.hasNextLine()) {
    String line = input.nextLine();
    String[] names = line.split("\\s+"); // <-- globs consecutive white-space
    if (names.length > 1) {
        String firstName = names[0];                // <-- indexes start at 0
        String lastName = names[names.length - 1];  // <-- so subtract 1!
        System.out.printf("%s, %s%n", lastName, firstName);
    } else {
        System.out.println("Unknown: " + line);
    }
}
查看更多
你好瞎i
5楼-- · 2019-03-04 08:32

The "cannot find symbol" will be palindrome(), which is a method call but you haven't defined it anywhere (and it doesn't seem relevant to the problem). You'll also get this on the line

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

because at that point you haven't declared lastName or firstName.

The "not a statement" is the line firstName, lastName = ..., which is not valid syntax in Java (though it is in some languages, where you can assign to two variables at once). You need to find a way to split the input up into two parts, and assign one part to firstName and one part to lastName; but as it stands, this line isn't meaningful Java.

The "unreachable code" is the return firstName. The previous return line ends execution of the method, so your code won't ever get to the return firstName statement. You're supposed to be printing things, not returning them.


In this case, all the errors reported are genuine. But in general, if you have lots of errors in the first part of your code, then the compiler won't reliably identify later errors, because it won't manage to parse the whole file. So sometimes, you'll find you fix a syntax error in the first few lines, and that seems to create a problem later on. It isn't really creating a new problem, it's just that the compiler wasn't previously able to find it.

查看更多
登录 后发表回答