[Java] Combining else if & user input.

Mave

TMS Founder
Administrator
Messages
234,508
Location
Belgium
Okay so I'm doing them TheNewBoston tuts and I'm at tutorial 19 atm.

Quick question (probably no biggie for you guys)

Right now I made this;

Code:
public class Weight {
    public static void main(String[] args) {


        int weight = 60;

        if(weight >= 50)
            System.out.println("You're not fat at all.");
        else if (weight>=60)
            System.out.println("You're not fat.");
        else if (weight>=70)
            System.out.println("You're a bit fat.");
        else if (weight>=80)
            System.out.println("Loose some weight man.");
        else if (weight>=90)
            System.out.println("They see me rollin'");
        else if (weight>=100)
            System.out.println("MAN THE HARPOONS");
        else if (weight>=150)
            System.out.println("You're dead");
        else
            System.out.println("Hey mr skinny.");

    }
}

Now I want to let the user input their weight, instead of defining it in the beginning...

In Bucky's 16th tutorial I saw this;

Code:
public class Bucky16 {
    private String girlName;
    public void setName(String name){
        girlName = name;
    }
    public String getName(){
        return girlName;
    }
    public void saying(){
        System.out.printf("Your awesome girlfriend is %s", getName());
    }

}


Below is class Bucky16b
Code:
import java.util.Scanner;
public class Bucky16b {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        Bucky16 Bucky16Object = new Bucky16();
        System.out.println("Enter name of Steffaaay here: ");
        String temp = input.nextLine();
        Bucky16Object.setName(temp);
        Bucky16Object.saying();
    }
}

That resulted in: "Enter name of Steffaaay here:"
And then the user could input a String.

Now I want the same thing for the weight code.
I'm probably not seeing the logic because I'm tired or IDK.

Halp please :megusta:
 
Enter name of Steffaaaay kinda defeats the purpose tbh LOL.
Eh I can only think of how to do it in VB/VBA, with a MsgBox.
Else, simply change the variables in Bucky16b and change it to an integer?
 
Here:

Code:
import java.util.Scanner;

public class Weight {
    public static void main(String[] args) {


        int weight;
        
        System.out.println("Enter your weight: ");
        Scanner input = new Scanner(System.in);
        weight = Integer.parseInt(input.nextLine());

        if(weight >= 50)
            System.out.println("You're not fat at all.");
        else if (weight>=60)
            System.out.println("You're not fat.");
        else if (weight>=70)
            System.out.println("You're a bit fat.");
        else if (weight>=80)
            System.out.println("Loose some weight man.");
        else if (weight>=90)
            System.out.println("They see me rollin'");
        else if (weight>=100)
            System.out.println("MAN THE HARPOONS");
        else if (weight>=150)
            System.out.println("You're dead");
        else
            System.out.println("Hey mr skinny.");

    }
}



First I imported the scanner (Obviously), then asked the user for their weight, then set the scanner to read the user's input.

From there, we set the variable weight to whatever the input was (Previously stored in the string variable input)
Since the input is coming in as a string, you have to "parse" (parse means convert) the String into an integer, that's done with Integer.parseInt(string name here). The string is called input, but since we're using a scanner, you have to put the regular scanner statement input.nextLine().

From there, the rest of the program goes about its business as usual. Hope that helps, ask questions if you have them :biggrin:
 
Thanks for the reply GP :3

The user input works now yay

Only I don't get why it always says "You're not fat at all", instead of the different messages, aka it's not working D:

EDIT: Also thanks for explaining what you did.
 
Ah, right. That's something I didn't catch before, it's pretty sneaky :tongue:

You see, your setup uses else if statements, meaning they fire in order from top to bottom. If one fires, the rest get ignored. So, what do you notice about this, relative to whatever your input is?
Code:
 if(weight >= 50)
            System.out.println("You're not fat at all.");
        else if (weight>=60)


If you put anything over 50 in there, the first IF will say OH SHIT they put something above 50 in, that must mean use this statement! And then the rest of the statements will get ignored. It won't check if it's above 60 or any of the other conditions, because one is already satisfied.

So, there's a few ways to go about fixing this, choose whatever you want.

- Invert the list, so the highest numbers get checked first
- Instead of using else statements, just use single IFs for each one
- Add another condition to each of the conditional statements to make sure the number is LESS than a certain amount, for example:
Code:
  if(weight >= 50 && weight < 60){
etc
}

That'll check both that the number is greater than 50 and less than 60 (That's probably the most complicated && || (< see what i did just there? :troll: ) the longest method, but probably also the most "Professional")
 
Woah thanks again for your informative reply :3

Seems like I need to think better on how to get around a problem like that. (you don't seem to have a problem with that :tongue:)
Going to add a condition, too lazy to turn everything around.

And also what did you mean with the &&||? :tongue:
 
In conditional statements, if you want to check more than one thing there's the operators && and ||, && means AND (For the statement to fire, 'this' AND 'this' both have to be true), and || means OR (For the statement to fire 'this' OR 'this' has to be true)



Panki said:
Wouldn't it be smarter to use a case if?

You mean a switch statement? (Looks something like this)

9ScBn.png



In this case (And most situations with specific numbers), yes it would be, but if this is for school then maybe Mave's class hasn't gotten that far yet :tongue:
 
GPow69 said:
Ah, right. That's something I didn't catch before, it's pretty sneaky :tongue:

You see, your setup uses else if statements, meaning they fire in order from top to bottom. If one fires, the rest get ignored. So, what do you notice about this, relative to whatever your input is?
Code:
 if(weight >= 50)
            System.out.println("You're not fat at all.");
        else if (weight>=60)


If you put anything over 50 in there, the first IF will say OH SHIT they put something above 50 in, that must mean use this statement! And then the rest of the statements will get ignored. It won't check if it's above 60 or any of the other conditions, because one is already satisfied.

So, there's a few ways to go about fixing this, choose whatever you want.

- Invert the list, so the highest numbers get checked first
- Instead of using else statements, just use single IFs for each one
- Add another condition to each of the conditional statements to make sure the number is LESS than a certain amount, for example:
Code:
  if(weight >= 50 && weight < 60){
etc
}

That'll check both that the number is greater than 50 and less than 60 (That's probably the most complicated && || (< see what i did just there? :troll: ) the longest method, but probably also the most "Professional")
Wouldn't this have the same result:
Code:
if(weight <= 50)
            System.out.println("You're a bit skinny.");
        else if (weight<=60)
            System.out.println("You're not fat at all.");
        else if (weight<=70)
            System.out.println("You're not fat.");
Also is a bit more logical in my eyes.
 
That would also work, yeah, but it's even more work to convert them all over. Since he's already typed the entire block out, it'd be easier to do one of the other methods, but typing from scratch, that idea is much more efficient.


Also wtf I just realized, Mave, you don't have any braces on your IFs o.O


We were tought to include braces ( { and } )on IFs, like
Code:
if (condition blablablb){
     shit here
}
else if (condition shit){
     look im between braces
}


It works your way too.. but I'm pretty sure that's "frowned upon" by the professionals :s
 
Updated my code to

Code:
public class Weight {
    public static void main(String[] args) {
        int weight;
        do {

            System.out.println("Enter your weight: ");
            Scanner input = new Scanner(System.in);
            weight = Integer.parseInt(input.nextLine());

            if(weight >= 50 && weight < 60){
                System.out.println("You're not fat at all.");
            }else if (weight>=60 && weight < 70){
                System.out.println("You're not fat.");
            }else if (weight>=70 && weight < 80){
                System.out.println("You're a bit fat.");
            }else if (weight>=80 && weight < 90){
                System.out.println("Loose some weight man.");
            }else if (weight>=90 && weight < 100){
                System.out.println("They see me rollin'");
            }else if (weight>=100 && weight < 150){
                System.out.println("MAN THE HARPOONS");
            }else if (weight>=150){
                System.out.println("You're dead");
            }else
                System.out.println("Hey mr skinny.");
        } while (weight < 999);

    }
}

Added the {}'s and it keeps asking the weight now until you enter 999.



I don't really get why case would be better? Wouldn't that make the code a lot bigger?
Wouldn't you need a case for every weight number?
 
Mave said:
... andit keeps asking the weight now until you enter 999.

I would have done that a different way; Create a new variable called flag, make it an integer, then set the loop to while (flag != 1), and make a new if statement that says like
Code:
if ("stop".equals input){
     flag = 1;
}
(Not sure if I did the .equals thing right, it's been a while)

So then it stops when you type in stop :tongue:

Ofc your way works too and is just as effective.



Mave said:
I don't really get why case would be better? Wouldn't that make the code a lot bigger?
Wouldn't you need a case for every weight number?

Ah, you're right, since it's a range of numbers you're checking (50 to 60 etc), and switch statements can only do cases for single numbers :tongue:
So yeah, for that you'd have to write out every number and that would suck. Way less efficient.
 
GPow69 said:
Mave said:
I don't really get why case would be better? Wouldn't that make the code a lot bigger?
Wouldn't you need a case for every weight number?
Ah, you're right, since it's a range of numbers you're checking (50 to 60 etc), and switch statements can only do cases for single numbers :tongue:
So yeah, for that you'd have to write out every number and that would suck. Way less efficient.

That is pretty gay o.o
I know that in some languages you can do
Code:
switch(i) {
    
    case 0>50:
     bin0();
    break; 
  
    case 50<100:
     bin1();
     break;
  }

This is taken straight from my 7-Segment counter, except I added the range instead of a single number.
What it does is execute a void (bin0 or bin1) and then return to the loop it's in (not displayed in this bit.)
 
GPow69 said:
Yeah, I don't think Java lets you use number ranges on Switches. (Not sure on this)
Hm... well you could change a value depending on your input value.
For example we have 2 integers: input for the input weight and x.
If input 0<50, set x=1
If input 50<100, set x=2
Then use x in your case.
 
Panki said:
GPow69 said:
Yeah, I don't think Java lets you use number ranges on Switches. (Not sure on this)
Hm... well you could change a value depending on your input value.
For example we have 2 integers: input for the input weight and x.
If input 0<50, set x=1
If input 50<100, set x=2
Then use x in your case.

That's a lot of work for the same outcome really :tongue:
 
Back
Top Bottom