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

    Default [New Issue] element for my array is showing null

    Sorry about the previous one ! I fixed my code but the output i want is showing nulls
    1000.txt
    Java Code:
    index = 9981, element = null
    index = 9982, element = null
    index = 9983, element = null
    index = 9984, element = null
    index = 9985, element = null
    index = 9986, element = null
    index = 9987, element = null
    index = 9988, element = null
    index = 9989, element = null
    index = 9990, element = null
    index = 9991, element = null
    index = 9992, element = null
    index = 9993, element = null
    index = 9994, element = null
    index = 9995, element = null
    index = 9996, element = null
    index = 9997, element = null
    index = 9998, element = null
    index = 9999, element = null
    electricity.txt is also showing nulls for its element how can i fix it?

    Here is my code:
    Java Code:
    import java.util.Scanner;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.InputMismatchException;
    import java.util.NoSuchElementException;
    
    public class Testing1
       public static void main(String[] args)throws InputMismatchException
       {
          if(args.length == 0)
          {
                //This prompt will show when no input detected on the command line arguments.
             System.out.println("Please input the file name.");
          }
          else
          {
             Integer[] array = Testing1.readFileReturnIntegers(args[0]);
             Testing1.printArrayAndIntegerCount(array, args[0]);
          }
       }// end of main
         
       public static Integer []readFileReturnIntegers(String inputData)
       { 
          Integer [] listOfIntegers = new Integer[10000];
          Integer index = new Integer(0); 
          int count = 0;
          
          File data = new File(inputData);
          Scanner inputReader = null;
          try 
          {
             	   //This will create a connection to the file.
             inputReader = new Scanner(data);
          } 
          catch (FileNotFoundException exception)
          {
             		   //This message will appear when the text file entered doesn't exist.
             System.out.print("ERROR: File not found for: \"");
             System.out.println(inputData+ "\"");
          } 
                //If the text file does exist, it will go through the file.
          if (inputData != null)
          {
             System.out.print("number of integers in file " + " \"");
             System.out.println(inputData + "\"");
                      while (inputReader.hasNextLine())
             {
                try
                {  
                   	   //reads an integer in a line of text       
                   Integer element = inputReader.nextInt();
                   listOfIntegers[count]=element;
                   		//prints out the integer and and its index in the array
                   System.out.println("index = " + index + ", element = " + element);
                   index ++;
                   count ++;
                }
                catch (InputMismatchException e)
                {
                   String word = inputReader.next();
                }
                catch (NoSuchElementException e)
                {
                   		  //to avoid program crashing against this exception.
                }
             }//end of while
          }
                    
          return listOfIntegers;             
       }//end of readFileReturnIntegers
            
       public static void printArrayAndIntegerCount(Integer [] array, String inputData)
       {  
          int count = 0; 
          System.out.print("\nTotal number of integers in file: " + " \"");
          System.out.println(inputData + "\"" + " = " + count);
          
       }//end of printArrayAndIntegerCount
             
    }//end of class
    My main issue here is since i put my integer range to 10000, it goes over there and shows null.

    Is there anyway to stop it at the limit of my input files? anything i need to fix on my loops?

    Here is electricity.txt
    Java Code:
     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
    Java Code:
    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
        till index 1000 and element 7919
    Attached Files Attached Files
    Last edited by aldz24; 08-30-2016 at 11:21 AM. Reason: Fixed my old issue

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    27

    Default Re: Converting Code to Methods and Error problems

    I worry when I see this sort of thing:
    //to avoid program crashing against this exception.

    That looks like sticky tape over a bug.
    At least print the stack trace.

    Same with the other exception you catch. You'll never know if there's a problem when parsing the file.

    Do you have a sample of the data in the files?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

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

    Default Re: Converting Code to Methods and Error problems

    Quote Originally Posted by Tolls View Post
    I worry when I see this sort of thing:
    //to avoid program crashing against this exception.

    That looks like sticky tape over a bug.
    At least print the stack trace.

    Same with the other exception you catch. You'll never know if there's a problem when parsing the file.

    Do you have a sample of the data in the files?
    yessir i added the input files i need i think it works if i change the value 10000 to 1000 but it would show an exception. I think it is required for me to do 10000 but i don't know how to stop the loop depending on the length of the input files' index and element

    EDIT:

    I attached the input files i'm doing
    Last edited by aldz24; 08-30-2016 at 11:15 AM.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    27

    Default Re: Converting Code to Methods and Error problems

    OK, so first off you need to skip a couple of lines until you get to the first line with integers on it.
    That would solve one of your exception issues.

    If you know how many Integers there are then using a hard coded array size is fine.
    If not, then you probably ought to use a List<Integer> instead.

    Finally I would read each line and then use a separate Scanner to pull out the integers.
    This will let you check line to see whether it has integers on it or not.

    Java Code:
    while hasNextLine
        read line into a new Scanner
        if line starts with an int (hasNextInt) then
            read all the ints into the array or list
    Something like that.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

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

    Default Re: Converting Code to Methods and Error problems

    A couple observations (caveate, I have not tried to compile the code):

    Line 42, inputData can never be null. Base on the fact that you
    used it in line 28, perhaps you meant data. And the validity of the
    data File object should be checked as soon as possible. Then you
    can forget about it. It also makes the code easier to read.
    Also, you can used various File methods such as exists, etc. to check accessibility.

    Stay away from static methods! Otherwise, you will develop
    bad habits. They have their use but in your case I would make
    the methods instance methods and declare some of your important
    variables as instance fields. Then you don't have to worry as much
    about scope. Static vs instance is a source of confusion for
    those new to programming so I recommend you learn it soon.

    Unless you are using List<> as Toll's suggested, I recommend you just
    use primitive types (int vs Integer) when you can.

    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. Problems with converting Python to JavaScript
    By mariabutenka in forum New To Java
    Replies: 1
    Last Post: 03-16-2014, 02:48 AM
  2. Replies: 2
    Last Post: 02-26-2013, 09:18 AM
  3. converting methods into pseudo codes
    By hd300 in forum New To Java
    Replies: 3
    Last Post: 03-07-2012, 03:32 PM
  4. Replies: 0
    Last Post: 12-10-2010, 04:55 AM
  5. [SOLVED] [HELP] converting methods
    By Gods Army in forum New To Java
    Replies: 5
    Last Post: 03-06-2009, 11:46 AM

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
  •