1 Replies - 729 Views - Last Post: 05 March 2012 - 11:14 AM Rate Topic: -----

#1 vyatka   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 05-March 12

Loop error

Posted 05 March 2012 - 11:05 AM

Hello everyone :)
I'm having an error with my code -- it is a numerology report. When I enter the incorrect date it works but if I enter the correct date it keeps asking me to renter
import java.util.Scanner;

public class plira_Numerology
{	
	public static void main (String args[])
	{
	int month;
	int day;
	int year;
	int date;
	int numerology;
	char  symbol;
	int t;

Scanner input =new Scanner(System.in);
System.out.print("Enter the birth date (mm/dd/yyyy):");
month=input.nextInt();
symbol=input.next().charAt(0);
day=input.nextInt();
symbol=input.next().charAt(0);
year=input.nextInt();
if(month<1||month>12||year<1880||year>2280||symbol!='/'||month==2)
t=44;
else{t=45;}

do{
while(month<1||month>12){
System.out.printf("Bad month:%d\n",month);
System.out.print("Enter birth date (mm/dd/yyyy):");
month=input.nextInt();
symbol=input.next().charAt(0);
day=input.nextInt();
symbol=input.next().charAt(0);
year=input.nextInt();
}
while (symbol!='/'){
System.out.print("Use forward slashes between mm/dd/yyyy!\n");
System.out.print("Enter birth date (mm/dd/yyyy):");
month=input.nextInt();
symbol=input.next().charAt(0);
day=input.nextInt();
symbol=input.next().charAt(0);
year=input.nextInt();
}
while(year<1880||year>2280){
System.out.printf("Bad year for %d\n",year);
System.out.print("Enter birth date (mm/dd/yyyy):");
month=input.nextInt();
symbol=input.next().charAt(0);
day=input.nextInt();
symbol=input.next().charAt(0);
year=input.nextInt();
}
while(month==1||month==3||month==5||month==7||month==8||month==10||month==12){
if(day<1||day>31){
System.out.printf("Bad day for %d/%d : %d\n",month,year,day);
System.out.print("Enter birth date (mm/dd/yyyy):");
month=input.nextInt();
symbol=input.next().charAt(0);
day=input.nextInt();
symbol=input.next().charAt(0);
year=input.nextInt();
}}
while(month==4||month==6||month==9|month==11){
if(day<1||day>30){
System.out.printf("Bad day for %d/%d : %d\n",month,year,day);
System.out.print("Enter birht date (mm/dd/yyyy):");
month=input.nextInt();
symbol=input.next().charAt(0);
day=input.nextInt();
symbol=input.next().charAt(0);
year=input.nextInt();
}}
while(month==2){
if(year%4==0||year%400==0||year==2000){
if(day<1||day>29)
System.out.printf("bad day for %d/%d : %d\n",month,year,day);
System.out.print("Enter birht date (mm/dd/yyyy):");
month=input.nextInt();
symbol=input.next().charAt(0);
day=input.nextInt();
symbol=input.next().charAt(0);
year=input.nextInt();
}
if (year%4!=0||year%100==0||year%400!=0||year!=2000){
if(day<1||day>28)
System.out.printf("bad day for %d/%d/ : %d\n",month,year,day);
System.out.print("Enter birht date (mm/dd/yyyy):");
month=input.nextInt();
symbol=input.next().charAt(0);
day=input.nextInt();
symbol=input.next().charAt(0);
year=input.nextInt();
}}
}while(t==44);

if(t==45){
System.out.printf("Welcome to the numerology report for %d/%d/%d :\n",month,day,year);
date=month+day+year;
	do{
	date=((date%10)+(date/10));
	  }while(date>=10);
numerology=date;
	
	switch(numerology)
		{
		case 1:
	System.out.print(":1: Your luck is about to change so get ready! ");
	break;
		case 2:
	System.out.print(":2: keep trying you are making it. ");
	break;
		case 3:
	System.out.print(":3: its time to relax! ");
	break;
		case 4:
	System.out.print(":4:Money prolems are about to get solved. ");
	break;
		case 5:
	System.out.print(":5: if you stay optimistic your dream will come true.");
	break;

		case 6:
	System.out.print(":6: although things may seem bad don't worry.");
	break;
		case 7:
	System.out.print(":7: you are about to find the love of your life.");
	break;
		case 8:
	System.out.print(":8: too much fun is not very healthy.");
	break;
		case 9:
	System.out.print(":9: something unexpected will happen today.");
	break;
	}}	
	}
}


This post has been edited by pbl: 05 March 2012 - 11:10 AM
Reason for edit:: Code tags added. Please :code:


Is This A Good Question/Topic? 0
  • +

Replies To: Loop error

#2 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8379
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Loop error

Posted 05 March 2012 - 11:14 AM

Digit can be part of a String
Assuming user enters 03/12/1980
month=input.nextInt();    // that reads 03 and leaves /12/1950 in the buffer
symbol=input.next().charAt(0);  // next reads all the String "/12/1950"
                                // char at extract "/" but nothing else to read



better to
String[] token = input.nextLine().split("/");

This post has been edited by pbl: 05 March 2012 - 11:14 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1