Java-新行循环
我有一个带有类型和数字的数组,打印时结果如下:Java-新行循环,java,arraylist,java.util.scanner,Java,Arraylist,Java.util.scanner,我有一个带有类型和数字的数组,打印时结果如下: car:1 car:2 car:3 boat:1 boat:2 boat:3 plane:1 plane:2 plane:3 我正在寻找一种方法来确定类型何时更改,然后再换行。这意味着在car:3(和boat:3)之后打印一个“\n”,以便所有车辆都位于自己的行上 我正在使用for循环打印所有这些项目,如下所示: for(Types filename: TypeTable) { Scanner s;
car:1 car:2 car:3 boat:1 boat:2 boat:3 plane:1 plane:2 plane:3
我正在寻找一种方法来确定类型何时更改,然后再换行。这意味着在car:3(和boat:3)之后打印一个“\n”,以便所有车辆都位于自己的行上
我正在使用for循环打印所有这些项目,如下所示:
for(Types filename: TypeTable)
{
Scanner s;
s = new Scanner(filename.toString());
while (s.hasNext())
{
System.out.print(s.nextLine()+ " ");
}
我想我需要一些局部循环,将第一个类型保存在某个临时变量中,并在循环中打印一个换行符,但我有点卡住了
编辑2:
休息了一会儿,回来后,我设法解决了这个问题,甚至一开始就没有空行。问题是我必须在main类中定义oldTransport:)ofc你不可能知道我的结构是怎样的。谢谢你的电鳗气垫船:)而不是通过
System.out.print(…)
获取行并将其放入变量中。通过String#Split(…)
方法将其拆分,并将获得的第一部分(从拆分中获得的数组[0]点中的字符串)与前一个字符串的第一部分(也保存到变量中)进行比较。如果不同,请在打印行之前创建新行
此外,如果要从扫描仪中提取nextLine(),请检查hasNextLine(),而不是hasNext()
伪码
String oldTransportation gets assigned ""
while the scanner has a next line
String variable line gets scanner's next line
String array called tokens gets this line split on ":"
String newTransportation gets the [0] item in the tokens array
Check if newTransportation equals(...) oldTransportation,
if different, call System.out.println().
print the line variable String
oldTransportation gets assigned newTransportation
end while scanner...
不要通过
System.out.print(…)打印该行。
获取该行并将其放入变量中。通过String#Split(…)
方法将其拆分,并将获得的第一部分(从拆分中获得的数组[0]点中的字符串)与前一个字符串的第一部分(也保存到变量中)进行比较。如果不同,请在打印行之前创建新行
此外,如果要从扫描仪中提取nextLine(),请检查hasNextLine(),而不是hasNext()
伪码
String oldTransportation gets assigned ""
while the scanner has a next line
String variable line gets scanner's next line
String array called tokens gets this line split on ":"
String newTransportation gets the [0] item in the tokens array
Check if newTransportation equals(...) oldTransportation,
if different, call System.out.println().
print the line variable String
oldTransportation gets assigned newTransportation
end while scanner...
让我们跟踪上一个类型
String lastTypeName = "";
for(Types filename: TypeTable) {
if(!lastTypeName.equals(filename.toString()) {
lastTypeName = filename.toString();
System.out.println();
}
Scanner s = new Scanner(filename.toString());
while (s.hasNext()) {
System.out.print(s.nextLine()+ " ");
}
s.close();
}
换行符将在第一行之前打印,但这可能不是问题。让我们跟踪前一种类型
String lastTypeName = "";
for(Types filename: TypeTable) {
if(!lastTypeName.equals(filename.toString()) {
lastTypeName = filename.toString();
System.out.println();
}
Scanner s = new Scanner(filename.toString());
while (s.hasNext()) {
System.out.print(s.nextLine()+ " ");
}
s.close();
}
换行符将在第一行之前打印,但这可能不是问题。我的编辑是如何解释您的伪代码的,但它似乎不起作用。哪里出了问题?我的意思是,第一次它不会打印行,因为“”不等于“car”,因此转到else:写一个新行,给oldTime值“car”。@Yaman:如果它不是一种新的运输类型,你做的与你需要做的相反。您需要
System.out.print(line)
而不是执行System.out.println()
。当然,我不是故意的,我希望我的类型在同一行,而不是每一行都有一行。我试着改变一些东西,尝试并添加了用于调试的打印。我的编辑是我如何解释你的伪代码的,但它似乎不起作用。哪里出了问题?我的意思是,第一次它不会打印行,因为“”不等于“car”,因此转到else:写一个新行,给oldTime值“car”。@Yaman:如果它不是一种新的运输类型,你做的与你需要做的相反。您需要System.out.print(line)
而不是执行System.out.println()
。当然,我不是故意的,我希望我的类型在同一行,而不是每一行都有一行。我试着改变一些东西,尝试并添加了用于调试的打印。是的,我试过你的第一次,但也没有真正奏效。这看起来很简单,虽然我试图保持它与扫描仪和其他易于维护的工具作为我的新手。是的,我试了你的第一次,但它也没有真正的工作。这看起来很简单,虽然我试图保持它与扫描仪和其他易于维护的工具作为im新手。