0

Currently having an error issue when I compile the file

I've been checking my while loops and stuff and see where the issue is but i can't find it

It's like the only error showing now (I hope)

Hopefully someone can help out on which bracket i am missing it on or what type of bracket it is cuz i am confused ;_;

import java.util.Scanner;
import java.io.File;
import java.util.StringTokenizer;
import java.util.InputMismatchException;
import java.io.FileNotFoundException;
import java.util.NoSuchElementException;


  public class InputFiler {
   
   public static void main(String [ ] args)throws InputMismatchException
    {
      //error checking for commandline input 
      //to make sure the user entered at least one comandline argument
      if(args.length == 0)
      {
         System.out.println("Please enter the file name " +
             "as the 1st commandline argument.");
      }
      else
      {
      
         Integer[ ] array = InputFiler.readFileReturnIntegers(args[0]);
         InputFiler.printArrayAndIntegerCount(array, args[0]);       
      }
     
   }//end of main


    if(filename.length() == 0){
     System.out.println("Please enter the file name as the 1st commandline argument.");
  }
        else {   //attempt connect and read file 
         File file = new File(filename);
          Scanner inputFromFile = null;
          }
     try {
        inputFromFile = new Scanner(file);
     }
     
     catch (FileNotFoundException fnfe) {
        System.out.print("ERROR: File not found for \"");
        System.out.println(filename+"\"");
     }        
     //if made connection to file, read file
     if(inputFromFile != null){         
        System.out.print("Reading from file \"" + inputFile + "\":\n");
        //loop and print to check if file connected

        //read next integer and store into array
        while (inputFromFile.hasNextLine()) {
           try {
              x = inputFromFile.nextInt();
              array[i] = x;
              i++;
              System.out.println(x);

           } 
           catch (InputMismatchException ime) {
              inputFromFile.next();
           }
           catch (NoSuchElementException nsee) {
           }
        }   
     }
     
     //...
      return array;
   }//end of readFileReturnIntegers
   
 
 System.out.println("Number of integers in file \"" + inputFile + "\" = " + array.length);
  //print array index and elements
     for(int i=0;i<array.length;i++) {
     if(array[i] != null){
     System.out.print("\nindex = " + i + ", ");
     System.out.print("element = " + array[i]);
     }
  }
}      

  //...
   }//end of printArrayAndIntegerCount
     
}//end of class

The error it shows is this:

}//end of class ^ 1 error

I am certainly missing something but I don't know which one

these are the input txt files i need to show for my output

it needs to match the length of index at the top (that's why i used array.length)

electricity.txt

number of integers in file "electricity.txt" = 4
    index = 0, element = 1877
    index = 1, element = 1923
    index = 2, element = 1879
    index = 3, element = 2000

1000.txt

number of integers in file "1000.txt" = 1001
    index = 0, element = 1000
    index = 1, element = 2
    index = 2, element = 3
    index = 3, element = 5
    index = 4, element = 7
    index = 5, element = 11
    index = 6, element = 13
    index = 7, element = 17
    index = 8, element = 19
    till index 1000 and element 7919

This is the code that works without any errors but nulls are showing up after a certain number for each of the txt file is reach

import java.util.Scanner;
import java.io.File;
import java.util.StringTokenizer;
import java.util.InputMismatchException;
import java.io.FileNotFoundException;
import java.util.NoSuchElementException;

public class Testing

   
   public static void main(String[] commandlineArguments)throws InputMismatchException 
   {
      if(commandlineArguments.length == 0)
      {
         System.out.println("Please enter the file name " +
             "as the 1st commandline argument.");
      }
      else
      {
         Integer[] array = Testing.readFileReturnIntegers(commandlineArguments[0]);
         Testing.printArrayAndIntegerCount(array, commandlineArguments[0]);
      }
   } 
 
   public static Integer []readFileReturnIntegers(String inputFile)
   {
       
      Scanner scanFile = null;
           
      try {
         scanFile = new Scanner(file);
      } 
      catch (FileNotFoundException exception) {
                 
         System.out.print("ERROR: File not found for \"");
         System.out.println(inputFile +"\"");
      }  
                  
      if(scanFile != null)
      {
          
         while (scanFile.hasNextLine()) 
         {   
            try
            {                       
               int element = scanFile.nextInt();
               array[size] = element;
               size++;                                 
            }
            catch (InputMismatchException exception)
            {
               scanFile.next();
            } 
            catch (NoSuchElementException exception)
            {
               scanFile.next();
            }        
         }
      }
     
      return array;          
   }

   public static void printArrayAndIntegerCount(Integer [] array, String inputFile)
   {
      int num = 0;     
      System.out.println("number of integers in file " + inputFile + " = " + array.length);
     
      for (int i = 0; i <array.length; i++)
      {
         System.out.println("index = " + i + ", element = "+ array[i]);
           
      }
     
     
   }
       
                                                                                                                                                                                                                                                                                                   
            
}
aldz24
  • 65
  • 8

1 Answers1

1

I took a few liberties but I think this is close to what you're needing.

import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;

public class InputFiler {

public static void main(String[] args) throws InputMismatchException {
    //error checking for commandline input
    //to make sure the user entered at least one comandline argument
    if (args.length == 0) {
        System.out.println("Please enter the file name " +
                "as the 1st commandline argument.");
    } else {

        List<Integer> array = InputFiler.readFileReturnIntegers(args[0]);
        InputFiler.printArrayAndIntegerCount(array, args[0]);
    }

}//end of main

private static List<Integer> readFileReturnIntegers(String filename) {
    File file ;
    Scanner inputFromFile = null;
    List<Integer> array = new ArrayList<>();
    if (filename.length() == 0) {
        System.out.println("Please enter the file name as the 1st commandline argument.");
    } else {   //attempt connect and read file
        file = new File(filename);
        try {
            inputFromFile = new Scanner(file);
        } catch (FileNotFoundException fnfe) {
            System.out.print("ERROR: File not found for \"");
            System.out.println(filename + "\"");
        }
        //if made connection to file, read file
        if (inputFromFile != null) {
            System.out.print("Reading from file \"" + filename + "\":\n");
            //loop and print to check if file connected

            //read next integer and store into array
            while (inputFromFile.hasNextLine()) {
                try {
                    int x = Integer.parseInt(inputFromFile.nextLine());
                    array.add(x);
                    System.out.println(x);
                } catch (InputMismatchException ime) {
                    inputFromFile.next();
                } catch (NoSuchElementException nsee) {
                }
            }
        }
    }

    return array;
}//end of readFileReturnIntegers

private static void printArrayAndIntegerCount(List<Integer> array, String inputFile) {
    System.out.println("Number of integers in file \"" + inputFile + "\" = " + array.size());
    //print array index and elements
    for (int i : array) {
        System.out.print("element = " + i);
    }
}//end of printArrayAndIntegerCount
}//end of class

theser were the error that were showing

InputFiler.java:25: error: cannot find symbol
   private static List<Integer> readFileReturnIntegers(String filename) {
                  ^
  symbol:   class List
  location: class InputFiler
InputFiler.java:65: error: cannot find symbol
   private static void printArrayAndIntegerCount(List<Integer> array, String inputFile) {
                                                 ^
  symbol:   class List
  location: class InputFiler
InputFiler.java:19: error: cannot find symbol
         List<Integer> array = InputFiler.readFileReturnIntegers(args[0]);
         ^
  symbol:   class List
  location: class InputFiler
InputFiler.java:28: error: cannot find symbol
      List<Integer> array = new ArrayList<>();
      ^
  symbol:   class List
  location: class InputFiler
InputFiler.java:28: error: cannot find symbol
      List<Integer> array = new ArrayList<>();
                                ^
  symbol:   class ArrayList
  location: class InputFiler
5 errors

I added these imports at the top

import java.util.Scanner;
import java.io.File;
import java.util.StringTokenizer;
import java.util.InputMismatchException;
import java.io.FileNotFoundException;
import java.util.NoSuchElementException;
Danny Beaumont
  • 356
  • 1
  • 8
  • @aldz24 make sure you have the imports I compiled and ran it. import java.io.File; import java.io.FileNotFoundException; import java.util.*; – Danny Beaumont Aug 29 '16 at 06:52
  • InputFiler.java:25: error: cannot find symbol private static List readFileReturnIntegers(String filename) { InputFiler.java:25: error: cannot find symbol showing these type of errors – aldz24 Aug 29 '16 at 06:56
  • import java.util.List; – Danny Beaumont Aug 29 '16 at 07:02
  • alright i finally compiled it..issue is with my previous code, i was trying to use an input file 1000.txt and electricty.txt to work it and output the index and the elements . – aldz24 Aug 29 '16 at 07:08
  • problem with this one is it can't read the two txt files says this: Reading from file "electricity.txt": Exception in thread "main" java.lang.NumberFormatException: For input string: "SCIENCE and ELECTRICITY" for electricty.txt Reading from file "1000.txt": Exception in thread "main" java.lang.NumberFormatException: For input string: " The First 1,000 Primes" and this for 1000.txt – aldz24 Aug 29 '16 at 07:11
  • it's expecting an Integer if you put doubles or strings it will error out. if you want it to accept everything. you'll change List to List remove the Integer.parseInt() and add the string. – Danny Beaumont Aug 29 '16 at 07:12
  • any changes i need to do? i added the txt files i have to match in my first post – aldz24 Aug 29 '16 at 07:16
  • i added a new post at the top with my other version of code that works but nulls are showing up – aldz24 Aug 29 '16 at 07:23