gergomat
gergomat

Reputation: 57

How to print out only return value of a method?

I've got a homework that I should write a couple of methods(e.g. a method that prompts customer for the car type and returns a valid car type) and then my program should display the car type, number of days car rented for,extras etc. This is first method that teacher wanted me to write ,

public static String promptForCarType(){
        Scanner input = new Scanner(System.in);
        char type;
        System.out.println("(E) Economy  - 50 TL");
        System.out.println("(M) Midsize  - 70 TL");
        System.out.println("(F) Fullsize - 100 TL");
        do{
            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' ); //I tried to define condition with "||" operator,didn't work.


        switch(type) {
            case 'E' : ;return "Economy";             
            case 'M' : return "Midsize";
            case 'F' : return "Fullsize";
            default : return " ";

        }


    }

How can I print out only return value of this method ? Should I add System.out.println("Car type is ...) part in promptForCarType()?

Upvotes: 2

Views: 71410

Answers (5)

Elliott Frisch
Elliott Frisch

Reputation: 201439

You could just return from an infinite loop (and simplify your code) like so -

public static String promptForCarType() {
    Scanner input = new Scanner(System.in);
    char type;
    for (;;) {
        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);
        switch (type) {
        case 'E':
            return "Economy";
        case 'M':
            return "Midsize";
        case 'F':
            return "Fullsize";
        }
    }
}

Upvotes: 0

Babbleshack
Babbleshack

Reputation: 341

String carType = promptForCarType();
System.out.println("Car type is: " + carType);

should work. the return value of promptForCar() is stored in the string carType, and then the print statement uses the string variable to print the return value. `

Upvotes: 0

A4L
A4L

Reputation: 17595

  1. Call your method from your main method
  2. Assign the retrurned value to a variable
  3. print that variable

    String carType = promptForCarType(); //1 + 2
    System.out.println(carType); //3
    

Or simply

System.out.println(promptForCarType());

Upvotes: 1

Adarsh
Adarsh

Reputation: 3641

The code control returns to the invoking function when it encounters the keyword return. So you can only operate in the current method till the program reaches the keyword return. So, if you need to print something, print it before the return. In your case, you need to print the value in your switch-case construct , for each case before the return statement.

 switch(type) {
        case 'E' : System.out.println("Economy");
                   return "Economy";             
       // similarly for each switch case.
    }

Or, the better way would be to assign the car type to a variable of type String and then print the value. and write a common return statement for the entire method(for all cases).

   String carType;
   switch(type) {
        case 'E' : carType ="Economy";
       // similarly for each switch case.
    }
    System.out.println(carType);
    return carType;

Upvotes: 6

user3149556
user3149556

Reputation: 1

if you want to print the instance of the car type you would simply go to your main method and print the called instance of your car accessing the method promptForCar. Ex: System.out.println(object.method);

Upvotes: 0

Related Questions