[英]char can not be dereferenced charAt().compareTo()
public static void main(String[] args)
{
Scanner in= new Scanner(System.in);
String time= in.nextLine();
String arr[]=time.split(":");
// System.out.println(arr[0]);
String PM="P";
if(!(arr[2].charAt(2)).compareTo(PM)) //I get an error here
{
arr[0]+=12;
System.out.print(arr[0]+":"+arr[1]+":"+arr[2]);
}
if((arr[2].charAt(2)).compareTo(PM)!=0) //I get an error here
{
System.out.print(arr[0]+":"+arr[1]+":"+arr[2]);
}
}
Please help me remove errors ! 请帮我消除错误! What i basically have to check is if time input has AM or PM. 我基本上要检查的是时间输入是否为AM或PM。
char
doesn't have a compareTo
method - or any methods, for that matter, since it is a primitive. char
没有compareTo
方法-或任何方法,因为它是基元。
You can compare primitives using relational operators like <
, >
and ==
. 您可以使用诸如<
, >
和==
类的关系运算符来比较基元。
Since PM
is a single-character string, you can also change its type to char: 由于PM
是一个单字符字符串,因此您也可以将其类型更改为char:
char PM = 'P';
and then just use !=
to compare, eg 然后使用!=
进行比较,例如
if (arr[2].charAt(2) != PM) {
// .. whatever.
.charAt()
returns a char
, and the char
behaves like at smaller int
try to use this: .charAt()
返回一个char
,并且char
行为类似于在较小的int
尝试使用此代码:
if(!(arr[2].charAt(2) == PM.charAt(0))) {
...
Instead of: 代替:
if(!(arr[2].charAt(2)).compareTo(PM))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.