gpt4 book ai didi

java - 我在尝试使用我的方法来检查参数时遇到错误

转载 作者:行者123 更新时间:2023-12-02 04:16:14 25 4
gpt4 key购买 nike

我必须让我的方法来读取字符串是否有空格,如果有,则进入下一步,即查看字符串中是否包含字母或数字 1 到 5。

以下是说明中的说明内容:此方法应检查其参数,看看它在位置 5、11、17、23 等处是否有空格,以及它是否由字母和数字 1 到 5 组成在所有其他职位上。如果是这种情况,则应返回 true,否则返回 false。

这是我收到的错误:

Enc1.java:41: error: no suitable method found for length(int)
if(s.indexOf(s.length(i)) == -1 || i > 5 || i < 1)
^
method CharSequence.length() is not applicable
(actual and formal argument lists differ in length)
method String.length() is not applicable
(actual and formal argument lists differ in length)

这是代码:

import java.util.Scanner;

public class Enc1
{
public static void main(String[] args)
{
Scanner stdin = new Scanner(System.in);
String c1 = args[0];
System.out.println("Please insert a word: ");
c1 = stdin.next();
isCoded(c1);

} // end main

private static final String CODE1 = "HTNZUL5m3lDQchenpIuORws1jPgVtzKWbfBxXSArdayCJkvqGiF2YoM4E" ;

private static final String PLAIN = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz ,.;:" ;

public static String encode(String message)
{
System.out.println("encode called");
return message;

} // end encode

public static String decode(String codedMessage)
{
System.out.println("decode called");
return codedMessage;

} // end decode

public static boolean isCoded(String s)
{
for( int i = 0; i < s.length(); ++i)
{
if( i % 6 == 5 && s.charAt(i) != ' ')
return false;
if(s.indexOf(s.length(i)) == -1 || i > 5 || i < 1)
return false;
}


} // end isCoded

} // end Enc1

最佳答案

错误消息准确地告诉您出了什么问题 - length 方法不带参数 - 因此请采取明显的解决方案并修复它:消除错误的调用。

改变

if(s.indexOf(s.length(i)) == -1 || i > 5 || i < 1)

if(s.indexOf(s.charAt(i)) == -1 || i > 5 || i < 1)

关于java - 我在尝试使用我的方法来检查参数时遇到错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33286238/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com