• Post Reply Bookmark Topic Watch Topic
  • New Topic

Find multiples  RSS feed

 
Shal Lango
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Write a method  isMultiple that determines, for a pair of integers , whether  the second integer  is a multiple of the first. The method  should take two integer  arguments  and return true  if the second is a multiple of the first and false  otherwise. [Hint: Use the remainder operator .] Incorporate this method  into an application that inputs a series of pairs of integers  (one pair at a time) and determines whether  the second value  in each pair is a multiple of the first.har()

This is what I have so far:


Error attached below. Anyone have any idea what I'm doing wrong here? Thank you
int.JPG
[Thumbnail for int.JPG]
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the problem could be the formatting.
You have
Enter one number:Enter a second number:
on one line. Maybe they require it on multiple lines as
Enter one number:
Enter a second number:

You can confirm this with your professor.

Some minor nitpicks

Instead of

use


Intead of

use


Instead of

use


When the requirement is
return true  if the second is a multiple of the first and false  otherwise.

why are you checking both conditions


What do you think will happen if the user enters N (instead of n) in this code?
 
Shal Lango
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your response! So I tried to change my code according to your suggestions


Errors:
Multiples.java:52: error: illegal start of expression
         return if(x % y == 0 || y % x == 0)
                ^
Multiples.java:52: error: not a statement
         return if(x % y == 0 || y % x == 0)

int.JPG
[Thumbnail for int.JPG]
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Errors:
Multiples.java:52: error: illegal start of expression
         return if(x % y == 0 || y % x == 0)
                ^
Multiples.java:52: error: not a statement
         return if(x % y == 0 || y % x == 0)


Oops! My bad.
It should be
 
Shal Lango
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you kind sir!
Why is my code not going into the do you want to enter another pair loop?

int.JPG
[Thumbnail for int.JPG]
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you please copy paste the error text? Images are not searchable and for my eyes too small to read properly.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your check for multiple is incorrect. The order of the values is significant. Given (3, 9), the method is expected to return false because of course, 3 is not a multiple of 9. Given (9, 3), however, it should return true because 9 is in fact a multiple of 3.

Also, look out for if statements where you *try* to compare a boolean variable with true or false but end up making a common mistake by doing so. Hint:a single = is assignment; a double == is checking for primitive value equality or reference equality.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Maneesh: I don't think OP is getting the kind of errors you might think she's getting. Her code fails some automated tests that her instructor apparently uses to check submissions.
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah! Automated tests! Not it is starting to make sense. Thanks for pointing this out 
 
Liutauras Vilda
Sheriff
Posts: 4923
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maneesh Godbole wrote:Some minor nitpicks

Instead of
use

It isn't nitpick. That is a plain logical error in OP's code. Luckily you have noticed and pointed that out and lately showed a technique how to avoid it.
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To be fair I did NOT notice the = instead of '=='
*Hides in corner
 
Knute Snortum
Sheriff
Posts: 4281
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shalmika Elango wrote:Why is my code not going into the do you want to enter another pair loop?

I think the output analyzer is very picky and doesn't like that you have a line feed at the end of your output.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:. . . I think the output analyzer is very picky and doesn't like that you have a line feed at the end of your output. . . .
If that is true, then I think the analyser is being unfairly picky about that. I usually use print rather than println for such prompt messages myself, however.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!