Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Debugging homework assignment/ questions surrounding partially filled arrays

  1. #1
    Junior Member
    Join Date
    Feb 2014
    Posts
    19
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Debugging homework assignment/ questions surrounding partially filled arrays

    So I'm working on a program that so far

    1) reads a set of numbers from an input file, puts them into an array. The array length is a constant number, but the input file (I run the program twice with different input files) either has more or less then the array size. I'm supposed to count the number of elements in the file including the ones which didnt fit in the array.

    This is my code so far.

    import java.util.Scanner;
    import java.io.*;
     
    public class QudratullahMommandi_3_10
    {
     
       public static void main(String[] args) throws IOException
        {
     
        final String inputFile = "QudratullahMommandi_S_10_Input_Set1.txt";
        final String outputFile = "QudratullahMommandi_S_10_Output_Set1.txt";
        int numOfItems;             // num of items in the array
        final int arrayLength = 15; // setting the array length
        int[] indexHolder;
        double[] valueHolder = new double[arrayLength];  // values from input
     
        // opening input file
        File file = new File(inputFile);    
        Scanner input = new Scanner(file);
     
        // Putting values into the array and getting back the num of items in the array
        // then outputting the num of items
        numOfItems = arraysorter(valueHolder,input);
        System.out.println(numOfItems);
     
     
     
       }
     
     
     
     
       public static int arraysorter(double[]arrayVals, Scanner input) 
        { 
          int subMarker = 0;
          int numOfItems = 0;  
          int arrayLength = arrayVals.length;
          int index = 0;
     
          while (input.hasNext())
          { numOfItems++;
            if  (index < arrayLength)
              { arrayVals[index] = input.nextDouble();
                index++;
              }
           }
     
           return numOfItems;
     
        }// end array Sorter
     
     
     
     
     
     
     
     }// end class

    I'm getting some kind of error where it compiles fine but when I run it, the program never ends. I don't get any output or error messages, it stays running and just never ends... and never outputs anything either.



    This is what my input file I'm using looks like

    88
    80
    66
    99
    22
    88
    54
    100
    77
    29
    100
    98
    66
    11
    73
    98
    100
    7
    21
    Last edited by Helplessdrowningpuppy; April 22nd, 2014 at 11:35 PM.


  2. #2
    Member
    Join Date
    Apr 2012
    Posts
    160
    Thanks
    0
    Thanked 27 Times in 27 Posts

    Default Re: Debugging homework assignment/ questions surrounding partially filled arrays

    Most likely you may need to clear the buffer after reading in the double. This is a pretty common issue when using scanners.

  3. The Following User Says Thank You to Parranoia For This Useful Post:

    Helplessdrowningpuppy (April 23rd, 2014)

  4. #3
    Junior Member
    Join Date
    Feb 2014
    Posts
    19
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: Debugging homework assignment/ questions surrounding partially filled arrays

    Quote Originally Posted by Parranoia View Post
    Most likely you may need to clear the buffer after reading in the double. This is a pretty common issue when using scanners.
    I thought it was that but it turned out I had to put something in for the hasNext() clause. I added a trash variable to hold my input for when it didnt fit the if clause and I got it to work.

     
    if  (index < arrayLength)
              { arrayVals[index] = input.nextDouble();
                index++;
              }
     
              else 
              { trashCan = input.nextDouble();
              }

  5. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Debugging homework assignment/ questions surrounding partially filled arrays


  6. The Following User Says Thank You to GregBrannon For This Useful Post:

    Helplessdrowningpuppy (April 23rd, 2014)

  7. #5
    Member
    Join Date
    Feb 2014
    Posts
    180
    Thanks
    0
    Thanked 48 Times in 45 Posts

    Default Re: Debugging homework assignment/ questions surrounding partially filled arrays

    Quote Originally Posted by Helplessdrowningpuppy View Post
    I thought it was that but it turned out I had to put something in for the hasNext() clause. I added a trash variable to hold my input for when it didnt fit the if clause and I got it to work.
    Using a trash variable is not a good way to implement this unless you are required to store or do something with the excess numbers. Using the trash variable slows down your program needlessly (which is not noticeable in this case, but what if there is a very long list of numbers?), and is not a good habit to get into. Instead, you should add a condition in the while loop to check that the array index is less than the length of the array, e.g.,
    while (input.hasNext() && index < arrayLength)
    This way you can even dispense with the if check in the body of the loop.

    Note that I'm assuming you're not interested in reading all the numbers in the file. If you are, then you can either increase the size of the array, or use a variable-size list such as ArrayList or LinkedList.

  8. The Following User Says Thank You to jashburn For This Useful Post:

    Helplessdrowningpuppy (April 23rd, 2014)

  9. #6
    Junior Member
    Join Date
    Feb 2014
    Posts
    19
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: Debugging homework assignment/ questions surrounding partially filled arrays

    Quote Originally Posted by jashburn View Post
    Using a trash variable is not a good way to implement this unless you are required to store or do something with the excess numbers. Using the trash variable slows down your program needlessly (which is not noticeable in this case, but what if there is a very long list of numbers?), and is not a good habit to get into. Instead, you should add a condition in the while loop to check that the array index is less than the length of the array, e.g.,
    while (input.hasNext() && index < arrayLength)
    This way you can even dispense with the if check in the body of the loop.

    Note that I'm assuming you're not interested in reading all the numbers in the file. If you are, then you can either increase the size of the array, or use a variable-size list such as ArrayList or LinkedList.
    We run this program twice with different input files, one of length 6 and one of length 19, but we have to use an array size of 15, to get us used to dealing with partially filled arrays. I used the counter because we need to count the number of items which couldnt fit into the array and output the information, if there were extras, srry if I didn't make it clear, I wrote this in a rush but I had weird specs for the program from my professor.

  10. #7
    Junior Member
    Join Date
    Apr 2014
    Posts
    7
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: Debugging homework assignment/ questions surrounding partially filled arrays

    Quote Originally Posted by Helplessdrowningpuppy View Post
    So I'm working on a program that so far

    1) reads a set of numbers from an input file, puts them into an array. The array length is a constant number, but the input file (I run the program twice with different input files) either has more or less then the array size. I'm supposed to count the number of elements in the file including the ones which didnt fit in the array.

    This is my code so far.

     
        int numOfItems;             // num of items in the array
        final int arrayLength = 15; // setting the array length
    Your array length is set to 15, but you have 19 values?

  11. The Following User Says Thank You to Leklur For This Useful Post:

    Helplessdrowningpuppy (April 23rd, 2014)

  12. #8
    Junior Member
    Join Date
    Feb 2014
    Posts
    19
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: Debugging homework assignment/ questions surrounding partially filled arrays

    Quote Originally Posted by Leklur View Post
    Your array length is set to 15, but you have 19 values?
    Yeah its part of my assignment, we were supposed to read the values from the input file that would fit into the array, and keep a tally of all the values processed so we could output to the console how many values were extraneous if any. So in this case I would output that I had 4 extraneous values.

Similar Threads

  1. Homework Assignment help..
    By mattmattmatt in forum Object Oriented Programming
    Replies: 16
    Last Post: February 26th, 2014, 12:07 AM
  2. Need help with Java Assignment (code partially complete)
    By forgiveme502 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 11th, 2013, 07:39 PM
  3. Homework - Calendar Assignment
    By Reegan777 in forum What's Wrong With My Code?
    Replies: 15
    Last Post: April 26th, 2013, 06:27 AM
  4. Homework assignment (beginner)
    By z3ohyr84 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: May 30th, 2011, 01:32 PM
  5. Array Homework assignment
    By lich0802 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 27th, 2011, 08:17 PM