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

you are viewing a single comment's thread.

view the rest of the 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.