如何在nextInt之后获取nextInt但保留该值

时间:2015-04-25 00:59:00

标签: java csv java.util.scanner

我的问题不是iValueNext的固定值,而是要运行Excel工作表上的下一个值,即125,152,...

import java.util.*;
import java.io.*;

public class ConvertingData
{
  public static void main (String [] args)
  {
    int i=1;
    int j;
    int iValue;
    int iValueNext;
    try 
    {
      Scanner ifsInput = new Scanner(new File("input.csv"));
      PrintStream ifsOutput = new PrintStream(new File("output.csv"));

      while(ifsInput.hasNextLine())
      {

        String tokens[] = ifsInput.nextLine().split(",");
        String Repeat = tokens[tokens.length - 1];

        String Value = tokens[tokens.length - 3];
        iValue = Integer.parseInt( Value );

        for (i=iValue;i<=iValueNext;i++)
        {
          System.out.println(i+","+Repeat);
          ifsOutput.println(i+","+Repeat);
        }
      }
      ifsInput.close();
      ifsOutput.close();
    }
    catch (FileNotFoundException sMsg)
    {
      System.out.println("File not found");
    }
  }
}

以下是csv文件的一部分:

89,31,31
125,1,32
152,-12,20
155,1,21
181,6,27
287,1,28
290,1,29
308,-8,21

1 个答案:

答案 0 :(得分:0)

如果您在处理当前行时需要“查看”下一行,首先读取所有行:

List<String> lines = new ArrayList<String>();
while(ifsInput.hasNextLine()) {
    lines.add(ifsInput.nextLine());
}
ifsInput.close();

然后逐行处理这些行,并访问下一行:

for (int i = 0; i < lines.size(); i++) {
    String line = lines.get(i);
    String nextLine = i < lines.size() - 1 ? null : lines.get(i + 1);

    String tokens[] = line.split(",");
    String nextTokens[] = nextLine.split(",");

    // whatever logic you need

    ifsOutput.close();
}