简体   繁体   English

Java I / O的字符串数组逻辑问题

[英]String array logic question with java I/O

I need to read a line like this from a file: 5 Chair 12.49 EDIT: I want to be able to have more lines of data. 我需要从文件中读取以下行:5主席12.49编辑:我希望能够拥有更多行数据。

I need to separate each word/number into different data types. 我需要将每个单词/数字分成不同的数据类型。 First one is an int, second is a String, third is a double. 第一个是整数,第二个是字符串,第三个是双精度型。 Then I need to have them formatted like this: Item: Chair Price: $12.49 Quantity: 5 然后,我需要将其设置为以下格式:项目:椅子价格:$ 12.49数量:5

Now I know how to format, the only problem I am having is parsing the string elements into their respected data types (int, string, double). 现在我知道如何格式化了,我唯一的问题就是将字符串元素解析为它们所尊重的数据类型(int,string,double)。

I've tried: 我试过了:

String [] lines = lineInput.split(" ");
for(String displayLines : lines) {
    bufferedWriter.write(displayLines);
    bufferedWriter.newLine();
    System.out.println(displayLines);
}

but that only separates each string into a new line and I couldn't for the life of me figure out how to have each line check if it's an int, string or double. 但这只会将每个字符串分隔成新行,而我一生都无法弄清楚如何检查每行是int,string还是double。

Then I was messing with substring, but couldn't get that to work. 然后我弄乱了子字符串,但无法正常工作。 I even thought of using mod % function and if: 我什至想到使用mod%函数,如果:

if(lines.length % 2 == 0) {it's a string}; 

but that would only work for the first line. 但这仅适用于第一行。

Please believe me when I say i've tried this on my own and am not just making some half-trying effort. 当我说我自己尝试过此操作时,请相信我,而不仅仅是半途而废。 I just giving up. 我只是放弃。 StackOverFlow, you're my only hope :( StackOverFlow,您是我唯一的希望:(

If you have lines with the format [int] [String] [double], you can do: 如果行的格式为[int] [String] [double],则可以执行以下操作:

String [] line = lineInput.split(" ");
int x = Integer.parseInt(line[0]);
String s = line[1];
double d = Double.parseDouble(line[2]);

You can modify this as necessary to handle lines with different formats. 您可以根据需要进行修改,以处理具有不同格式的行。

I'm confused as to why you're splitting lines based on spaces. 我对您为什么要根据空格分割感到困惑。

But anyway, I think to solve your problem look at Integer.parseInt(), Double.parseDouble(). 但是无论如何,我认为要解决您的问题,请查看Integer.parseInt(),Double.parseDouble()。 If you need to check something is a number, you could use an appropriate regular expression (see the String.matches() method). 如果需要检查某些东西是数字,则可以使用适当的正则表达式(请参见String.matches()方法)。 Or a dirty way that might work for your case is to call one of the parsing methods and see if it throws an exception... 或者一种可能适合您的情况的肮脏方法是调用解析方法之一,然后查看它是否引发异常。

You could also look at the Scanner class, but this may be overkill for what you need. 您也可以查看Scanner类,但这对于您所需的内容可能会显得过高。

First, a small nit: calling the string array into which you split your line "lines" is misleading. 首先,一个小技巧:调用将行分割为“线”的字符串数组会产生误导。 Warning: not compiled or tested. 警告:未经编译或测试。


    String [] words = lineInput.split(" "); 
    if (words.length != 2) 
       System.err.println ("Error parsing line [" + lineInput + "]: " + wrong number of tokens on line");
    else
       try {
         int numberOfItems = Integer.parseInt(words[0]);
         String itemName = words[1];
         double itemPrice = Double.parseDouble(words[2]);
         System.out.println (itemName + " Price: $" + itemPrice + " Quantity: " + numberOfItems);
       } catch (NumberFormatException ex) {
          System.err.println ("Error parsing line [" + lineInput + "]: " + ex);
       }
    } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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