Java中无效的字符常量 [英] Invalid character constant in java

查看:70
本文介绍了Java中无效的字符常量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

return (int) (feetPart) + '\' ' + inchesPart + '\''+'\'';

为什么上面的无效字符常量是常量,所以在JavaScript中效果很好.我想以英尺和英寸为单位显示高度,并且正在使用此客户端,但是当在服务器端使用相同高度时,它会显示无效的字符常量.

Why is the above invalid character constant, this works perfectly in JavaScript. I want to display height in feet and inches and was using this client side, but when I use the same in server side it shows Invalid character constant.

推荐答案

为什么上面的无效字符常量

Why is the above invalid character constant

由于这一部分:

'\' '

试图指定一个 character 文字,它实际上是两个字符(撇号和空格).字符文字必须恰好是一个字符.

That's trying to specify a character literal which is actually two characters (an apostrophe and a space). A character literal must be exactly one character.

如果要指定撇号空间",则应使用字符串文字-在这种情况下,不必对撇号进行转义:

If you want to specify "apostrophe space" you should use a string literal instead - at which point the apostrophe doesn't have to be escaped:

"' "

您的整个陈述会更好:

return (int) (feetPart) + "' " + inchesPart + "''";

或使用"代替英寸,使用'':

Or to use " instead of '' for the inches:

return (int) feetPart + "' " + inchesPart + "\"";

请注意,我什至还不清楚如果 did 进行编译,原始代码将执行您想要的操作,因为我怀疑它会在 feetPart 上执行整数运算还有角色...

Note that it's not even clear to me that the original code would do what you wanted if it did compile, as I suspect it would have performed integer arithmetic on feetPart and the character...

您的代码在Javascript中应该没问题,因为在字符串文字中同时使用了单引号和双引号.

Your code would have been okay in Javascript, because there both single quotes and double quotes are used for string literals.

这篇关于Java中无效的字符常量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆