Author Topic: [Java] Combining else if & user input.  (Read 4399 times)

0 Members and 1 Guest are viewing this topic.

Online Mave

  • TMS Founder
  • Administrator
  • Godfather
  • *********
  • Posts: 124453
[Java] Combining else if & user input.
« on: October 24, 2011, 06:33 »
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: [Select]
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: [Select]
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: [Select]
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:

Offline Panki

  • VIP
  • Ultimate Member
  • *
  • Posts: 12567
  • 0x1A4 every day
Re: [Java] Combining else if & user input.
« Reply #1 on: October 24, 2011, 07:14 »
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?

Offline GPow69

  • consololol ಠ_ಠ
  • Global Moderator
  • Ultimate Member
  • ********
  • Posts: 18856
  • EDM for life ♥
Re: [Java] Combining else if & user input.
« Reply #2 on: October 24, 2011, 08:13 »
Here:

Code: [Select]
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 :D

Online Mave

  • TMS Founder
  • Administrator
  • Godfather
  • *********
  • Posts: 124453
Re: [Java] Combining else if & user input.
« Reply #3 on: October 24, 2011, 08:17 »
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.
« Last Edit: October 24, 2011, 08:19 by Mave »

Offline GPow69

  • consololol ಠ_ಠ
  • Global Moderator
  • Ultimate Member
  • ********
  • Posts: 18856
  • EDM for life ♥
Re: [Java] Combining else if & user input.
« Reply #4 on: October 24, 2011, 08:27 »
Ah, right. That's something I didn't catch before, it's pretty sneaky :P

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: [Select]
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: [Select]
  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")

Online Mave

  • TMS Founder
  • Administrator
  • Godfather
  • *********
  • Posts: 124453
Re: [Java] Combining else if & user input.
« Reply #5 on: October 24, 2011, 08:31 »
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 :P)
Going to add a condition, too lazy to turn everything around.

And also what did you mean with the &&||? :P

Offline Panki

  • VIP
  • Ultimate Member
  • *
  • Posts: 12567
  • 0x1A4 every day
Re: [Java] Combining else if & user input.
« Reply #6 on: October 24, 2011, 08:34 »
Wouldn't it be smarter to use a case if?

Offline GPow69

  • consololol ಠ_ಠ
  • Global Moderator
  • Ultimate Member
  • ********
  • Posts: 18856
  • EDM for life ♥
Re: [Java] Combining else if & user input.
« Reply #7 on: October 24, 2011, 08:34 »
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)



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

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




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 :P
« Last Edit: October 24, 2011, 08:38 by GPow69 »

Offline Panki

  • VIP
  • Ultimate Member
  • *
  • Posts: 12567
  • 0x1A4 every day
Re: [Java] Combining else if & user input.
« Reply #8 on: October 24, 2011, 08:42 »
Exactly, except that you set the cases to 0>51 etc :3

Offline GPow69

  • consololol ಠ_ಠ
  • Global Moderator
  • Ultimate Member
  • ********
  • Posts: 18856
  • EDM for life ♥
Re: [Java] Combining else if & user input.
« Reply #9 on: October 24, 2011, 09:11 »
Yeah, that's a bit more advanced in Java :P

Offline Impulse

  • Global Moderator
  • Master Member
  • ********
  • Posts: 7848
  • LozL
Re: [Java] Combining else if & user input.
« Reply #10 on: October 24, 2011, 09:18 »
Ah, right. That's something I didn't catch before, it's pretty sneaky :P

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: [Select]
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: [Select]
  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: [Select]
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.

Offline GPow69

  • consololol ಠ_ಠ
  • Global Moderator
  • Ultimate Member
  • ********
  • Posts: 18856
  • EDM for life ♥
Re: [Java] Combining else if & user input.
« Reply #11 on: October 24, 2011, 09:27 »
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: [Select]

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

Online Mave

  • TMS Founder
  • Administrator
  • Godfather
  • *********
  • Posts: 124453
Re: [Java] Combining else if & user input.
« Reply #12 on: October 25, 2011, 08:25 »
Updated my code to

Code: [Select]
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?

Offline GPow69

  • consololol ಠ_ಠ
  • Global Moderator
  • Ultimate Member
  • ********
  • Posts: 18856
  • EDM for life ♥
Re: [Java] Combining else if & user input.
« Reply #13 on: October 25, 2011, 10:48 »
... 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: [Select]
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 :P

Ofc your way works too and is just as effective.



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 :P
So yeah, for that you'd have to write out every number and that would suck. Way less efficient.

Offline Panki

  • VIP
  • Ultimate Member
  • *
  • Posts: 12567
  • 0x1A4 every day
Re: [Java] Combining else if & user input.
« Reply #14 on: October 25, 2011, 02:49 »
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 :P
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: [Select]
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.)
« Last Edit: October 25, 2011, 02:55 by Panki »

Offline GPow69

  • consololol ಠ_ಠ
  • Global Moderator
  • Ultimate Member
  • ********
  • Posts: 18856
  • EDM for life ♥
Re: [Java] Combining else if & user input.
« Reply #15 on: October 25, 2011, 02:52 »
Yeah, I don't think Java lets you use number ranges on Switches. (Not sure on this)

Offline Panki

  • VIP
  • Ultimate Member
  • *
  • Posts: 12567
  • 0x1A4 every day
Re: [Java] Combining else if & user input.
« Reply #16 on: October 25, 2011, 02:57 »
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.

Offline GPow69

  • consololol ಠ_ಠ
  • Global Moderator
  • Ultimate Member
  • ********
  • Posts: 18856
  • EDM for life ♥
Re: [Java] Combining else if & user input.
« Reply #17 on: October 25, 2011, 03:06 »
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 :P

Online Mave

  • TMS Founder
  • Administrator
  • Godfather
  • *********
  • Posts: 124453
Re: [Java] Combining else if & user input.
« Reply #18 on: October 25, 2011, 05:06 »
Thanks for the additional info guys:D