gpt4 book ai didi

Java错误不是一个声明

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

当我尝试编译代码时,我收到消息

Main.java:31: error: not a statement
String bed = f.split(" ")

还有

error: ';' expected
String bed = f.split(" ");

我不明白发生了什么事。我需要将文件的一部分分配给字符串。

代码如下:

import java.util.Scanner; 
import java.io.*;

public class Main{

public static void main(String[] args){
if ( args[0] == null){

System.out.println("File not Found");
return;
}

try {
File driver = new File (args[0]);
Scanner j = new Scanner( driver );
int i = 0;
while( j.hasNextLine()) {
String f = j.nextLine();
if (f.isEmpty() || f.startsWith("/")){ //If the String f is empty or has / it will continue reading the nextline after the / or space
continue;
if (f.startsWith("d")) {
String d = f.split(" ");

}
if (f.startsWith("b")) {
String b = f.split(" ");

}

if (f.startsWith("bed"))
String bed = f.split(" ");
}



System.out.println(f);

}

}
catch(FileNotFoundException e) {

System.out.println(e.getMessage());



}

catch (Exception ex) {
System.out.println(ex.getMessage);

}



}

}

最佳答案

.split() 方法返回一个数组,因此您应该将其存储在数组中,而不是字符串中。您的代码应该是 string b[]=f.split("") 而不是将其存储在 string b

关于Java错误不是一个声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30271675/

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