Jimmy Guo
Jimmy Guo

Reputation: 1

Reverse name task

Here is what my code is trying to do: Write a method called processName that accepts a Scanner for the console as a parameter and prompts the user to enter a full name, then prints the name in reverse order (i.e., last name, first name).

Here is an example dialogue with the user:

Please enter your full name: Sammy Jankis

Your name in reverse order is Jankis, Sammy

import java.util.*; 
public class Project3 {
    public static void main(String[] args) {
        System.out.print("Please enter your full name: ");
        String firstname = console.next();
        String lastname = console.next();
        processName(firstname, lastname);
    }

    public static void processName(String y, String z) {
        System.out.print("Your name in reverse order is"
             + z + ", " + y);               
    }
}

I get an error reading:

Project3.java:16: cannot find symbol
symbol  : variable console
location: class Project3
        String firstname = console.next();
                           ^
Project3.java:17: cannot find symbol
symbol  : variable console
location: class Project3
        String lastname = console.next();
                          ^

My question is, how can I divide the name string into two parts so that I can reverse it? Please use simple terms because I am new to coding.

Upvotes: 0

Views: 1057

Answers (1)

Elliott Frisch
Elliott Frisch

Reputation: 201439

You forgot to declare (and initialize) console. I think you wanted

Scanner console = new Scanner(System.in);

before String firstname = console.next();

Upvotes: 2

Related Questions