장대희
장대희

Reputation: 53

Java how to pass and return string, method?

import java.util.Scanner;

import javax.swing.JOptionPane;

public class StarWars { 

    public static void main(String[] args) {
        Scanner reader = new Scanner(System.in);
        String firstName = "";
        String lastName = "";
        String maidenName = "";
        String town = "";

        System.out.print("What is your first name? ");
        firstName = reader.nextLine();
        System.out.print("What is your last name? ");
        lastName = reader.nextLine();
        System.out.print("What is your mothers maiden name? ");
        maidenName = reader.nextLine();
        System.out.print("What town were you born? ");
        town = reader.nextLine(); 

        String Sfirstname = firstName.substring(0,2);
        String Slastname = lastName.substring(0,3);
        String SmaidenName = maidenName.substring(0,2);
        String Stown = town.substring(0,3);
        String Star = Sfirstname + Slastname;
        String War = SmaidenName + Stown; 
        String StarWar = Star + War;
        System.out.print("Your Star Wars name is: " + StarWar);
    }

    public static String StarWar (String Star, String War) {
        String name;
        name = Star + " " + War;
        return War;
    }
}

So this is my code about my project. While I'm doing my project I have some problem about the returning method and passing method.

I set up the main method perfectly to print out thing that what I want to see.

The problem is I also have to use passing method and returning method. My teacher want me to do two things with passing/returning method.

  1. Pass all this data to your method, and the method should generate and return the users Star Wars name.
  2. Get the return value of the method, and display it to the screen.

I have no idea what should I do with this problems (took 5 hrs to do everything I learn but wrong..).

Can someone give a hint or teach me what actually my teacher want me to do and How I can do this?

I really need help from you guys.

Additional, if I run a program it should be like this.

first name? user input: Alice last name? user input:Smith mothers maiden name? user input: Mata town were you born? user input: Sacramento

Your Star Wars name is: SmiAl MaSac

Upvotes: 0

Views: 10944

Answers (3)

Elliott Frisch
Elliott Frisch

Reputation: 201447

There are a few things we can improve here, lets start with the method - the method name looks like a constructor and doesn't perform the logic itself, lets describe what it does and move the logic into the method - we don't need all of those temporary variables (we can use a StringBuilder) like

public static String buildStarWarsName(String firstName, String lastName, 
            String maidenName, String town) 
{
    return new StringBuilder(lastName.substring(0, 3)) //
            .append(firstName.substring(0, 2)) //
            .append(" ") // <-- for the space between first and last
            .append(maidenName.substring(0, 2)) //
            .append(town.substring(0, 3)) //
            .toString();
}

Then you can initialize your variables when you read them and finally call the method

public static void main(String[] args) {
    Scanner reader = new Scanner(System.in);
    System.out.print("What is your first name? ");
    String firstName = reader.nextLine();
    System.out.print("What is your last name? ");
    String lastName = reader.nextLine();
    System.out.print("What is your mothers maiden name? ");
    String maidenName = reader.nextLine();
    System.out.print("What town were you born? ");
    String town = reader.nextLine();

    System.out.print("Your Star Wars name is: " + //
            buildStarWarsName(firstName, lastName, maidenName, town));
}

Upvotes: 1

Sinil
Sinil

Reputation: 87

Your method is returning the 'war' parameter. Based on what your trying to do it looks like it should be returning 'name'. That's what the method built.

Upvotes: 0

Naman
Naman

Reputation: 31898

You should return what you evaluated instead :

return name;

and then call this defined method while you want to read the value.


The changes highlighted in the comments as well:

   String StarWar = Star + War;  // this would not be required, as handled by your method 'starWarName'
   System.out.print("Your Star Wars name is: " + starWarName()); // calling the method defined
}

public static String starWarName (String Star, String War) { //renamed method to break the similarity with other variables
    String name;
    name = Star + " " + War;
    return name; //returning the complete star war name
}

Upvotes: 1

Related Questions