Reputation: 341
I am having some difficulty understanding the way to change a user input of dates an an int to be compared to another int. Am trying to get the user to input his date of birth, then compare it to zodiac signs dates in a switch format if it is possible. Going through most of the post on how to change an input to int, with pars, and SimpleDateFormat I was not able to apply it, as shown in my code when I try to implement "dateOB" that should've been formated to an int, in the switch statement it did not recognize it as so ...
My code so far:
import java.util.*;
import java.text.*;
public class signs {
public static void main(String[] args) {
Scanner userInput = new Scanner (System.in);
// Intro message
System.out.println("Hello you, Lets get to know each other");
// User input begin
//name
String userName;
System.out.println("What is your name ?");
userName = userInput.nextLine();
//date of birth
System.out.println(userName + " please enter you DoB (DD/MM/YYY)");
String dateOB = userInput.next();
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/mm/yyyy");
try {
Date date = new SimpleDateFormat("dd/MM/yyyy").parse(dateOB);
System.out.println(dateOB);
} catch (ParseException e) {
e.printStackTrace();
return;
}
System.out.println("So your date of birth is " + dateOB);
// choosing zodiac sign
//starting the switch statement
int convertedDate = dateOB;
String zodiacSign;
switch (convertedDate){
}
}
}
I would appreciate it if someone could explain to me how to implement this in a simple way ...
So i get really great suggestion by you guys, and i ended up with the right understanding of things, just complication implementing minor suggestion to make the code function the right way,
what i got so far is :
boolean correctFormat = false;
do{
System.out.println(userName + " please enter you DoB (DD/MM/YYY)");
String dateOB = userInput.next();
try{
Date date = new SimpleDateFormat("dd/MM/yyyy").parse(dateOB);
System.out.println(dateOB);
System.out.println("So your date of birth is " + dateOB);
correctFormat = true;
}catch(ParseException e){
correctFormat = false;
}
}while(!correctFormat);
So the problem i am facing is that " dateOB is now since it is inside a while loop, is not recognized out side the loop, which is checking if the date format is right , so when i try and conver the date to a number : int dayNmonth = Integer.parseInt(new SimpleDateFormat("ddMM").format(dateOB));
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int day = cal.get(Calendar.DAY_OF_MONTH);
//int month = cal.get(Calendar.MONTH)+ 1;
it is not accepted ?
how do i tackle this issue ?
Hey so far i've been having some trouble with part of the code:
Calendar cal = Calendar.getInstance();
cal.setTime(dayNmonth);
int day = cal.get(Calendar.DAY_OF_MONTH);
int month = cal.get(Calendar.MONTH)+ 1;
as in eclips it wont let me get the day and the month from "dateOB" with this code.
could someone try and help me understand what is the problem !?
Upvotes: 0
Views: 5870
Reputation: 339043
You are using terribly-flawed date-time classes that are now legacy. Use only the modern java.time classes. Never use Calendar
, Date
, etc.
For a date-only value, without time-of-day, and without time zone, use LocalDate
class.
compare it to zodiac signs dates in a switch format
No can do with switch
. But you can use a series of if
tests.
Notice in this code how we declare our birthDate
and astrology
variables before the repeat
loop. Then we assign object references to those variables within the loop. The loop can test for null
to detect whether the user has accomplished their task or not.
final DateTimeFormatter formatter = DateTimeFormatter.ofPattern ( "dd/MM/uuuu" ) ;
LocalDate birthDate = null ;
String astrology = null ;
repeat
{
… // Gather input from user.
try
{
birthDate = LocalDate.parse ( input , formatter ) ;
final int dayOfYear = date.getDayOfYear ( );
if ( dayOfYear < 100 ) { astrology = "Aquarius"; }
else if ( dayOfYear < 200 ) { astrology = "Taurus"; }
else { astrology = "Whatever"; }
}
catch ( DateTimeParseException e )
{
… // Explain error to user. Prompt for input again.
}
}
while ( Objects.isNull ( birthDate ) ) ;
Upvotes: 0
Reputation: 341
Well, finally i was able to get over the problem, with full understanding of new concepts thanks to @LordAnomander for his patience and detailed instructions, @cdaiga insightful alternative solutions and @Dato Mumladze cooperation.
the correct code i reached so far without the zodiac comparison which i will have to apply a switch method, and i am sure since i am still learning it will face some other issue but all good more to learn,
import java.util.*;
import java.text.*;
import java.lang.*;
public class signs {
public static void main(String[] args) throws ParseException {
Scanner userInput = new Scanner (System.in);
// Intro message
System.out.println("Hello you, Lets get to know each other");
// User input begin
//name
String userName;
System.out.println("What is your name ?");
userName = userInput.nextLine();
//date of birth
Date date = new Date();
String dateOB = "";
boolean correctFormat = false;
do{
System.out.println(" please enter you DoB (dd/MM/yyyy)");
dateOB = userInput.next();
try{
date = new SimpleDateFormat("dd/MM/yyyy").parse(dateOB);
System.out.println("day and month " + dateOB);
correctFormat = true;
}catch(ParseException e){
correctFormat = false;
}
}while(!correctFormat);
//Choosing sign
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int day = cal.get(Calendar.DAY_OF_MONTH);
int month = cal.get(Calendar.MONTH)+ 1;
// announcing sign
//converting the day and month to a number to compare
int zodiacNum = day * 100 + month;
System.out.println(" zodiac number is " + zodiacNum);
//closing userInput
userInput.close();
}
}
Thank you again guys, i hope this could be helpful for someone with basic knowledge as mine, and help them understand some issues.
Upvotes: 1
Reputation: 1123
Once you have converted the input to a Date
you can also get the information by using Calendar
.
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int day = cal.get(Calendar.DAY_OF_MONTH);
int month = cal.get(Calendar.MONTH) + 1;
Edit #1:
To get the number you want you have to do an additional calculation, i.e.,
int zodiacNumber = month * 100 + day;
It will result in 1105 for the input 05/11/1984. Vice versa you can use day * 100 + month
if you prefer 511.
Or you can parse the date straight to the format you wish by using
int zodiac = Integer.valueOf(new SimpleDateFormat("ddMM").format(date))
which is an even shorter solution.
Edit #2: As you replied to one question with the comment you want a loop to make sure the date is entered in the correct format, do something like this:
boolean correctFormat = false;
do {
// ask the user for input
// read the input
try {
// try to format the input
correctFormat = true;
} catch (ParseException e) {
correctFormat = false;
}
} while (!correctFormat);
You can be sure the date was not entered in a correct way if the exception is thrown and thus, by setting the helper variable you can make sure to ask for a date until it is convertable and hence, correct.
Edit #3: You need to use a Date
if you are working with SimpleDateFormat
not a String
.
int dayNmonth = Integer.parseInt(new SimpleDateFormat("ddMM").format(dateOB));
will not work! You have to use
int dayNmonth = Integer.parseInt(new SimpleDateFormat("ddMM").format(date));
So your program has to look like this:
Date date = new Date();
String dateOb = "";
boolean correctFormat = false;
do {
(...)
date = new SimpleDateFormat("dd/MM/yyyy").parse(dateOB);
(...)
} while (...);
int dayNmonth = Integer.parseInt(new SimpleDateFormat("ddMM").format(date);
(...)
cal.setTime(date);
(...)
It is important for you to know that date
is the real date whereas dateOB
is only what the user entered - this String
cannot be used as real date!
Upvotes: 0
Reputation: 4939
I think you could get the day and the month of the user's input in "MMdd" format. Then use Integer.parseInt() to get a number, then use if statements to locate the zodiac sign since it is dependent on the day and the month.
For example Aries. Mar 21 - Apr 19. So you can get the input number from the user's date like:
int userVal=0;
if (userDate != null) {
DateFormat df = new SimpleDateFormat("MMdd");
userVal = Integer.parseInt(df.format(date));
}
Now you can return aries by doing:
if(userVal >= 321 && userVal <= 419) //Mar 21 == 0321 and April 19 == 0419
System.out.println("Your zodiac sign: Aries");
Just continue for the other signs.
Implementation
public static void main(String[] args) throws ParseException {
Date date;
int mmdd;
DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
DateFormat mmDDFormat = new SimpleDateFormat("MMdd");
Scanner scanner = new Scanner(System.in);
System.out.println("Please enter your date of birth(dd/MM/yyyy):");
String stringDate = scanner.next();
date = dateFormat.parse(stringDate);
System.out.println("This is your date of birth: " + dateFormat.format(date));
mmdd = Integer.parseInt(mmDDFormat.format(date));
System.out.println("This is the mmdd value: " + mmdd);
//Now getting the Zodiac sign
System.out.println("The zodiac sign of your date of birth is: ");
if (mmdd >= 321 && mmdd <= 419) {
System.out.println("ARIES");
} else if (mmdd >= 420 && mmdd <= 520) {
System.out.println("TAURUS");
} else if (mmdd >= 521 && mmdd <= 620) {
System.out.println("GEMINI");
} else if (mmdd >= 621 && mmdd <= 722) {
System.out.println("CANCER");
} else if (mmdd >= 723 && mmdd <= 822) {
System.out.println("LEO");
} else if (mmdd >= 823 && mmdd <= 922) {
System.out.println("VIRGO");
} else if (mmdd >= 923 && mmdd <= 1022) {
System.out.println("LIBRA");
} else if (mmdd >= 1023 && mmdd <= 1121) {
System.out.println("SCORPIO");
} else if (mmdd >= 1122 && mmdd <= 1221) {
System.out.println("SAGITTARIUS");
} else if ((mmdd >= 1222 && mmdd <= 1231) || (mmdd >= 11 && mmdd <= 119)) {
System.out.println("CAPRICORN");
} else if (mmdd >= 120 && mmdd <= 218) {
System.out.println("AQUARIUS");
} else if (mmdd >= 219 && mmdd <= 320) {
System.out.println("PISCES");
}
}
Nb: I used Netbeans 8.0.1
Upvotes: 1
Reputation: 928
You can do it like this
public static void main(String[] args) throws ParseException {
Scanner userInput = new Scanner(System.in);
System.out.println("Hello you, Lets get to know each other");
String userName;
System.out.println("What is your name ?");
userName = userInput.nextLine();
System.out.println(userName + " please enter you DoB (DD/MM/YYY)");
String dateOB = userInput.next();
Date date = new SimpleDateFormat("dd/MM/yyyy").parse(dateOB);
System.out.println(dateOB);
System.out.println("So your date of birth is " + dateOB);
Calendar c = Calendar.getInstance();
c.setTime(date);
int day = c.get(Calendar.DAY_OF_YEAR);
// IF day is from January 20 - to February 18 this means he or she is aquarius
if (day >= 20 && day <= 49) {
System.out.println("Aquarius");
} else if (day >= 50 && day <= 79) {
//If day if from February 19 to March 20 then pisces
System.out.println("Pisces");
}
//write all zodiac signs ...
userInput.close();
}
Upvotes: 1
Reputation: 331
I suggest u to use a simple Bean ZodiacSign like that:
class ZodiacSign {
private String name;
private int startMonth;
private int startDay;
private int endMonth;
private int ednDay;
public ZodiacSign(String name, int startMonth, int startDay, int endMonth, int ednDay) {
super();
this.name = name;
this.startMonth = startMonth;
this.startDay = startDay;
this.endMonth = endMonth;
this.ednDay = ednDay;
}
// getter & setter
}
and iterate over a collection until you find a match, like this:
List<ZodiacSign> zodiac = Collections.emptyList();
zodiac.add(new ZodiacSign("AQUARIUS", Calendar.JANUARY, 20, Calendar.FEBRUARY, 18));
zodiac.add(new ZodiacSign("PISCES", Calendar.FEBRUARY, 19, Calendar.MARCH, 20));
// ..
zodiac.add(new ZodiacSign("CAPRICORN", Calendar.DECEMBER, 22, Calendar.JANUARY, 19));
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
int month = calendar.get(Calendar.MONTH);
for (ZodiacSign sign : zodiac) {
if (month >= sign.getStartMonth() && month <= sign.getEndMonth()) {
if (dayOfMonth >= sign.getStartDay() && dayOfMonth <= sign.getEdnDay()) {
System.out.println("Zodiac Sign: " + sign.getName());
}
}
}
Upvotes: 1