I've got a homework that is about methods. So first part is

Write a method that prompts the customer for the car type. There are three acceptable car types (E: Economy, M: Midsize, F: Fullsize). Your method will return a valid car type.

But the thing that I don't understand is, should I read input from the user first and then make the method give a valid car type, or should method also read from user?

char type;
System.out.println("(E) Economy  - 50 TL");
System.out.println("(M) Midsize  - 70 TL");
System.out.println("(F) Fullsize - 100 TL");
    System.out.println("Enter the car type (E/M/F) : ");
    type = input.next().charAt(0);
    type = Character.toUpperCase(type);
} while (type != 'E' && type != 'M' && type != 'F' );


public static String carType(char y){

    String x ="";
    if(y == 'E')
        x = "Economy";
    if(y == 'M')
        x = "Midsize";
    if(y == 'F')
        x = "Full size";
    return x;

Do you think this is the right way to do it? Because it says "Write a method that prompts the user..." but what's the point of using method if I move all of the code above into method?


closed as off-topic by Jamal Nov 20 '14 at 3:16

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions containing broken code or asking for advice about code not yet written are off-topic, as the code is not ready for review. After the question has been edited to contain working code, we will consider reopening it." – Jamal
If this question can be reworded to fit the rules in the help center, please edit the question.


From the way you describe the problem, it is clear that there only needs to be one method. That method is supposed to: "... prompts the customer for the car type. ... Your method will return a valid car type".

What you have presented here, is the guts of some code which prompts the user for a character value, and then another method which converts the char to a String type.

From what I can tell (and filling in some blanks) your code looks functional, but I am not certain it satisfies the requirements (for just one method).

I would start with the 'specification' for the method, and use that as a template for the code. I will make the assumption (as you have) that the method should return the 'full' name of the car type (i.e. return "Economy" instead of 'E').

The good things in your code are the use of the do {...} while(...) block. You have a good loop there, with good conditional exit criteria.

You have not shown us what input is so I will assume it is a Scanner on System.in.

If we wrap the code you have there with a method that makes sense, it would be something like:

public String promptForCarType(Scanner input) {
    return carType;

That would be a method that prompts the user for a car type, and returns the user's selection.

The code inside that is what you have written above. There are a few things you should consider though:

  • You should probably get the full input from the user (not just the first character), and make sure the user only entered a single character (data validation).
  • Do you know what a 'switch' statement is? It would be really neat in here (and would remove the need to call the carType(char) method).
  • When you have java methods that return a value, there is often no need to 'store' the return value to the end of the method... you can just return when you have it....
  • when dealing with user input, you should try to leave them an 'exit strategy', like "enter Q to quit"
  • if you do an early-return mechanism, there is no need to do any conditions in the loop, you can just do while (true) {...}

One possible method for you may be something like:

public String promptForCarType(Scanner input) {
    System.out.println("(E) Economy  - 50 TL");
    System.out.println("(M) Midsize  - 70 TL");
    System.out.println("(F) Fullsize - 100 TL");
    // add in a Q option here ....
    // the Scanner may not have input....
    while (true) { // convert to while loop.
        System.out.println("Enter the car type (E/M/F) : ");
        if (!input.hasNext()) {
            throw SomeExceptionToSayTheresNoMoreInput(....);
        String userinput = input.next();
        // check that input is just one character long.....
        char type = userinput.charAt(0);
        type = Character.toUpperCase(type);
        switch (type) {
            case 'E' : return "Economy";
            .....   // other cases
            case 'Q' : throw new UserWantsToQuitException(".....");
            default : // no need for a default, the loop will just go around again
  • \$\begingroup\$ I didn't know that I can use a user input as a parameter and I guess that's the way teacher wanted me to use. Thanks so much, your answer helped me a lot. \$\endgroup\$ – v_7 Dec 11 '13 at 23:34
  • \$\begingroup\$ But I have no idea how to throw exceptions.. \$\endgroup\$ – v_7 Dec 11 '13 at 23:52
  • \$\begingroup\$ All great advice, and I would go ahead with separating the steps into methods. If the teacher insists on using only one method, "write a method" is not strong enough. Writing three methods involves writing a method . . . and then writing two more. ;) \$\endgroup\$ – David Harkness Dec 12 '13 at 4:59

An enum would be naturally suited to this situation. Since this is a homework problem, I have omitted parts of the solution.

enum CarType {
    Economy  (50),
    Midsize  (70),
    Fullsize (100);

    private final char init;
    private final int tl;
    private static final String MENU;

    static {
        StringBuilder sb = new StringBuilder();
        // …
        MENU = sb.toString();

    CarType(int tl) {
        // …

    public String toString() {
        return String.format("(%c) %-8s - %d TL", init, name(), tl);

    public static String menu() {
        return MENU;

    public static CarType parse(String s) {
        char init = Character.toUpperCase(s.charAt(0));
        for (CarType ct : values()) {
            // …
        return null;

If you ever need to add another car type in the future, it's trivial to define an additional enum member.

Since the car types are all defined in the enum, prompting can be done elegantly:

CarType ct;
do {
    ct = CarType.parse(input.next());
} while (null == ct);
System.out.println("You picked " + ct);

Not the answer you're looking for? Browse other questions tagged or ask your own question.