[英]Printing coordinates from a .txt file in Java
我的问题很简单。 我正在通过 Oracle iLearning 学习关于简单明了的 Java 但他们没有解决问题,我正在用我的大脑来解决这个问题。 我有一个如下所示的 .txt 文件:
128 3.141 小狗!
线路 2 这是线路 2。 100002 这仍然是 Line2。
3号线
Line4 444 蓝色保险杠 90 120*
我需要的是在“BlueBumper”之后找到两个令牌(即90和120)。
我的代码类似于:
import java.util.Scanner;
public class Input04 {
public static void main(String[] args){
Scanner sc = new Scanner(Input04.class.getResourceAsStream("input04text.txt"));
//Edit these lines to advance the scanner
sc.nextLine();
System.out.println(sc.nextLine());
//Does this line contain "BlueBumper"?
System.out.println(sc.findInLine("BlueBumper"));
//Store the next two numbers as xPosition and yPosition
//Print these positions
int x = sc.nextInt();
int y = sc.nextInt();
System.out.println("X: "+ x +", Y: " + y);
sc.close();
}
}
我正在尝试使用每个令牌存储一个变量 x 和一个变量 y。 我真的不知道为什么该练习提供了前几个 System.out.println 来找到实例“BlueBumper”,因为直到最后一行我才需要打印任何东西,但感觉就像我不需要'不太明白如何使它在这种情况下工作。
我看到你做错的唯一一件事是不会进入第 4 行。你必须添加一个额外的 sc.nextLine();。 以下对我来说很好。
public class Input04 {
public static void main(String[] args) {
Scanner sc = new Scanner(Input04.class.getResourceAsStream("input04text.txt"));
//Edit these lines to advance the scanner
// below will print, if you print : 128 3.141 Puppies!
sc.nextLine();
// will print : Line2 This is Line2. 100002 This is still Line2.
System.out.println(sc.nextLine());
// will print, if you print : Line3
sc.nextLine();
//Does this line contain "BlueBumper"?
System.out.println(sc.findInLine("BlueBumper"));
//Store the next two numbers as xPosition and yPosition
//Print these positions
int x = sc.nextInt();
int y = sc.nextInt();
System.out.println("X: "+ x +", Y: " + y);
sc.close();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.