有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java通过读取文件。下一行

当我运行我的程序时,我得到了以下错误。它运行在文件的第一行,但当它返回到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();
}  
}

共 (1) 个答案

  1. # 1 楼答案

    请看以下几行:

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

    在这里,您两次调用了next()方法,第二次调用没有使用hasNext()进行显式检查。你有NoSuchElementException的风险。这与你的问题没有直接关系,但你会发现下一个问题

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