Reputation: 57
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
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
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
Reputation: 17595
print that variable
String carType = promptForCarType(); //1 + 2
System.out.println(carType); //3
Or simply
System.out.println(promptForCarType());
Upvotes: 1
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
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