Java 当外部循环运行一次时?
我正在运行2个while循环。第一个循环将从.txt文件中逐行读取,第二个循环将从该行读取每个字符串。但当我运行循环时,它将读取第一行和字符串,然后停止。我不知道哪里出错了。我遗漏了一些内容,请帮助 更改您的内部Java 当外部循环运行一次时?,java,string,Java,String,我正在运行2个while循环。第一个循环将从.txt文件中逐行读取,第二个循环将从该行读取每个字符串。但当我运行循环时,它将读取第一行和字符串,然后停止。我不知道哪里出错了。我遗漏了一些内容,请帮助 更改您的内部,同时循环到此: import java.io.BufferedReader; import java.io.DataInputStream; import java.io.FileInputStream; import java.io.InputStreamReader; p
,同时循环到此:
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.InputStreamReader;
public class extractvalues {
@SuppressWarnings("null")
public static void main(String [] args)
{
try{
// command line parameter
FileInputStream fstream = new FileInputStream("c:/kdd.txt");
// Get the object of DataInputStream
DataInputStream in = new DataInputStream(fstream);
//to Read values
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
//Read File Line By Line
while ((strLine = br.readLine()) != null)
{
System.out.println(strLine);
String[] splitted=null;
//split the given line into array of words seperated by comma
splitted=strLine.split(",");
int i=0;
//continue loop till it find "." since everyline contain "." in the end
while(!".".equals(splitted))
{
//print each string one by one
System.out.println(splitted[i++]);
}
}
//Close the input stream
in.close();
}
catch (Exception e)
{//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
}
}
//逐行读取文件
而((strLine=br.readLine())!=null){
系统输出打印LN(斯特林);
String[]splitted=null;
//将给定行拆分为以逗号分隔的单词数组
拆分=strLine.split(“,”);
对于(int i=0;i
您引用的是被拆分的整个数组,而不是访问数组中的元素,后者才是您真正想要做的。在我的本地计算机中运行了代码,并在此行获得了异常-ArrayIndexOutOfBounds
//Read File Line By Line
while ((strLine = br.readLine()) != null) {
System.out.println(strLine);
String[] splitted=null;
//split the given line into array of words seperated by comma
splitted=strLine.split(",");
for (int i=0; i < splitted.length; ++i ) {
if (splitted[i].equals("."))
break;
//print each string one by one
System.out.println(splitted[i]);
}
}
问题是splitted是一个字符串#数组
,您不能使用equals()
方法比较这样的数组
System.out.println(splitted[i++]);
因此,请尝试forEach
循环,并将数组中的每个字符串与
进行比较
while(!".".equals(splitted)) ---> This is logically incorrect way !!
for(字符串温度:拆分){
如果(temp.equals(“.”)将您的内部while循环更改为该循环,并确保不需要
for(String temp:splitted){
if(temp.equals(".")) <-- Using equals for comparing 2 String
break;
System.out.println(temp);
}
由于字符串数组的最后一个索引不仅包含(.)字符,还包含额外字符。
if (splitted[i].equals("."))
while((strLine=br.readLine())!=null){
系统输出打印LN(斯特林);
String[]splitted=null;
//将给定行拆分为以逗号分隔的单词数组
拆分=strLine.split(“,”);
//继续循环,直到最后找到“.”,因为everyline包含“.”
for(int i=0;iIt工作得很好,非常感谢。我的代码出了什么问题?我将标记,在允许我标记之前还需要3分钟。他应该迭代数组的长度,以避免像您刚才发现的那样的问题。实际上,如果每行以“,”结尾,他的输入格式相当奇怪,但问题是这样的。如果是这样,那么我们可以在拆分的字符串length-1
上迭代几次,甚至不检查句点。我的数据集是KDDcup99
while ((strLine = br.readLine()) != null){
System.out.println(strLine);
String[] splitted=null;
//split the given line into array of words seperated by comma
splitted=strLine.split(",");
//continue loop till it find "." since everyline contain "." in the end
for(int i=0;i<splitted.length;i++)
{
System.out.println(splitted[i++]);
}
}