# Problem with school assignment. print hifive if multiple of 5 etc. PLZ HELPQUICK

• 09-16-2012, 05:11 AM
Rstuart970
Problem with school assignment. print hifive if multiple of 5 etc. PLZ HELPQUICK
Hello, I am currently taking my first java class ever and I have spent hours on trying to figure out what in my code is wrong. I only have until early tomorrow morning to have it complete any help would be much appreciated, I started reading these forums and I think I will be reading them as a part of my daily routine from now on. Now here is my assignment word for word.

3. (10 pts) Write a program that prompts the user to enter an integer. If the number is a multiple of 5, print HiFive. If the number is divisible by 2 or 3, print Georgia. Here are the sample runs:
<Output> Enter an integer: 6
Georgia
<End Output>
<Output> Enter an integer: 15
HiFive Georgia
<End Output>
<Output> Enter an integer: 25
HiFive
<End Output>

And here is my code I have for my final. I have a strong feeling it is where I have the final else if trying to print HiFive and Georgia in the case that it is a multiple of 5 and is divisible by 2 or 3. Now in my test run I enter the number 6 which should just print Georgia although, I am getting a print of Georgia and then another print on a new line with HiFive Georgia.

// Unit 1 Assignment - #3

import java.util.Scanner;

public class HiFiveInteger {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);
System.out.print("Enter an integer: ");
int num1 = input.nextInt();

// Check if multiple of 5
if (num1 % 5 == 0)
{
System.out.println("HiFive");

}

// Check if integer is a divisible of 2 or 3
else if (num1 % 2 == 0
|| num1 % 3 == 0)
{
System.out.println("Georgia");

}

// Check if both are true
else if (num1 % 5 == 0
&& num1 % 2 == 0 || num1 % 3 == 0);
{
System.out.println("HiFive Georgia");

}

}

}
• 09-16-2012, 08:21 AM
DarrylBurke
Re: Problem with school assignment. print hifive if multiple of 5 etc. PLZ HELPQUICK
• 09-16-2012, 08:47 AM
killutch
Re: Problem with school assignment. print hifive if multiple of 5 etc. PLZ HELPQUICK
Code:

```else if (num1 % 5 == 0 && num1 % 2 == 0 || num1 % 3 == 0); { System.out.println("HiFive Georgia"); }```
what your saying here is if a number divided by 5 and 2 has no denominate then your good

OR(||)

if a number divided by 3 has no denominater your good

6/3 = 2 with no denominater so therefor it prints "HiFive Georgia"

so the && will make it to where both conditions need to be true

the || means this or that needs to be true

so think of it like this
num1 % 5 == 0 and num1 % 2 == 0 or this num1 % 3 == 0

hope this helps
• 09-16-2012, 12:00 PM
JosAH
Re: Problem with school assignment. print hifive if multiple of 5 etc. PLZ HELPQUICK
The && operator has a higher precedence than the || operator, so A && B || C evaluates as (A && B) || C; that is not what you want, you want to evaluate it as A && (B || C) so you should put parentheses around the subexpression; oh, and get rid of that semi-colon at the end of that if-clause.

kind regards,

Jo
• 09-16-2012, 01:17 PM
Fubarable
Re: Problem with school assignment. print hifive if multiple of 5 etc. PLZ HELPQUICK
He has had this solved elsewhere in a cross-post but neglected to tell us. Very inconsiderate of him.
• 09-16-2012, 01:34 PM
flyingeagle
Re: Problem with school assignment. print hifive if multiple of 5 etc. PLZ HELPQUICK
Quote:

// Unit 1 Assignment - #3

import java.util.Scanner;

public class HiFiveInteger {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);
System.out.print("Enter an integer: ");
int num1 = input.nextInt();

// Check if multiple of 5
if (num1 % 5 == 0)
{
System.out.println("HiFive");

}

// Check if integer is a divisible of 2 or 3
if (num1 % 2 == 0
|| num1 % 3 == 0)
{
System.out.println("Georgia");

}

}

}
The above changed code should to the trick.