我正在尝试使用for循环来检查单维字符串数组中的每个字符,并打印“Character [a]为大写”或“Character [b]为小写”或“Character [c]为空格”。 “我还必须打印索引号和该索引中的字符。
我以为我应该用大写字母和小写字母创建单独的字符串,但事实证明我错了。我也不知道我是否应该将字符串数组转换为char数组?
我想我应该使用charAt()
方法,但我不知道如何。
这就是我所做的:
for(x = 0; x < fullName.length(); x++)
{
if(fullName[0] == nameUpperCase[2]);
{
System.out.print("character [0] located at position [0] is lowercase");
}
if(fullName[1] == nameLowerCase[4]);
{
System.out.print("character [b] located at position [1] is uppercase");
}
}
正如你所看到的,我甚至无法弄清楚如何使用循环打印'a'或'b',所以我不得不手动插入每个循环以及它们所处的位置...... / p>
答案 0 :(得分:1)
fullName[0] == nameUpperCase[2]
上面的表达是错误的。我假设你使用 full.length()方法时,你的for循环中的fullName是 String 。 fullName不是数组,因此执行fullName [0]是错误的。要获取字符串中的字符,请使用String.charAt(index)method。如果您是初学者,请查看String Class API以供参考。
你可以做这样的事情我想: String[] s= {"Ab "};
for(String ss: s) {
for(int i=0; i<ss.length(); i++) {
if(Character.isUpperCase(ss.charAt(i))) {
System.out.println("character "+ss.charAt(i)+" located at position"+i+ " is lowercase");
}
else if(Character.isLowerCase(ss.charAt(i))) {
System.out.println("character "+ss.charAt(i)+" located at position"+i+ " is lowercase");
}
else if(else if(Character.isSpaceChar(ss.charAt(i)))) {
System.out.println("character "+ss.charAt(i)+" located at position"+i+ " is lowercase");
}
}
答案 1 :(得分:1)
可能有一种更简单的方法,但这可行。
if (str.toUppercase().equals(str)) {
// It's uppercase.
}
if (str.toLowerCase().equals(str)) {
// It's lowercase.
}
答案 2 :(得分:1)
如果您正在比较两个字符串(即数组是String[]
),那么您的代码将比较每个字符串的内存引用,这将是不同的。
为了比较String
的相等性,您需要使用equals
方法。
for(x = 0; x < fullName.length(); x++)
{
if(fullName[0].equals(nameUpperCase[2]));
{
System.out.print("character [0] located at position [0] is lowercase");
}
if(fullName[1].equals(nameLowerCase[4]));
{
System.out.print("character [b] located at position [1] is uppercase");
}
}
我还建议你从其他答案中提取一些想法。
将原始String
转换为char数组,而不是......
char[] fullName = name.toCharArray();
或者您可以简单地获取给定索引处的字符...
for(x = 0; x < originalString.length(); x++)
{
char charAt = originalString.charAt(x);
if(Character.isUpperCase(charAt));
{
System.out.print("character [0] located at position [0] is lowercase");
}
if(Character.toUpperCase(originalString.charAt(1)) == originalString.charAt(4));
{
System.out.print("character [b] located at position [1] is uppercase");
}
}
更新了示例
现在我们已经确定我们无法将String
值与==
进行比较,这是我认为您想要实现的一个实际示例......
String value = "This is a simple string";
for (int index = 0; index < value.length(); index++) {
String whatIs = "Unknown";
if (Character.isUpperCase(value.charAt(index))) {
whatIs = "Upper case";
} else if (Character.isLowerCase(value.charAt(index))) {
whatIs = "Lower case";
} else if (Character.isSpaceChar(value.charAt(index))) {
whatIs = "Space";
} else if (Character.isDigit(value.charAt(index))) {
whatIs = "Digit";
}
System.out.println("Character @ " + index + " (" + value.charAt(index) + ") is a " + whatIs + " character");
}
哪个输出
Character @ 0 (T) is a Upper case character
Character @ 1 (h) is a Lower case character
Character @ 2 (i) is a Lower case character
Character @ 3 (s) is a Lower case character
Character @ 4 ( ) is a Space character
Character @ 5 (i) is a Lower case character
Character @ 6 (s) is a Lower case character
Character @ 7 ( ) is a Space character
Character @ 8 (a) is a Lower case character
Character @ 9 ( ) is a Space character
Character @ 10 (s) is a Lower case character
Character @ 11 (i) is a Lower case character
Character @ 12 (m) is a Lower case character
Character @ 13 (p) is a Lower case character
Character @ 14 (l) is a Lower case character
Character @ 15 (e) is a Lower case character
Character @ 16 ( ) is a Space character
Character @ 17 (s) is a Lower case character
Character @ 18 (t) is a Lower case character
Character @ 19 (r) is a Lower case character
Character @ 20 (i) is a Lower case character
Character @ 21 (n) is a Lower case character
Character @ 22 (g) is a Lower case character
答案 3 :(得分:1)
如果您使用的是String
,则可以使用charAt
来选择字符。例如,使用fullName[0]
不可能选择单个字符。如果fullName
是字符数组,则表示 。
String fullName = "Stack Overflow";
for (int x = 0; x < fullName.length(); x++)
{
char testChar = fullName.charAt(x);
if (Character.isLowerCase(testChar))
{
System.out.println("character [" + testChar + "] located at position [" + x + "] is lowercase");
}
else if (Character.isUpperCase(testChar))
{
System.out.println("character [" + testChar + "] located at position [" + x + "] is uppercase");
}
else if (Character.isSpaceChar(testChar)) {
System.out.println("character [" + testChar + "] located at position [" + x + "] is a space");
}
}
答案 4 :(得分:0)
您可能想尝试,
for(x = 0; x < fullName.length(); x++)
{
if( fullName[x].equals(nameUpperCase[x]) );
{
System.out.print("character [" + fullName[x] + "] located at position [" + x + "] is lowercase.");
}
if( fullName[x].equals(nameLowerCase[x]) );
{
System.out.print("character [" + fullName[x] + "] located at position [" + x + "] is uppercase.");
}
}
如果 fullName,nameUpperCase和nameLowerCase 是字符串数组,它应该可以正常工作。虽然,看看其他人发布的解决方案,我建议你看看那些更清晰,更清晰的代码。
答案 5 :(得分:0)
你可能想要这样的东西:
String s = "Hello World";
for (int i = 0 ; i < s.length() ; i++) {
char c = s.charAt(i);
if (c == ' ')
System.out.println("character located at position [" + i + "] is a space");
else if (Character.isUpperCase(c))
System.out.println("character [" + c + "] located at position [" + i + "] is uppercase");
else if (Character.isLowerCase(c))
System.out.println("character [" + c + "] located at position [" + i + "] is lowercase");
// else character is not recognized
}
当然,我在这里只使用了一个String
,但在String[]
的情况下,您只需将此流程应用于每个成员。