Getting an weird error.... | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Getting an weird error....

String p="12"; int a=Integer.nextInt(p); getting this error- .nextInt(); ^illegal start of expression. getting the same error if i use Integer.valueOf(p); can anybody tell me why??

28th Mar 2018, 1:49 PM
Sagnik Majumder2
Sagnik Majumder2 - avatar
15 Answers
+ 11
Both Nathan and Palanski are right. Also, there are some little mistakes including array index range. The later part seemed complex, so I changed it in my own way. Feel free to knock if you have any question :) https://code.sololearn.com/c0oB8rDC7kZ0/?ref=app
28th Mar 2018, 3:12 PM
Shamima Yasmin
Shamima Yasmin - avatar
+ 9
By definition, a prime number shouldn't have any other divisor except 1 and the number itself. So while checking if the number is prime or not, don't check 1 and that number. That's why the loop range should be like for (i=2; i<number; i++) // start from 2, stop before number Now, consider a particular number, say 12. 12's divisors: 1,2,3,4,6,12 excluding 1 and 12: 2,3,4,6 See, there's no divisor after 6 which is number/2. so we can skip the later numbers. This is not mandatory but efficient. In your code, you're checking divisibility by 2. Instead, you have to check whether the numbers are divisible by (2.... number-1 or number/2) or not.
28th Mar 2018, 3:40 PM
Shamima Yasmin
Shamima Yasmin - avatar
+ 5
Working fine here, did I miss anything? https://code.sololearn.com/cOyy5tXFdrIS/?ref=app
28th Mar 2018, 2:04 PM
Shamima Yasmin
Shamima Yasmin - avatar
+ 2
There is no Integer.nextInt() function. nextInt() belongs to scanner class instead. Use Integer.parseInt(p);
28th Mar 2018, 1:57 PM
cHiRaG GhOsH
cHiRaG GhOsH - avatar
+ 2
use parseInt() instead of nextInt(). Your assign a string into integer that's why you get an error. Convert it into integer.
28th Mar 2018, 2:01 PM
Jonathan T Rantael
Jonathan T Rantael - avatar
+ 2
remove public in "public int reverse1", and in your for loop there's an error. Mispelled declaration of int. It must be int j = 3 not intj = 3
28th Mar 2018, 2:41 PM
Jonathan T Rantael
Jonathan T Rantael - avatar
+ 2
I think its the "*" that's causing the error input number: 9 for example when this line has been reached: int reverse1 = Integer.parseInt(*9); we get an error...
28th Mar 2018, 3:02 PM
Heji Palaña
Heji Palaña - avatar
+ 1
Shamima Yasmin i copied what you did but i am still getting the same illegal start of expression error. I will share the code. I am writing a program to find if a number is twisted prime or not eg: 167 and 761 both are primes https://code.sololearn.com/cXwMc2Pe9IpD/?ref=app
28th Mar 2018, 2:12 PM
Sagnik Majumder2
Sagnik Majumder2 - avatar
+ 1
nextInt() is method of Scanner object you cannot call that from an Integer class
28th Mar 2018, 2:26 PM
Heji Palaña
Heji Palaña - avatar
+ 1
Palanski i updated the question
28th Mar 2018, 2:31 PM
Sagnik Majumder2
Sagnik Majumder2 - avatar
+ 1
Palanski thankx it worked.... understood my mistake
28th Mar 2018, 3:11 PM
Sagnik Majumder2
Sagnik Majumder2 - avatar
+ 1
my pleasure...
28th Mar 2018, 3:14 PM
Heji Palaña
Heji Palaña - avatar
+ 1
Shamima Yasmin in the for loop why did you do number/2.
28th Mar 2018, 3:17 PM
Sagnik Majumder2
Sagnik Majumder2 - avatar
+ 1
Shamima Yasmin here's my updated code why am i getting no output. https://code.sololearn.com/cXwMc2Pe9IpD/?ref=app
28th Mar 2018, 3:18 PM
Sagnik Majumder2
Sagnik Majumder2 - avatar
0
cHiRaG GhOsH i am so sorry i made a small mistake in the question.... i used parseInt(); but still keep getting the same error....
28th Mar 2018, 2:00 PM
Sagnik Majumder2
Sagnik Majumder2 - avatar