Jump to content

  •  

  • iBotModz CBox


    Photo

    Java help


    • Please log in to reply
    2 replies to this topic

    #1 ColinMc

    ColinMc

      Apprentice Grade 2

    • Members+

    • 18 posts
    • Joined: 26-April 09

    Posted 04 November 2009 - 02:42 PM

    I'm in computer science 12 and I'm doing really well but I have a problem on one of my programs is that it's suppose to output the users number saying if it's prime or not prime and I can get prime to work but to display not prime I can't. I had it working before but it was bad coding so I'm fixing it up. My problem is there is a infinate loop and I can't figure it out if anyone can help that would be great.


    //Colin McQueen
    //Prime J7
    //November 01 09
    import java.io.*;//tells Java to use input
    class Prime
    {//begin class
    //Program tells the user if the user's number is a prime number or not
    public static void main(String [] args) throws IOException
    {//begins main method
    InputStreamReader inStream = new InputStreamReader(System.in);
    BufferedReader buffread = new BufferedReader (inStream);
    String inData;
    int input, remain,denominator;//declares integer variables
    boolean prime = true; //declares a boolean variable
    System.out.println("Enter a number:");
    inData = buffread.readLine();
    input = Integer.parseInt(inData);
    denominator = input - 1; //denominator is the users number subtract 1
    remain = 0;
    
    while (prime = true && denominator > 1)
    {//begins loop
    
    remain = (input % denominator); //remain is input mod denominator
    
    if (remain != 0)//remain does not equal 0
    {
    denominator = denominator - 1; //denominator is denominator - 1
    }
    
    else //when remain equals 0
    {
    prime = false; //denominator equals 0
    
    }
    
    }//ends loop
    
    
    if (prime = true)//prime equals true and remain doesn't equal 0 or input equals 2
    {
    System.out.println("Prime");//outputs the number as a Prime
    }
    else //remain equals 0 or denominator equals 1
    {
    System.out.println("Not Prime");//outputs the number not a Prime
    }
    
    }//end main method
    }//end class

    also some of the comments I have to fix because I didn't bother with them till I get the program 100% working.

    Edited by ColinMc, 04 November 2009 - 02:43 PM.


    #2 iBotPeaches

    iBotPeaches

      General Grade 6

    • Owner

    • 6,570 posts
    • Joined: 29-July 07
    • Gender:Male
    • Location:Kansas

    Posted 04 November 2009 - 03:07 PM

    So you keep breaking the number down, to see if it will equal 0 or not? To check if prime?

    I think a long time ago when I learned VB, I needed a prime number check for my stat application, to make sure I could divide it or not.

    I re-install java today, and then check.

    #3 ColinMc

    ColinMc

      Apprentice Grade 2

    • Members+

    • 18 posts
    • Joined: 26-April 09

    Posted 04 November 2009 - 03:13 PM

    So you keep breaking the number down, to see if it will equal 0 or not? To check if prime?

    I think a long time ago when I learned VB, I needed a prime number check for my stat application, to make sure I could divide it or not.

    I re-install java today, and then check.


    never mind I got it my mistake was with the if statements for prime had to be prime == true, while I had prime = true. >: ( I'm mad because that was such a small error I couldn't find till today lol.

    Edited by ColinMc, 05 November 2009 - 02:33 PM.