3 Replies - 520 Views - Last Post: 07 October 2013 - 04:06 AM Rate Topic: -----

#1 Goggz56  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 68
  • Joined: 25-March 11

equality statement isn't working as expected, please help

Posted 06 October 2013 - 05:47 PM

This really sucks because I thought this would be a really simple and easy project to do but apparently not. The program I need to write is really simple:

Write a program that asks the user for their name and age then if they are between 0 and 21 says no drinking for you, 21-65 hi ho hi ho its off to work you go, then 65 and over enjoy your retirement while the money lasts. Turn in the .java file of a zip of the entire project. Make sure to comment your code.

Well, I have most of the code taken care of but for some reason that I have spent the last hour racking my brain trying to figure it out to no avail.

Here is my code:

package project_1;

import java.util.Scanner;

public class Project_1 {

	public static void main(String[] args) {
		String name;
		int age;
		Scanner  data = new Scanner(System.in);
		
		System.out.print("Hello, please enter your name: ");
		name = data.next();
		
		System.out.print("\nThank you, now how old are you? ");
		age = data.nextInt();
		
		data.close();
		
		if (age < 21);
			System.out.println("\nSorry " +name+ " but no drinking for you.");
		
	        if (age >=21 && age <= 65);
			System.out.println("Hi ho hi ho its off to work you go!");
		


Going by logic, 22 is greater that 21 but yet anytime I type 22 or higher I still get the "no drinking for you" message. Also when I type anything less than 21 I get the "Hi ho" message from the second if statement. I don't understand why. Help please? Thanks

Btw, this program is written with Eclipse.

This post has been edited by Goggz56: 06 October 2013 - 05:51 PM


Is This A Good Question/Topic? 0
  • +

Replies To: equality statement isn't working as expected, please help

#2 ChrisNt  Icon User is offline

  • cute bug

Reputation: 225
  • View blog
  • Posts: 724
  • Joined: 31-July 13

Re: equality statement isn't working as expected, please help

Posted 06 October 2013 - 06:00 PM

Remove semicolons , you say
if(age < 21) DO NOTHING
print
if(age >=21 && age <= 65) DO NOTHING
print


if (age < 21);
if (age >=21 && age <= 65);

This post has been edited by ChrisNt: 06 October 2013 - 06:22 PM

Was This Post Helpful? 1
  • +
  • -

#3 Goggz56  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 68
  • Joined: 25-March 11

Re: equality statement isn't working as expected, please help

Posted 06 October 2013 - 06:03 PM

Thank you! I thought it would be something simple but I didn't realize it would be something THAT simple. Much thanks man.
Was This Post Helpful? 0
  • +
  • -

#4 manofax  Icon User is offline

  • New D.I.C Head

Reputation: 12
  • View blog
  • Posts: 20
  • Joined: 17-May 13

Re: equality statement isn't working as expected, please help

Posted 07 October 2013 - 04:06 AM

Instead of using two if statements as you have, you should us an if-else statement (better coding practice):

if (age < 21){
//do stuff
}else if(age >= 21 && age <= 65){
//do stuff
}else{
//do stuff
}

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1