Question

I am having trouble creating a method to import data from a txt file into a 1d string array and a 2d double array. Here is what I have thus far:

public static void main(String[] args){
    String[] productName = new String[100];
    double[][] sales = new double[100][5];
    initializeArrays(productName, sales);

....}
  public static void initializeArrays(String a[], double b[][]){
    try
    {
      String output = "";
      File inputFile = new File("c:/temp/salesdata.txt");
      if (inputFile.exists())
      {
        Scanner scanner = new Scanner(inputFile);
        while (scanner.hasNext())
        {
          for (i=1;i<6;i++)
          {
            b[a.length][i]=scanner.nextDouble();  
          }
          rowCount += 1;
        }
      }

    }
      catch (FileNotFoundException e)
    {
      System.out.println("Error reading file: ");
    }

No correct solution

OTHER TIPS

Try this code:

public static void initializeArrays(String a[], double b[][]){
    try
    {
      String output = "";
      File inputFile = new File("c:/temp/salesdata.txt");
      int counter = 0;
      if (inputFile.exists())
      {
        Scanner scanner = new Scanner(inputFile);
        while (scanner.hasNext())
        {
          for (i=1;i<6;i++)
          {
            b[counter][i]=scanner.nextDouble();  
          }
          counter++;
          rowCount += 1;
        }
      }

    }
      catch (FileNotFoundException e)
    {
      System.out.println("Error reading file: ");
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top