compareto

Comparing dates in Java

不打扰是莪最后的温柔 提交于 2019-12-10 12:15:17
问题 I already read date functions but i cant think a best way to solve my problem. I have a couple of dates from database which is String and i want to compare it to may current date. I am using the compareTo , but there is a problem using this function i guess it is because of i was comparing strings. This is my function: public int dateCompare(String today, String date2){ return today.compareTo(date2); } And when i use it in sample dates: dateCompare("04/19/2013","04/18/2013"); it returns 1,

How does compareTo work?

不羁的心 提交于 2019-12-10 02:25:17
问题 I know that compareTo returns a negative or positive result on how well one string correlates to the other, but then why: public class Test { public static void main(String[] args) { String y = "ab2"; if(y.compareTo("ac3") == -1) { System.out.println("Test"); } } } is true and public class Test { public static void main(String[] args) { String y = "ab2"; if(y.compareTo("ab3") == -1) { System.out.println("Test"); } } } is also true? 回答1: The general contract of Comparable.compareTo(o) is to

Java - compareTo and operators

拥有回忆 提交于 2019-12-08 16:51:38
问题 If I have a class Person that implements Comparable (compares personA.height to personB.height , for example), is it possible to use personA < personB as a substitute for personA.compareTo(personB) == -1? Are there any issues in doing this or do I need to overload operators? 回答1: There is no operator overloading in Java. You probably come from a C++ background. You have to use the compareTo method. 回答2: No, it isn't possible to use personA < personB as a substitute. And you can't overload

In Java what should I use for a PriorityQueue that returns the greatest element first?

爷,独闯天下 提交于 2019-12-08 03:02:13
问题 Java's PriorityQueue places the least element at the head of the list, however I need it to place the greatest element at the head. What what's the neatest way to get a priority queue that behaves like that. Since I wrote the class stored in this queue I could simply reverse the results of compareTo , its not used outside this queue. However I like to make the code an accurate representation of what I'm modeling, what I'm trying to do is get the greatest first so the code should say that

How can I calculate age in Java accurately given Date of birth

陌路散爱 提交于 2019-12-07 16:00:38
问题 I am trying to calculate age in java that accounts for months, so just subtracting years will not work. I also want to tell the user if today is their birthday. Here is the code I have so far, but I am afraid it is a bit off. It also will not tell if today is the birthday even though the two dates it's comparing are equal. The way I tried to originally calculate was using milliseconds. The reason you see 2 ways of getting the current date is because I was trying something to get it working,

Java: Three strings, lexicographic order

不羁岁月 提交于 2019-12-06 13:53:33
beginner Java programmer here. I am trying to compare three strings to each other, and have the system spit out the second/middle word in lexicographic order. import java.util.*; public class Ordered2 { public static void main(String[] args) { String firstString, secondString, thirdString; Scanner keyboard = new Scanner(System.in); System.out.println("Enter three different strings."); System.out.println("The string in the middle order lexicographically will be displayed."); firstString = keyboard.nextLine(); secondString = keyboard.nextLine(); thirdString = keyboard.nextLine(); String

Using the compareTo method with an array to sort students by name and test score

∥☆過路亽.° 提交于 2019-12-06 12:15:03
问题 I need to use the comparable interfact and a compareTo method to sort a list of students alphabetically and then by test score. I'm struggling at getting this to work in my application. The list of names needs to be read from a text file. I don't know how many names will be in the text file that my professor uses except that it will be less than 100. I am also supposed to display the average of the grades at the bottom as well as write a message next to any student that is 15 points below

In Java what should I use for a PriorityQueue that returns the greatest element first?

此生再无相见时 提交于 2019-12-06 07:29:41
Java's PriorityQueue places the least element at the head of the list, however I need it to place the greatest element at the head. What what's the neatest way to get a priority queue that behaves like that. Since I wrote the class stored in this queue I could simply reverse the results of compareTo , its not used outside this queue. However I like to make the code an accurate representation of what I'm modeling, what I'm trying to do is get the greatest first so the code should say that rather than least first with an unusual definition of least. [edit]just a quick thank you everyone,

How can I calculate age in Java accurately given Date of birth

扶醉桌前 提交于 2019-12-06 03:46:19
I am trying to calculate age in java that accounts for months, so just subtracting years will not work. I also want to tell the user if today is their birthday. Here is the code I have so far, but I am afraid it is a bit off. It also will not tell if today is the birthday even though the two dates it's comparing are equal. The way I tried to originally calculate was using milliseconds. The reason you see 2 ways of getting the current date is because I was trying something to get it working, but wanted to show everyone my work so that they could point me in the right direction. EDIT FOR

Java compareTo for String and Integer arguments

∥☆過路亽.° 提交于 2019-12-05 23:56:20
I am implementing the bubble sort algorithm and I want it to be able to accept both Integer and String parameters. I cast all input as Strings and use the compareTo method to compare the integers casted as strings to the strings. I am getting an incorrect answer when using compareTo to compare the casted integers. What am I doing wrong? Integer.compareTo sorts numbers numerically. This is what you want. String.compareTo sorts strings lexicographically; that is, in alphabetical order. I remember in Windows 3.1 that the folder of photos from my digital camera was ordered like this: PHOTO1,