java:如何使用.txt中的数据创建多个数组 [英] java: How to create multiple arrays using data from .txt

查看:71
本文介绍了java:如何使用.txt中的数据创建多个数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以这是我必须编写的第一个真正的Java程序. 我对Java也很陌生.

so this is my first real java program i have to write. I'm also fairly new to java..

该程序必须使用2个命令行参数(假定是x和y坐标)运行,然后确定坐标位于哪个城市和县.为此,我想使用一个绕线号码".

The program must run with 2 command-line arguments, which are suppose to be x and y coordinates and then determines in which municipality and county the coordinates are. For this I'm suppose to use a 'winding number'.

但是在开始执行程序的那些部分之前,我首先需要创建并填充不同的数组,对吧?

BUT before I can start with those parts of the program, I first need to create and fill different array's , right?

.txt文件包含以下内容:(所有地点和坐标都在荷兰顺便说一句) 县市的身份证号码. 例如:

The .txt file contains the following: (All places and coords are in the Netherlands btw) ID number county municipality. for example:

0 Groningen Haren

0 Groningen Haren

1个格罗宁根韭菜

etc等.接下来的61650行是这样的坐标:

etc etc for the first 518 lines. The next 61650 lines or so are coordinates looking like this:

0 6.665039 53.181004

0 6.665039 53.181004

0 6.666431 53.180642

0 6.666431 53.180642

第一个整数(此处为0)与所在城市相对应

Where the first integer number ( here 0) corresponds with the municipality

回到问题.无法更改文本文件,我必须像在cmd中那样使用redirection(?):java'programname'< filename.txt.它应该读取整个文件,创建1个数组,其中的ID号都是整数.一个县城和一个市政府的阵列,对不对?但是当数据在同一行上时,如何创建3个不同的数组(oke 2、1个整数数组和2个字符串数组).

To get back to the question. The textfile can not be altered, I have to use redirection(?) like in the cmd: java 'programname' < filename.txt. and it should read the entire file, create 1 array with only id numbers which are all integers. An array with the county and an array with municipality, right? But HOW do I create 3 different arrays ( oke 2, 1 integer array and 2 string arrays) when the data is on the same line..

我想使用StdIn.readInt,StdIn.readString和StdIn.readLine,但我只是无法使其工作.

I'm suppose to use StdIn.readInt, StdIn.readString and StdIn.readLine but I just cannot make it work.

在此先感谢您的帮助.

这是我到目前为止所拥有的,它给出了错误,因为(我想是因为这样)它希望读取一个整数,但是同一行上也有字符串..

This is what I have so far, It gives error because ( I think it's because of this) it expects to read an integer but there is also strings on that same line..

public class test{

 public static void main(String[] args){


   int n = 519;
   Integer[] a = new Integer[n];
   for(int i = 0; i < n; i++){
   a[i] = StdIn.readInt();
   StdOut.println(a[i]);
    }
 }
}

推荐答案

由于不同的条目由行分隔,因此请阅读完整的行,然后将其拆分为空格字符,然后将第一个转换为int并将其分配给int数组,另外两个到您的字符串数组:

Since different entries are separated by line, read a complete line, then split it at the space character and then convert the first to an int and assign it to the int array and the other two to your string arrays:

String line = StdIn.readLine();
String[] split = line.split(" ");
intArray[X] = Integer.parseInt(split[0]);
stringArray1[X] = split[1];
stringArray2[X] = split[2];

您可以循环执行此操作.

You can do this in a loop.

要读取类似java -jar your.jar < inputFile.txt的文件,您需要将阅读器的输入流设置为System.in,关于如何执行此操作(或者StdIn是否为您执行此操作),我不知道来源,无法知道用于StdIn.

To read a file like java -jar your.jar < inputFile.txt you need to set the input stream of your reader to System.in, on how to do it (or if StdIn does that for you) I cannot tell, without knowing the source for StdIn.

编辑

如果我对您的理解正确,那么您无需执行任何操作,StdIn就会自动使用System.in进行初始化.

When I understood you correctly, then you don't need to do anything, StdIn initializes itself automatically with System.in.

这意味着StdIn.readLine()将返回输入文件的第一行.您可以使用以下命令读取文件的每一行:

This means StdIn.readLine() will return the first line of the input file. You can read each line of the file with:

while (StdIn.hasNextLine())
{
    String line = StdIn.readLine();
    // stuff from above
}

要从命令行参数获取x和y,您需要从主方法访问String[] args参数:

To get x and y from the command line argument you need to access the String[] args argument from the main method:

int x = Integer.parseInt(args[0]);
int y = Integer.parseInt(args[1]);

请注意,我在此处编写的所有代码都不会检查可能发生的任何异常(例如,如果您不输入两个int).我想这取决于您的任务(如果它说您可以假设输入正确,那么上面的代码应该可以).

Note that all code I wrote here does not check for any exceptions that might occure (for example, if you don't enter two ints). I guess this depends on your task (if it says you can assume correct input, the above code should be fine).

然后可以像这样执行Java程序:java -jar your.jar 100 20 < inputFile.txt.其中x是100,y是20,inputFile.txt是包含位置数据的文件.

You can then execute your java program like this: java -jar your.jar 100 20 < inputFile.txt. Where 100 is the value for x and 20 the value for y and inputFile.txt the file with the location data.

这篇关于java:如何使用.txt中的数据创建多个数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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