Hey guys,

I've constructed this program and I have this error that I don't understand.

What I'm trying to do is have a user type in a positive number and then it counts up to that number. If the number is divisible by 3 or by 5, it prints Beep instead. If the number is divisble by 3 and 5 it prints Beep Beep instead.

Here is my program so far:

//BeepBeep.java
//

import java.util.Scanner;

public class BeepBeep
{
    // prompts for and reads in positive integer limit
    //
    // prints out each number from 1 up to limit unless
    // the number is divisible by 3 or divisble by 5.
    // for numbers divisble by 3 and only or by 5, it prints Beep 
    // instead of the number, for numbers divisble by 3 and by 5 
    // it prints Beep-Beep instead of the number.
    
    public static void main(String[] args)
    {
        Scanner Scan = new Scanner (System.in);
        
        int number;
        
        System.out.println("What positive integer should we count up to?");
        number = Scan.nextInt();
        
        System.out.println("BeepBeep to"+number+":");
        
            int count = 0;
            while ( count < number)
                {
                    if ( number / 3 || 5)
                    {
                        System.out.println("Beep");
                    }
                    else if ( number / 3 && 5)
                    {
                        System.out.println("Beep-Beep");
                    }
                }
                count ++;
    }
}

Errors when compiled:

BeepBeep.java:30: operator || cannot be applied to int,int
if ( number / 3 || 5)
^
BeepBeep.java:34: operator && cannot be applied to int,int
else if ( number / 3 && 5)
^
2 errors

----jGRASP wedge: exit code for process is 1.
----jGRASP: operation complete.


Thanks in advance!

Wootens

If the number is divisible by 3 or by 5

if (number%3==0 || number%5==0)

If the number is divisble by 3 and 5

if (number%3==0 && number%5==0)

Cheers, it compiles now. :)

Although when I run it, I get an endless run, plus its not counting numbers and changing any of the divisble ones to beep and beep.

Any suggestions?

//BeepBeep.java
//

import java.util.Scanner;

public class BeepBeep
{
    // prompts for and reads in positive integer limit
    //
    // prints out each number from 1 up to limit unless
    // the number is divisible by 3 or divisble by 5.
    // for numbers divisble by 3 and only or by 5, it prints Beep 
    // instead of the number, for numbers divisble by 3 and by 5 
    // it prints Beep-Beep instead of the number.
    
    public static void main(String[] args)
    {
        Scanner Scan = new Scanner (System.in);
        
        int number;
        
        System.out.println("What positive integer should we count up to?");
        number = Scan.nextInt();
        
        System.out.println("BeepBeep to"+number+":");
        
            int count = 0;
            while ( count < number)
                {
                    if (number%3==0 || number%5==0)
                    {
                        System.out.println("Beep");
                    }
                    else if (number%3==0 && number%5==0) 
                    {
                        System.out.println("Beep-Beep");
                    }
                }
                count ++;
    }
}

Cheers,

Wootens

May I suggest you use the selection structure where the final selection will be associated with while loop to print a series of number up to the limit.
(1)The cases "Beep" and "Beep-Beep" should no be placed withing the while loop since they are each individual case respectively which should be done once, i.e. just print Beep or Beep-Beep.
Hence, they should be associated with the selection structure, such as

if (number%3==0 && number%5==0) 
             System.out.println("Beep-Beep");
             else if (number%3==0 || number%5==0)
             System.out.println("Beep");

(2) be noticed that the case : if (number%3==0 && number%5==0) should be the first case to be checked. (Why?)
(3) The case, which neither-("Beep") nor ("Beep-Beep"), should be the third selection in the if else structure. In the third selection you may use while loop to print out a series of count number. Therefore the third selection could be the while loop:

else while(count<=number)
     System.out.println(count ++);

Edited 6 Years Ago by tong1: n/a

I looked over what you said and I'm a bit confused. I made some changes and it stopped the endless loop but still not changing numbers divisble by 3 only or 5 to Beep and numbers divisble by 3 and 5 to BeepBeep.

//BeepBeep.java
//

import java.util.Scanner;

public class BeepBeep
{
    // prompts for and reads in positive integer limit
    //
    // prints out each number from 1 up to limit unless
    // the number is divisible by 3 or divisble by 5.
    // for numbers divisble by 3 and only or by 5, it prints Beep 
    // instead of the number, for numbers divisble by 3 and by 5 
    // it prints Beep-Beep instead of the number.
    
    public static void main(String[] args)
    {
        Scanner Scan = new Scanner (System.in);
        
        int number;
        
        System.out.println("What positive integer should we count up to?");
        number = Scan.nextInt();
        
        System.out.println("BeepBeep to "+number+":");
        
        
                {
                    if (number%3==0 || number%5==0)
                    {
                        System.out.println("Beep");
                    }
                    else if (number%3==0 && number%5==0) 
                    {
                        System.out.println("Beep-Beep");
                    }
                }
                    int count = 0;
                    while(count<=number)
                    System.out.println(count ++);
    }
}

----jGRASP exec: java BeepBeep
What positive integer should we count up to?
17
BeepBeep to 17:
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

----jGRASP: operation complete.

Edited 6 Years Ago by Wootens: n/a

(1) in the selection structure of "if else" you should place the "Beep-Beep" case (if (number%3==0 && number%5==0)...) as the first selection, then "else if (number%3==0 || number%5==0)" for the case "Beep" because the condition for "Beep-Beep" always meets the condition for "Beep".

if (number%3==0 && number%5==0) //the "Beep-Beep" case should be placed as the first selection since its condition always meets the condition for the next selection.
System.out.println("Beep-Beep");
else if (number%3==0 || number%5==0)  // the second selection , i.e. the "Beep" case
System.out.println("Beep");

(2) For the above two cases you have no need to print up the numbers from 0 up to the limit. Only in the third case each number from 1 up to limit has to be printed.

(3) therefore, the case where each number from 1 up to limit has to be printed should be your third selection:

else {  // the third selection/case
int count = 0;
while(count<=number)
System.out.println(count ++);
}

Edited 6 Years Ago by tong1: n/a

Hold on a minute here - tong1 - are you sure you are answering the original requirement here?
Wootens: go back to the version in your post that starts "Cheers, it compiles now", that's almost perfect, its just that you increment count OUTSIDE the loop when, obviously, it should be incremented INSIDE the loop (see lines 38/39), and you need to print the number in a final else clause if it's not beep or beepbeep

Edited 6 Years Ago by JamesCherrill: n/a

I put the Count back in the loop (its not endless anymore), but alas it still doesn't work properly.

I'm not sure on how the final else, as when I change it I get an error on line 34 stating that it's an illegal start of type.

It's still not counting properly :3

//BeepBeep.java
//

import java.util.Scanner;

public class BeepBeep
{
    // prompts for and reads in positive integer limit
    //
    // prints out each number from 1 up to limit unless
    // the number is divisible by 3 or divisble by 5.
    // for numbers divisble by 3 and only or by 5, it prints Beep 
    // instead of the number, for numbers divisble by 3 and by 5 
    // it prints Beep-Beep instead of the number.
    
    public static void main(String[] args)
    {
        Scanner Scan = new Scanner (System.in);
        
        int number;
        
        System.out.println("What positive integer should we count up to?");
        number = Scan.nextInt();
        
        System.out.println("BeepBeep to "+number+":");
                
                int count = 0;
                while(count<=number)
                count ++;

                {
                    if (number%3==0 || number%5==0)
                    {
                        System.out.println("Beep");
                    }
                    else if (number%3==0 && number%5==0) 
                    {
                        System.out.println("Beep-Beep");
                    }
                }
                
    }
}

Small syntax error - you put the count++ OUTSIDE the {} for the while loop, it's before the opening {, so the compiler sees

while(count<=number) count ++; // this is a valid complete while loop
{ // this isn't valid now

what you should have is

while(count<=number)
{
count ++;
etc

ps: you reversed the order of the if tests compared to tong's code - this is inportant.

Edited 6 Years Ago by JamesCherrill: n/a

Oo alright, made a change now.

Although its not counting numbers and replacing those divisible by 3 or 5 with Beep and those divisble by 3 and 5 with printprint. Not sure what to do to fix that :3

//BeepBeep.java
//

import java.util.Scanner;

public class BeepBeep
{
    // prompts for and reads in positive integer limit
    //
    // prints out each number from 1 up to limit unless
    // the number is divisible by 3 or divisble by 5.
    // for numbers divisble by 3 and only or by 5, it prints Beep 
    // instead of the number, for numbers divisble by 3 and by 5 
    // it prints Beep-Beep instead of the number.
    
    public static void main(String[] args)
    {
        Scanner Scan = new Scanner (System.in);
        
        int number;
        
        System.out.println("What positive integer should we count up to?");
        number = Scan.nextInt();
        
        System.out.println("BeepBeep to "+number+":");
                
                int count = 0;
                while(count<=number)

                {
                    count ++;

                    if (number%3==0 || number%5==0)
                    {
                        System.out.println("Beep");
                    }
                    else if (number%3==0 && number%5==0) 
                    {
                        System.out.println("Beep-Beep");
                    }
                }
                
    }
}

I've typed in the number 17 but it just prints BeepBeep to 17:

I'm trying to make it count to 17 and then change those divisble numbers with beep or beep beep.

Sorry I'm a bit of a noob, still learning >.<

Which values are you testing on lines 33 and 37 ??? ;-)
I didn't type this just for fun. Read those lines again. Think about this question.

*facepalm*

The number never changes, I changed it to count. :P

Got it to work, thanks for the help!

This question has already been answered. Start a new discussion instead.