GIDForums  

Go Back   GIDForums > Computer Programming Forums > Java Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 15-May-2008, 21:02
javachallenged javachallenged is offline
New Member
 
Join Date: May 2008
Posts: 6
javachallenged is on a distinguished road

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:
import java.util.Scanner;
import java.io.*;

public class primeToFile
{
   public static void main (String[ ] args) 
   {
  
      
 		//variables
	
     	String input;        // To hold keyboard input
      	String message;      // Message to display
      	int number;          // Number to check for prime 
   		boolean isPrime;
      	
      	
      isPrime = isPrime;
      
      displayResults(isPrime, int number);
      
      //Create Scanner object for reading input.
      Scanner keyboard = new Scanner(System.in);
      
     
      
      // Get the number.
   		System.out.print("Enter an integer to determine if it is prime or not.");
     	number = keyboard.nextInt();
     	
   }
      // Determine whether it is prime or not.
      	public static boolean isPrime(int number)
      	
   {
      		boolean isPrime;
      		for (int x=2; x < number; x++)
 
		{
			if (number%x<0)
				isPrime = true;
			else
				isPrime = false;	
      	}
      	return isPrime; 
      	
   }
   public static void displayResults(isPrime, int number)   throws IOException
   {
   		String message; 		//To hold message
   		
   		
   			
      if (isPrime)
         message = number + " is a prime number.";
         
	   			while (number <=100 && number >=1);
   			{
	   			
	   			 //Open the file.
	   			PrintWriter outputFile = new PrintWriter("PrimeNumbers.txt");
	   			
	   			//Write number to the file.
	   			outputFile.println("PrimeNumbers.txt");
	   			
	   			//Close the file.
	   			outputFile.close();
	   			System.out.println("Number written to file.");
   			}
         
      else
         message = number + " is not a prime number.";
   
      // Display a message.
      System.out.print(message);
                      
      System.exit(0);
   }
 
}//end main
Last edited by LuciWiz : 16-May-2008 at 04:07. Reason: Please insert your Java code between [java] & [/java] tags
  #2  
Old 16-May-2008, 06:27
TurboPT's Avatar
TurboPT TurboPT is offline
Senior Member
 
Join Date: Feb 2006
Location: Atlanta, GA
Posts: 1,233
TurboPT is a jewel in the roughTurboPT is a jewel in the roughTurboPT is a jewel in the rough

Re: Lost...


Quote:
Originally Posted by javachallenged
Any help?
Sure.

What you've posted has errors:
Code:
C:\Documents and Settings\user\Desktop\OTH\primeToFile.java:20: '.class' expected displayResults(isPrime, int number); ^ C:\Documents and Settings\user\Desktop\OTH\primeToFile.java:20: ')' expected displayResults(isPrime, int number); ^ C:\Documents and Settings\user\Desktop\OTH\primeToFile.java:48: <identifier> expected public static void displayResults(isPrime, int number) throws IOException ^ C:\Documents and Settings\user\Desktop\OTH\primeToFile.java:79: ')' expected ^ 4 errors
So lets look at these: (it's important to work them from top to bottom, as some of the 'lower' errors in the list may sometimes clear after fixing the 'upper' errors in the list)

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:
C:\Documents and Settings\user\Desktop\OTH\primeToFile.java:48: <identifier> expected public static void displayResults(isPrime, int number) throws IOException ^ C:\Documents and Settings\user\Desktop\OTH\primeToFile.java:79: ')' expected ^ 2 errors
The first error here [at line 48], was also listed with the first four, and is where the function 'displayResults' is DEFINED. At definition, the types must be specified.
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:
C:\Documents and Settings\user\Desktop\OTH\primeToFile.java:71: 'else' without 'if' else ^ 1 error
I'll let you fix the logic in this one, but it is due to the fact of having a loop located between the if and else in such a way that it breaks the else match to the if. (Did you notice that the error at line 79 went away after fixing the line 48 problem? That's 'why' as to fixing error(s) top-down, some may go away.)

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:
      isPrime = isPrime;
I believe the intent is to to check that 'number' [once retrieved] is prime or not, using the isPrime(...) function to determine:
JAVA Code:
      isPrime = isPrime( number );

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  
Old 16-May-2008, 17:35
javachallenged javachallenged is offline
New Member
 
Join Date: May 2008
Posts: 6
javachallenged is on a distinguished road

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:
import java.util.Scanner;
import java.io.*;
 
public class primeToFile
{
public static void main (String[ ] args) throws IOException
{
 
        //variables
 
    String input; // To hold keyboard input
    String message; // Message to display
    int number; // Number to check for prime 
        boolean isPrime;
 
     //Create Scanner object for reading input.
    Scanner keyboard = new Scanner(System.in);
 
     // Get the number.
        System.out.print("Enter an integer to determine if it is prime or not.");
    number = keyboard.nextInt();
 
    isPrime = isPrime(number);
 
    displayResults(isPrime, number);
 
}
// Determine whether it is prime or not.
    public static boolean isPrime(int number)
 
{
        boolean isPrime;
        for (int x=2; x < number; x++)
 
        { 
            if (number%x<0)
                isPrime = true;
            else
                isPrime = false;    
    }
    return isPrime; 
 
}
public static void displayResults(boolean isPrime, int number) throws IOException
{
        String message;         //To hold message
 
 
 
if (isPrime)
message = number + " is a prime number.";
 
else
message = number + " is not a prime number.";
 
while (number <=100 && number >=1);
            {
 
                  //Open the file.
                 PrintWriter outputFile = new PrintWriter("PrimeNumbers.txt");
 
                 //Write number to the file.
                 outputFile.println("PrimeNumbers.txt");
 
                 //Close the file.
                 outputFile.close();
                 System.out.println("Number written to file.");
            }
 
// Display a message.
System.out.print(message);
 
System.exit(0);
}
 
}//end main
Last edited by admin II : 19-May-2008 at 20:57. Reason: Please surround your Java code with [java] your code [/java]
  #4  
Old 16-May-2008, 17:42
TurboPT's Avatar
TurboPT TurboPT is offline
Senior Member
 
Join Date: Feb 2006
Location: Atlanta, GA
Posts: 1,233
TurboPT is a jewel in the roughTurboPT is a jewel in the roughTurboPT is a jewel in the rough

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  
Old 16-May-2008, 18:01
javachallenged javachallenged is offline
New Member
 
Join Date: May 2008
Posts: 6
javachallenged is on a distinguished road

Re: Lost...


So how do I fix it?
  #6  
Old 16-May-2008, 18:03
TurboPT's Avatar
TurboPT TurboPT is offline
Senior Member
 
Join Date: Feb 2006
Location: Atlanta, GA
Posts: 1,233
TurboPT is a jewel in the roughTurboPT is a jewel in the roughTurboPT is a jewel in the rough

Re: Lost...


Give it a value:
JAVA Code:
// boolean isPrime;  << this is only a declaration, left UNinitialized.

boolean isPrime = false;  // initialized.
__________________
Use the force...read the source!!
WYCIWYG -- what you code is what you get!
  #7  
Old 16-May-2008, 18:07
javachallenged javachallenged is offline
New Member
 
Join Date: May 2008
Posts: 6
javachallenged is on a distinguished road

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  
Old 16-May-2008, 18:18
javachallenged javachallenged is offline
New Member
 
Join Date: May 2008
Posts: 6
javachallenged is on a distinguished road

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  
Old 16-May-2008, 20:28
TurboPT's Avatar
TurboPT TurboPT is offline
Senior Member
 
Join Date: Feb 2006
Location: Atlanta, GA
Posts: 1,233
TurboPT is a jewel in the roughTurboPT is a jewel in the roughTurboPT is a jewel in the rough

Re: Lost...


That is not the problem, you have an endless loop:
JAVA Code:
while (number <=100 && number >=1); // << no semi-colon here.
__________________
Use the force...read the source!!
WYCIWYG -- what you code is what you get!
  #10  
Old 17-May-2008, 09:25
TurboPT's Avatar
TurboPT TurboPT is offline
Senior Member
 
Join Date: Feb 2006
Location: Atlanta, GA
Posts: 1,233
TurboPT is a jewel in the roughTurboPT is a jewel in the roughTurboPT is a jewel in the rough

Re: Lost...


Quote:
Originally Posted by TurboPT
That is not the problem, you have an endless loop:
JAVA Code:
while (number <=100 && number >=1); // << no semi-colon here.
More info: ...as long as 'number' is in the loop's range, makes it an endless loop. So if/when you test ran with the loop like that, using 5 as an example, the program will get to that loop and run that statement, alone, continuously.

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 GIDBlogInstall Adobe Flash - Without Administrator Rights by LocalTech

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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 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 C++ Forum 0 07-Nov-2003 15:01

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 18:20.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.