我想从一个文件中读取一个(例子)类似'@ 123 @ 123 @ 123'的模式。遇到@后,123将被存储在一个数组中,依此类推。 [英] I want to read a (example) pattern something like '@123@123@123' from a file. After encountering @, 123 to be stored in an array, and so on.

查看:91
本文介绍了我想从一个文件中读取一个(例子)类似'@ 123 @ 123 @ 123'的模式。遇到@后,123将被存储在一个数组中,依此类推。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要阅读的实际文件如下:

@

1

2





90

@

1

2





90

@

1





$

@

我想读取文件,遇到@时,@之后的数据(这里是整数)必须读入数组。然后在数据传输到数组后,继续读取文件,当再次遇到@时,重复该过程并再次将@中的下一组数据存储在数组中。



我尝试了什么:



我试过这段代码,这给了我一个错误。我不明白这是什么错误。请帮我。显示错误的代码行如下:

My actual file to be read is something like:
@
1
2
.
.
90
@
1
2
.
.
90
@
1
.
.
90
@
I want to read the file, when @ is encountered, the data (here integers) after @ must be read into an array. Then after data is transferred to the array, resume reading the file, when @ is encountered again, repeat the process and store the next set of data after @ in the array again.

What I have tried:

I have tried this code,which is giving me an error. I don't understand what is the mistake. Please help me. The line of code that shows an error is below:

public void P()
        {
            while(s.hasNext())
            {
            String lineOfText = s.nextLine();
            if (lineOfText.startsWith("@"))
            
                
            {
            
            }
            
			codes[m] = s.nextInt();
			m++;
            
            }
		
        }



当我只有

@

1

2







在文件中,代码工作正常。但我的文件是像上面描述的iv'e模式。请帮忙。


When I have only
@
1
2
.
.
90
in the file, the code works fine. But my file is a pattern like iv'e described above. Please help.

推荐答案

试试这个:

Try this:
while(s.hasNext()) {
    String lineOfText = s.nextLine();
    if (lineOfText.startsWith("@")) {
        // ignore lines starting with @
    }
    else {
        codes[m] = Integer.parseInt(lineOfText);
        System.out.println("num: " + codes[m]);
        m++;
    }
}


查看你的代码。

如果你读的行开头会怎么样'@'?如果它做了什么呢?如果不是,会发生什么?在实践中是否做了什么?



并帮自己一个忙:用文本编辑器仔细查看输入文件(可能是二进制查看器好):你在这里描述了同一个问题中两种截然不同的格式,数据不能同时存在!
Look at your code.
What happens if the line you read starts with '@'? What does it do if it does? What happens if it isn't? Does the if do anything in practice?

And do yourself a favour: have a good close look at your input file with a text editor (and possible a binary viewer as well): you describe two very different formats in the same question here, and the data cannot be both!


这篇关于我想从一个文件中读取一个(例子)类似'@ 123 @ 123 @ 123'的模式。遇到@后,123将被存储在一个数组中,依此类推。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆