通过.nextLine从文件中读取

时间:2015-03-05 06:35:06

标签: java indexoutofboundsexception

运行程序时出现以下错误。它运行在文件的第一行,但是当它返回到main方法中的循环来处理第二行时,这就是我得到的。

java.lang.ArrayIndexOutOfBoundsException: 1 
    at Popcorn.barGraphByLine(Popcorn.java:94)
    at Popcorn.main(Popcorn.java:31)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)

我的代码:

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

public class Popcorn
{

public static void main(String args[])throws IOException
{

printHeading(4, "Popcorn");

//This will call a method to validate a user input as a file name, then
//create a Scanner object to read from the FileReader object
String fileName = validatedFileName();
Scanner inFromFile = new Scanner(new FileReader("Test.txt"));

printBarGraphHeader();

while(inFromFile.hasNextLine())
{
  String thisLine = inFromFile.nextLine();
  barGraphByLine(thisLine);
}


inFromFile.close();
}

private static String validatedFileName()
{
Scanner keyboardIn = new Scanner(System.in);

System.out.print("Please enter a file name.");
String tryName = keyboardIn.next();
File createFile = new File(tryName);

while(!createFile.exists())
{
  System.out.print("The file name you entered is invalid, Please enter a
  vaild file name.");
  tryName = keyboardIn.next();
  createFile = new File(tryName);
} 
keyboardIn.close();
return tryName;
}

public static void printBarGraphHeader()
{
System.out.println("                     Popcorn Co-Op                   ");
System.out.println();
System.out.println("                             Production in Hundreds  ");
System.out.println("                             of Pint Jars per Acre   ");
System.out.println("Farm Name                       1   2   3   4   5   6");
System.out.println("                             ---|---|---|---|---|---|");
}


public static void barGraphByLine(String str)
{
String acresString = "";
String jarsString = "";

//This takes any spaces of one or more grouped together and replaces it
//with a single space.
str = str.replaceAll("\\s+" , " ");

//This takes the String str that is composed of the entire line from the file and splits it into two strings at the
//comma, since every name has a comma after it.
String[] splitStr = str.split(", ", 2);

String str1 = splitStr[0];
String str2 = splitStr[1];

String farmName = str1;

Scanner readStr2 = new Scanner(str2);

while(readStr2.hasNext())
{

  acresString = readStr2.next();

  jarsString = readStr2.next();

}

double acres = Double.parseDouble(acresString);
double jars = Double.parseDouble(jarsString);
double jarsPerAcre = jars/acres;

String graphStars = "";
if(jarsPerAcre < 400)
{
  for(int count = 25; count < jarsPerAcre; count += 25)
  {
    graphStars += "*";
  }
  for(int count = 375; count >jarsPerAcre;count -= 25)
  {
    graphStars = graphStars + " ";
  }
  graphStars = graphStars + "|";
}
else
{
  for(int count = 25; count < 400; count += 25)
  {
    graphStars += "*";
  }
  graphStars += "#";
  for(int count = 400; count < jarsPerAcre; count += 25)
  {
    graphStars += "*";
  }
}


int afterNameSpaceCount = 29 - farmName.length();
for(int count = 0; count < afterNameSpaceCount; count++)
{
  farmName = farmName + " ";
}

System.out.println(farmName + graphStars);

readStr2.close();
}  
}

2 个答案:

答案 0 :(得分:1)

看看以下几行:

while (readStr2.hasNext()) {
  acresString = readStr2.next();
  jarsString = readStr2.next();
}

此处您正在调用next()方法两次,并且没有明确检查hasNext()第二次调用。您有NoSuchElementException风险与您的问题没有直接关系,但您将会找到下一个问题。

如果您想阅读两个字符串,则每次都必须拨打hasNext()。但是,请注意:使用您的代码,如果while语句循环超过2次,您将丢失从readStr2读取的一些值。

答案 1 :(得分:0)

我发现了我的问题,我的Test.txt文件在数据行之间有一个空行。因此,发送给方法的第二个字符串(由空行组成)导致错误。