This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

SPOJ - PRIME, got error java.util.NoSuchElementException on ideone

 
Murari Madkyto
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello guys, I just begin to learn algorithm, and I decide to practice in SPOJ.... I am in "PRIME1" problem for generete prime number from other typical range of number.

this is my code :



I try this code in my IDE (netbeans) and it can run, but when I submitted in spoj, it show runtime error = NZEC, and when I try in ideone.com it show :


I dont understand about this error, could you tell me how to solve this error ? please....

thank you.
 
Mansukhdeep Thind
Ranch Hand
Posts: 1158
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the ranch Murari.

Murari wrote: and when I try in ideone.com it show :
view plaincopy to clipboardprint?
Note: Text content in the code blocks is automatically word-wrapped

Exception in thread "main" java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Scanner.java:1585)
at Main.main(Main.java:10)



I dont understand about this error, could you tell me how to solve this error ? please....

thank you.


Yes, I will tell you how to solve it. Go to ideone.com. Read carefully how to use the online IDE. Then provide the correct input for your stdin stream, and then click on run. I just tried it and I am able to successfully execute the given code. You have to read it carefully.

Having said that, I have a question for you. Have you just copied it from the net or have you thought about the logic and then written the class yourself?
 
Murari Madkyto
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mansukhdeep Thind wrote:
Having said that, I have a question for you. Have you just copied it from the net or have you thought about the logic and then written the class yourself?


O my God, that's my wrong, thank you mansukh, it works in ideone... but when i try in spoj, the runtime error : NZEC still show. I make this code by myself base on my understanding about Sieve of Eratosthenes in this site : http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes .
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The first thing to do is get rid of ideone, which is not helping you at all. Run that code from the command line, or get a real IDE like Eclipse or NetBeans. At the beginner’s stage the command line is better.
Next, realise that the Sieve of Eratosthenes is designed to work from numbers starting 1. Your app ran happily for 12345 23456 and confidently told me that 23231 is prime, which it isn’t. It is 11 × 2111. 2111 is prime, however. If you want to run a Sieve, you need to start from 1 (or 0, then the array index can match the value). Remember 1 does not count, and 0 divides by every other number, so the smallest prime number is 2. Fill a boolean[] with true, set 0–1 to false, then iterate the array from every value which is still true, making all its multiples false.
You can use a BitSet instead of an array, if you prefer. I have a Sieve implementation which took approx 0.1sec to check primes up to 2200000, so it is not slow.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic