简体   繁体   English

从 Java 中的 a.txt 文件打印坐标

[英]Printing coordinates from a .txt file in Java

My question is very simple.我的问题很简单。 I'm learning via Oracle iLearning about simple and plain Java but they don't have solved questions and I'm messing around with my brain trying to solve this.我正在通过 Oracle iLearning 学习关于简单明了的 Java 但他们没有解决问题,我正在用我的大脑来解决这个问题。 I have a.txt file that looks like this:我有一个如下所示的 .txt 文件:

128 3.141 Puppies! 128 3.141 小狗!
Line2 This is Line2.线路 2 这是线路 2。 100002 This is still Line2. 100002 这仍然是 Line2。
Line3 3号线
Line4 444 BlueBumper 90 120* Line4 444 蓝色保险杠 90 120*

And what I need is to find the two tokens (ie 90 and 120) following "BlueBumper".我需要的是在“BlueBumper”之后找到两个令牌(即90和120)。

My code looks similar to:我的代码类似于:


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();
    }    
}

I'm trying to store one variable x and one variable y with each token.我正在尝试使用每个令牌存储一个变量 x 和一个变量 y。 I don't really know why the exercise offered the first couple of System.out.println's in order to find the instance "BlueBumper", since I don't need to print anything until the last line, but it feels like I don't quite understand how to make it work in this context.我真的不知道为什么该练习提供了前几个 System.out.println 来找到实例“BlueBumper”,因为直到最后一行我才需要打印任何东西,但感觉就像我不需要'不太明白如何使它在这种情况下工作。

I see only thing you are doing wrong is not going to line 4. you have to add a extra sc.nextLine();.我看到你做错的唯一一件事是不会进入第 4 行。你必须添加一个额外的 sc.nextLine();。 the below is working fine for me.以下对我来说很好。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM