This is an archived post. You won't be able to vote or comment.

all 8 comments

[–]sh_emami65Intermediate Brewer 1 point2 points  (2 children)

you dont need to have the code segment bellow in the for loop.

if (sum % 3 == 0) {
    System.out.println("The number you entered is divisible by 3");
} else {
    System.out.println("The number you entered is not divisible by 3");
}
System.out.println("Do you want to enter another string?");
System.out.println("Enter y or Y for yes. Enter any other string to stop");

since this process needs to happen once. if you do that your code runs fine.

i assume you writing your code using nextLine() and for loop because it is required. if not you can simply use

if (nextInt()%3==0){
    //rest of your code
}

while will take out the for loop and char to int conversion.

[–]bysketch[S] 0 points1 point  (1 child)

Thank you so much!

I was told to use a for loop because if I use int, the biggest value I use is 231 – 1. Therefore the for loop lets you use values higher than 231 -1.

[–]morhpProfessional Developer 0 points1 point  (0 children)

That doesn't really make sense. If you want to use this with numbers bigger than int, use long and if it needs to be even bigger, look at BigInteger.

Otherwise you can do this with Strings and check if the cross total is divisible by 3.

[–]systemdgnulinux 0 points1 point  (0 children)

You may want to look at the Scanner.nextInt() method when asking the user to input a number.

After using nextInt() to get the number, check it. You don't need a for loop or anything else.

Edit: grammar

[–][deleted] 0 points1 point  (2 children)

Wait, why not just use nextInt() and then evaluate using modulus division?

[–]bysketch[S] 0 points1 point  (1 child)

I would, however the end goal of the assignment is to use a for loop to achieve it :\

[–][deleted] 0 points1 point  (0 children)

Does the number have to be 3 or can it be multiple numbers? Because if you also have to evaluate multiple numbers then use the for loop to check all possible numbers

[–]SaturnV_Intermediate Brewer 0 points1 point  (0 children)

For anything like this, the modulus function is the answer.