Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 07-13-2004, 03:54 PM   #1 (permalink)
george12
Registered User
 
george12's Avatar
 
Join Date: Jul 2004
Posts: 1
george12 is on a distinguished road
Question new to java

Hey, really new to java and i have no idea how to stop the while loop. my output just continuely spits the number and message to the screen.

Any help would be muchly appreciated,

ta


Code:
//imports for keyboard I/O
import java.io.*; 

class PrimeCheck
{
public static void main(String[]args)throws java.io.IOException{
    //create a keyboard input stream

BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));

//Variables for 
int num=-1;
boolean num1;
double num2;
String s1, s2; 

//Prompt user for integer

System.out.println("Enter a positive integer:");
num = Integer.parseInt(stdin.readLine());
{

//start while loop and determine if integer is positive
while(num!=0)
{
if(num>1)
{
    num1=((num % 2-1)==0); 
    if(num1==true)
    {
    
    System.out.println(num + " is a prime number.");
    } //end if

    else
    {
    System.out.println(num + " is not a prime number.");    
    } //end else
    
} //end if
else 
{

System.out.println("A positive integer was not entered, please enter a positive integer:");
}  //end else

}  //end while
}
}  //end main
}  //end class
george12 is offline   Reply With Quote
Old 07-13-2004, 04:45 PM   #2 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,444
sde is on a distinguished road
just move the stdin line inside the while loop. then at the top of each loop, it will stop and ask for a new number:
Code:
//imports for keyboard I/O
import java.io.*; 

class PrimeCheck
{
public static void main(String[]args)throws java.io.IOException{
      //create a keyboard input stream
  
  BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));
  
  //Variables for 
  int num=-1;
  boolean num1;
  double num2;
  String s1, s2; 
  
  //Prompt user for integer
  System.out.println("Enter a positive integer:");

  //start while loop and determine if integer is positive
  while(num!=0)
  {
    num = Integer.parseInt(stdin.readLine());
  
    if(num>1)
    {
      num1=((num % 2-1)==0); 
      if(num1==true)
      {
      
      System.out.println(num + " is a prime number.");
      } //end if
  
      else
      {
      System.out.println(num + " is not a prime number.");    
      } //end else
        
    } //end if
    else 
    {
  
      System.out.println("A positive integer was not entered, please enter a positive integer:");
    }  //end else
  
    }  //end while
  }  //end main
}  //end class
__________________
Mike
sde is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Java Developer Position in Kansas City justplaindoug Java 0 10-29-2004 11:52 AM
JAVA Hobbyists Wanted emteam Java 0 08-29-2004 11:27 AM
IBM urges Sun to make Java open source redhead Code Newbie News 0 03-01-2004 10:39 PM


All times are GMT -8. The time now is 01:39 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting