Home > Enterprise >  How can I add string components to an integer for an if statement?
How can I add string components to an integer for an if statement?

Time:11-17

I am currently making an if statement, but in that if statement I need to add string character, so based on what you say it either takes away 4 or not.

public static void yesornodisability()
{
    String disabled;
    Scanner scanner = new Scanner(System.in);
    System.out.println("Are you registered disabled(Yes / No)? ");
    disabled = scanner.nextLine();
    return;
}

This is the method I am using for my string, When I run the top code (yesornodisability) it works. However when I run the second code it gives me an error saying void cannot be converted to java.lang.String.

This is my if statement

public static int swimmingprice()
 {
     int userAge = age();
     int totalCost = total();
     String disabled = yesornodisability();
        
     if (userAge<=18)
     {
         totalCost= totalCost/2;
     }
     else if(userAge>=65)
     {
         totalCost = totalCost-3;
     }
     else if(disabled.equals("Yes"))
     {
         totalCost = totalCost-4;
     }
     else
     {
         totalCost = 10;
     }
     
     System.out.println("The swimming price for you is " totalCost " pounds.");
     return swimmingprice();
 }

CodePudding user response:

As others have pointed out, you need to change your method's return type and return the value from it.

Here is fixed code for you.

public static String yesornodisability()
{
    String disabled;
    Scanner scanner = new Scanner(System.in);
    System.out.println("Are you registered disabled(Yes / No)? ");
    disabled = scanner.nextLine();
    return disabled;
}

CodePudding user response:

    public static void yesornodisability()
{
    String disabled;
    Scanner scanner = new Scanner(System.in);
    System.out.println("Are you registered disabled(Yes / No)? ");
    disabled = scanner.nextLine();
    return;
}

You need to get rid of the void and return disabled.

return disabled;

You also need to fix your what your returning in the swimmingPrice() method.

return totalCost;

Instead of System.out.println("The swimming price for you is " totalCost " pounds."); return swimmingprice();

Change to return "The swimming price for you is " totalCost "pounds." Then in the main method System.out.println(swimmingPrice());

  • Related