Please Help me with problem "Divisible" in JAVA | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Please Help me with problem "Divisible" in JAVA

Hello, I would need your help. Have any of you solved the "Code Coach" tag with the "Divisible" tag in Java? It is at the "Medium" level but only for the Pro version. I fail in case 3, where it is locked. The problem is this: You need to know if a number is divisible by each of a group of other numbers. If you are given the number and the group of other numbers, you will test to make sure that it is divisible by all of them. Task: Test your number against all of the other numbers that you are given to make sure that it is divisible by them. Input Format: Two inputs: an integer value (the number you are testing) and a string of variable length of the integers that you should test against separated by spaces. Output Format: A string that says 'divisible by all' or 'not divisible by all'. Sample Input: 100 2 5 10 Sample Output: divisible by all Explanation: 100 is divisible by 2, 5, and 10.

23rd Feb 2022, 2:34 PM
Micheal Atridis
Micheal Atridis - avatar
15 Answers
+ 4
Micheal Atridis I used to Skip newline character nextInt() doesn't read the newline character of input so when we use nextLine() the scanner finds the newline character. So you have to do this. int x = sc.nextInt(); sc.nextLine(); //skip the newline character String str = sc.nextLine(); If you don't want to do that then use only next() method: int x = sc.nextInt(); String str = sc.next(); Both will work
23rd Feb 2022, 8:34 PM
A͢J
A͢J - avatar
+ 6
This was really surprising. I tried a lot, I failed a lot, tried AJs suggestion, realised that it can work. Until I tracked it all down to this one line: int c = Integer.parseInt(testNum); The third test case throws an exception because the line does not only contain a number. Doing int testNum = s.nextInt(); String str = s.nextLine(); // read rest of line if(str.isEmpty()) System.out.println("divisible by all"); fails the third test case, showing that the line is not empty after reading an int. Alternatively, the parseInt on the line can be wrapped in a try-catch. If an exception is thrown, print divisible by all, and it passes the third test case.
23rd Feb 2022, 6:22 PM
Ani Jona 🕊
Ani Jona 🕊 - avatar
+ 4
Yeah, using try-catch is a good idea. Since it's working without them, it's all about whitespace either side of the input. '' 4 5 or 4 5 '' By default, next() method trims it while nextLine() doesn't. You may find trim() method useful in this case. nextLine().trim()
24th Feb 2022, 2:19 AM
Simba
Simba - avatar
+ 3
According to the question, just this part of for loop is enough I guess. for(var x:arrOfNums){ int d=Integer.parseInt(x); if(c%d==0) sum++; }
23rd Feb 2022, 3:15 PM
Avinesh
Avinesh - avatar
+ 2
Micheal Atridis No need to use abs method because numbers are Integer
23rd Feb 2022, 3:38 PM
A͢J
A͢J - avatar
+ 2
What if there is a whitespace instead of integers for the second input?
23rd Feb 2022, 4:24 PM
Simba
Simba - avatar
+ 2
AJ Thank you all. I realized where I was wrong. But could you explain to me why you put this: int c = s.nextLine (); s.nextLine (); // This line? String divNums = s.nextLine (); What does it read at this point?
23rd Feb 2022, 7:04 PM
Micheal Atridis
Micheal Atridis - avatar
+ 1
Thank you but it still not working in case 3. Is anybody solve it?
23rd Feb 2022, 3:54 PM
Micheal Atridis
Micheal Atridis - avatar
+ 1
Micheal Atridis Don't take first input as a String. Your code is right but you just have to do some changes: Take input like this: int c = s.nextInt (); s.nextLine(); //this is just to ignore empty character. String divNums = s.nextLine();
23rd Feb 2022, 4:24 PM
A͢J
A͢J - avatar
+ 1
Micheal Atridis This is your solution: import java.util.*; public class Main { public static void main(String[] args) { Scanner s=new Scanner(System.in); int c = s.nextInt (); s.nextLine(); String divNums = s.nextLine(); String[] arrOfNums = divNums.split(" "); int sum = 0; //int c=Integer.parseInt(testNum); for(var x : arrOfNums) { int d = Integer.parseInt(x); if(c % d == 0) sum++; } if(sum == arrOfNums.length) System.out.println("divisible by all"); else System.out.println("not divisible by all"); } }
23rd Feb 2022, 4:29 PM
A͢J
A͢J - avatar
0
Can you share the link to your code? I think people would like to see it first, before they can suggest you something. Follow this guide to share links https://www.sololearn.com/post/75089/?ref=app
23rd Feb 2022, 2:45 PM
Ipang
0
import java.util.*; public class Main { public static void main(String[] args) { Scanner s=new Scanner(System.in); String testNum=s.nextLine (); String divNums=s.nextLine(); String[] arrOfNums=divNums.split(" "); int sum=0; int c=Integer.parseInt(testNum); for(var x:arrOfNums){ int d=Integer.parseInt(x); if(Math.abs(c)>=Math.abs(d)){ if(c%d==0) sum++; } else { if(d%c==0) sum++; } } if(sum== arrOfNums.length) System.out.println("divisible by all"); else System.out.println("not divisible by all"); } }
23rd Feb 2022, 3:05 PM
Micheal Atridis
Micheal Atridis - avatar
0
Make a code bit out of the most recently updated code, and share its link. We can't see what changes you made, nor how it affects (or not) the code behaviour.
23rd Feb 2022, 4:19 PM
Ipang
23rd Feb 2022, 4:26 PM
Micheal Atridis
Micheal Atridis - avatar
0
Hello everyone, (Late post) I was able to clear all the test cases, below is code (issue was with reading 1st number) import java.util.*; public class Program { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String numStr = sc.nextLine().trim(); int num = Integer.parseInt(numStr); String strs = sc.nextLine().trim(); boolean flag = true; if(!strs.isEmpty()){ String[] nums = strs.split(" "); for(String str : nums){ try{ int val = Integer.parseInt(str); if(num % val != 0 || val == 0){ flag = false; break; } }catch(Exception e){ flag = false; break; } } } String res = (flag)?"divisible by all":"not divisible by all"; System.out.print(res); } }
12th Mar 2023, 2:41 PM
H SHREESHA BHARADWAJ
H SHREESHA BHARADWAJ - avatar