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;
}
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]
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:
Complete code:
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.
I have the following solution for your problem, the first name and last name must be separated using a space
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 likeThe "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 linebecause at that point you haven't declared
lastName
orfirstName
.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 tofirstName
and one part tolastName
; but as it stands, this line isn't meaningful Java.The "unreachable code" is the
return firstName
. The previousreturn
line ends execution of the method, so your code won't ever get to thereturn 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.