简体   繁体   中英

How do you compare values (greater than/less than) of strings that have been turned into integers?

So, I have to make a code that takes in 2 dates (month/day/year) from the user and returns "true" if the FIRST date is less than the second date. In any other case the date will be "false" or "they are the same". I was instructed that I could NOT demand the user to do a specified format (ie mm/dd/yyyy) and that instead I should focus on the "/"s.

The problem is, it always returns the "they are the same" answer no matter what I put in. Any tips and advice are greatly appreciated! I'm just a beginner programmer, so I'm not sure what's wrong.

Here's The code:

    import java.util.Scanner;

public class Mari_WS15IsDateEarlier {

public static void main (String[] args) {

//initial variables, asks for variable input and determines month/day/year

  Scanner dateInput = new Scanner(System.in);
  String answr;

//splits string input into month, day and year
System.out.println("Enter a month, day, and year(separate with a slash) :");
  String date1 = dateInput.next();
  String[] splitStrings = date1.split("/"); 
     String month1 = splitStrings[0];
     String day1 = splitStrings[1];
     String year1 = splitStrings[2];

System.out.println("Enter another month, day, and year (separate with a slash) :");
  String date2 = dateInput.next();
  String[] splitStrings2 = date1.split("/");
     String month2 = splitStrings[0];
     String day2 = splitStrings[1];
     String year2 = splitStrings[2];

//turns string into integer for testing (greater than/less than)
int mn1 = Integer.parseInt(month1);
int mn2 = Integer.parseInt(month2);
int dy1 = Integer.parseInt(day1);
int dy2 = Integer.parseInt(day2);
int yr1 = Integer.parseInt(year1);
int yr2 = Integer.parseInt(year2);

//Determine if the set of variables is a geometric sequence
if (yr1 < yr2) {
  answr = "true";
}
else if ((yr1 == yr2)&&(mn1 < mn2)) {
  answr = "true";
}
else if ((mn1 == mn2)&&(dy1 < dy2)) {
  answr = "true";
}
else if (dy1 == dy2) {
  answr = "ERROR: Dates are identical.";
}
else {
  answr = "false";
}

//Prints out the answer
System.out.println(answr);

Great Question and fantastic code! The solution is simple enough, from the looks of your code you simply copied date1 and pasted for date2 . However you didn't change all of the variables so the code was comparing date1 to date1 hence your error. Make sure to change both date1 to date2 and splitStrings to splitStrings2 .

Also just a little suggestion specific to your code, I would look again at the identical dates if statement as you are only comparing days. Try date1=1/2/2 and date2=2/2/2 and you will see the issue!

Watch-out for the 2nd date, you need to change date1.split("/"); with date2.split("/")

Replace your 2nd date top two lines with following:

String date2 = dateInput.next();
String[] splitStrings2 = date2.split("/");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM