![]() |
|
#1
|
|||
|
|||
Lost...Hi, this program is "supposed" to take an integer from a user, decide if it's prime and write it to a file if it is. It's supposed to have a method in it called "isPrime," which I'm not sure I'm doing right either. I'm having so many problems with this, and feel like I'm going in circles. Any help?
JAVA Code:
Last edited by LuciWiz : 16-May-2008 at 04:07.
Reason: Please insert your Java code between [java] & [/java] tags
|
|
#2
|
||||
|
||||
Re: Lost...Quote:
What you've posted has errors: Code:
The first three are related. The difference being in how a function is CALLED vs. how a function is DEFINED. First, take note in how Java's error list provides the file [class] name and the line number. Java is typically very helpful with where it suspects problems. So at line 20, you are making a CALL to function. When calling a function, the parameter's type is not passed along with the value. Thus at line 20, the 'int' before number is the problem at that line. Removing the 'int' at line 20 reduces the errors to: Code:
Notice what's missing? What is the type of 'isPrime' supposed to be? I'll guess boolean, since that is how it appears to be used. Now the error is: Code:
So, I'll let you fix that, but then let's also take a small look at main, as you kinda have the 'cart-before-the-horse', so to speak. Think about how the flow there as it stands right now: 1. isPrime (see comment #1, below, about this line) 2. displayResult 3. declare/create a Scanner 4. get a number Thinking in that order, how are the first two items expected to use the value at/from #4? The intent, I believe, is to: 1. Declare/create the Scanner 2. get a number 3. check if number is prime 4. displayResult 1. This statement, although not an error, is pointless. JAVA Code:
JAVA Code:
Note that there will be a couple of more errors after fixing the if/else to resolve, but hopefully this will give a good start. Use the error's line numbers to help determine the problem. Post again (to this same thread) if you still run into issues. __________________
Use the force...read the source!! WYCIWYG -- what you code is what you get! Last edited by TurboPT : 16-May-2008 at 07:09.
|
|
#3
|
|||
|
|||
Re: Lost...Thanks for your help. I was able to get all of those errors fixed, but now I have an error that tells me "variable isPrime might not have been initialized" for the "return isPrime." I'm not exactly sure what that means, because I thought it was initialized earlier. Help?
JAVA Code:
Last edited by admin II : 19-May-2008 at 20:57.
Reason: Please surround your Java code with [java] your code [/java]
|
|
#4
|
||||
|
||||
Re: Lost...Inside isPrime(), the variable 'isPrime' has NOT been initialized, only declared.
The compiler complains, because it is theoretically possible for 'isPrime' to be return uninitialized -- because if the parameter 'number' is passed-in with a zero (or possibly less than zero), the loop won't execute, and thus 'isPrime' is left unassigned. __________________
Use the force...read the source!! WYCIWYG -- what you code is what you get! |
|
#5
|
|||
|
|||
Re: Lost...So how do I fix it?
|
|
#6
|
||||
|
||||
Re: Lost...Give it a value:
JAVA Code:
__________________
Use the force...read the source!! WYCIWYG -- what you code is what you get! |
|
#7
|
|||
|
|||
Re: Lost...Okay, thanks. I was confused about declaring it versus initializing it. Now the program says there aren't any errors, but it doesn't print out whether or not the number is prime after I type in a number, and it's not writing it to a file. Did I leave something else out?
|
|
#8
|
|||
|
|||
Re: Lost...Okay, I now have it printing out whether or not the number is prime, but it isn't writing it to a file. I moved the while loop to after the else, so it wasn't interrupting the if-else process. Did I put it in the wrong spot?
|
|
#9
|
||||
|
||||
Re: Lost...That is not the problem, you have an endless loop:
JAVA Code:
__________________
Use the force...read the source!! WYCIWYG -- what you code is what you get! |
|
#10
|
||||
|
||||
Re: Lost...Quote:
That's why it appeared as if nothing printed, it can't continue on to other statements. __________________
Use the force...read the source!! WYCIWYG -- what you code is what you get! |
Recent GIDBlog
Last Week of IA Training by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Bring back lost file | terry brian | Computer Software Forum - Windows | 4 | 14-Apr-2007 10:59 |
| Iterator,container,copy Lost | FFCD | CPP / C++ Forum | 7 | 28-Nov-2006 16:47 |
| Lost driver to cd writer | femme | Computer Hardware Forum | 7 | 09-Jan-2005 17:15 |
| Session Variables lost by Mac? | shrdlu | MySQL / PHP Forum | 1 | 06-Mar-2004 22:55 |
| Please Help! Very Lost! | rjd72285 | CPP / C++ Forum | 0 | 07-Nov-2003 15:01 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The