codetoad.com
  ASP Shopping CartForum & BBS
  - all for $20 from CodeToad Plus!
  
  Home || ASP | ASP.Net | C++/C# | DHTML | HTML | Java | Javascript | Perl | VB | XML || CodeToad Plus! || Forums || RAM 
Search Site:
Search Forums:
  Sentinel value to terminate loop  gosi at 17:06 on Thursday, October 27, 2005
 

Hi, hope there is someone that can help me. I have been trying to make a loop so every time you press 0 you quit the program, I've been trying all kind of while loops where (value !=0) should terminate the loop but it doesn't seemes to work, so I would really appreciate some help.





public static void main(String[] args) {

final int CALENDAR_START_DATE = 1582;
String another = "y";

Scanner scan = new Scanner(System.in);



while (another.equalsIgnoreCase("y"))

{


System.out.print("Enter the year to be tested(0 to quit): ");
int year = Integer.parseInt(scan.nextLine());


String isLeapYear = new String(year + " is a leap year.");
String isNotLeapYear = new String(year + " is not a leap year.");
String errorMessage = new String("Invalid input, has to be greater or equal to 1582");



if (year%4==0 && year%100!=0 && year%400!=0 && year>=CALENDAR_START_DATE||
year%4==0 && year%100==0 && year%400==0 && year>=CALENDAR_START_DATE)

System.out.println(isLeapYear);


else if (year%4!=0 && year%100!=0 && year%400!=0 && year>=CALENDAR_START_DATE ||
year%4==0 && year%100==0 && year%400!=0 && year>=CALENDAR_START_DATE)

System.out.println(isNotLeapYear);

else
{
System.out.println(errorMessage);
System.out.println();

}
System.out.print("Test another year (y/n)? ");
another = scan.nextLine();

}
}

}

  Re: Sentinel value to terminate loop  crwood at 19:43 on Thursday, October 27, 2005
 


import java.util.Scanner;

public class QuitTest {
public static void main(String[] args) {
final int CALENDAR_START_DATE = 1582;
boolean another = true;

Scanner scan = new Scanner(System.in);

while (another)
{
System.out.print("Enter the year to be tested(0 to quit): ");
String entry = scan.nextLine();
if(entry.equals("0"))
break;
int year = Integer.parseInt(entry);

String isLeapYear = year + " is a leap year.";
String isNotLeapYear = year + " is not a leap year.";
String errorMessage = "Invalid input, has to be greater or equal to 1582";

if (year%4==0 && year%100!=0 && year%400!=0 && year>=CALENDAR_START_DATE ||
year%4==0 && year%100==0 && year%400==0 && year>=CALENDAR_START_DATE)
System.out.println(isLeapYear);
else if (year%4!=0 && year%100!=0 && year%400!=0 &&
year>=CALENDAR_START_DATE || year%4==0 && year%100==0 &&
year%400!=0 && year>=CALENDAR_START_DATE)
System.out.println(isNotLeapYear);
else
{
System.out.println(errorMessage);
System.out.println();
}
System.out.print("Test another year (y/n)? ");
entry = scan.nextLine();
if(entry.equalsIgnoreCase("n"))
another = false;
}
}
}


  Re: Sentinel value to terminate loop  gosi at 14:34 on Friday, October 28, 2005
 

It¨s great, thanks for the held :D








CodeToad Experts

Can't find the answer?
Our Site experts are answering questions for free in the CodeToad forums








Recent Forum Threads
•  Asics Onitsuka
•  burberry outlet
•  Re: Need Javascript Multiple sidebar menu with sliding effects
•  Re: Submit button, email setup
•  Re: Write text strings to Serial Port
•  Re: TO SEND Text message or URL through Clickatell
•  sending of file over network
•  project on networking
•  Re: Walk With Walter for UNICEF Sponsorship


Recent Articles
ASP GetTempName
Decode and Encode UTF-8
ASP GetFile
ASP FolderExists
ASP FileExists
ASP OpenTextFile
ASP FilesystemObject
ASP CreateFolder
ASP CreateTextFile
Javascript Get Selected Text


© Copyright codetoad.com 2001-2011