Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By jim829

Thread: Error and Fixing Outputs integers from user input external files.

  1. #1
    aldz24 is offline Member
    Join Date
    Aug 2016
    Posts
    21
    Rep Power
    0

    Default Error and Fixing Outputs integers from user input external files.

    So I am currently getting an error from my code.

    Java Code:
    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
    
    
       public static Integer []readFileReturnIntegers(String filename){
        int i = 0;
            int x = 0;
          Integer [] array = new Integer[10000];//instantiate array of 10000 integers
          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 \"" + filename + "\":\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
       
      
    public static void printArrayAndIntegerCount(Integer [ ] array, String filename) {
      //print number of integer in file
      System.out.println("Number of integers in file \"" + filename + "\" = " + 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 thing
         
    }//end of class
    	     
    }
    here are the errors

    InputFiler:88: error: illegal start of expression
    public static void printArrayAndIntegerCount(Integer [ ] array, String filename) {
    ^
    InputFiler:88: error: illegal start of expression
    public static void printArrayAndIntegerCount(Integer [ ] array, String filename) {
    ^
    InputFiler.java:88: error: ';' expected
    public static void printArrayAndIntegerCount(Integer [ ] array, String filename) {
    ^
    InputFiler.java:88: error: '.class' expected
    public static void printArrayAndIntegerCount(Integer [ ] array, String filename) {
    ^
    InputFiler.java:88: error: ';' expected
    public static void printArrayAndIntegerCount(Integer [ ] array, String filename) {
    ^
    InputFiler.java:88: error: ';' expected
    public static void printArrayAndIntegerCount(Integer [ ] array, String filename) {


    I am fixing my program so I can use the two txt files i'm using to work which are:

    electricity.txt
    1000.txt

    I was having issues with it showing null values for some reason so I had to used array.length instead

    I need to match the ou using my code:

    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
    to.... index 1000 and element 7919

    This is my first post so and since i'm new to java and arrays too hopefully someone can help out ! :(

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Error and Fixing Outputs integers from user input external files.

    First, you need to double check your {}. Match everything up and make certain the indentation is
    consistent. Looks like you are missing a brace at lines 37-39.

    Once you do that you will have another (at least one) problem. The inputFromFile variable within the else block is also
    local to that block. So it goes out of scope and is essentially gone once program exits that block.

    Regards,
    Jim
    Last edited by jim829; 08-29-2016 at 02:12 AM.
    aldz24 likes this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    aldz24 is offline Member
    Join Date
    Aug 2016
    Posts
    21
    Rep Power
    0

    Default Re: Error and Fixing Outputs integers from user input external files.

    Quote Originally Posted by jim829 View Post
    First, you need to double check your {}. Match everything up and make certain the indentation is
    consistent. Looks like you are missing a brace at lines 37-39.

    Once you do that you will have another (at least one) problem. The inputFromFile variable within the else block is also
    local to that block. So it goes out of scope and is essentially gone once program exits that block.

    Regards,
    Jim
    Thanks !

  4. #4
    aldz24 is offline Member
    Join Date
    Aug 2016
    Posts
    21
    Rep Power
    0

    Default Re: Error and Fixing Outputs integers from user input external files.

    Quote Originally Posted by jim829 View Post
    First, you need to double check your {}. Match everything up and make certain the indentation is
    consistent. Looks like you are missing a brace at lines 37-39.

    Once you do that you will have another (at least one) problem. The inputFromFile variable within the else block is also
    local to that block. So it goes out of scope and is essentially gone once program exits that block.

    Regards,
    Jim
    I am now getting an

    }//end of class
    ^
    1 error

    Am I missing something? O_o

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Error and Fixing Outputs integers from user input external files.

    Once again, look at your braces and ensure they match and are located correctly. Sometimes you can also have too many closing braces.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. User Input for GUI error message
    By deeevo in forum New To Java
    Replies: 4
    Last Post: 05-24-2013, 09:19 AM
  2. Replies: 4
    Last Post: 12-12-2012, 11:27 AM
  3. Replies: 3
    Last Post: 09-15-2012, 04:29 PM
  4. Replies: 0
    Last Post: 07-22-2011, 12:31 PM
  5. dynamically search user input files
    By Juuno in forum Advanced Java
    Replies: 2
    Last Post: 04-29-2009, 04:51 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •