6 Replies - 302 Views - Last Post: 22 April 2014 - 11:30 PM Rate Topic: -----

#1 Banapple  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 22-March 14

Default Debugging homework assignment/ partially filled arrays

Posted 22 April 2014 - 09:23 PM

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

Is This A Good Question/Topic? 0
  • +

Replies To: Default Debugging homework assignment/ partially filled arrays

#2 DimitriV  Icon User is offline

  • They don't think it be like it is, but it do
  • member icon

Reputation: 584
  • View blog
  • Posts: 2,738
  • Joined: 24-July 11

Re: Default Debugging homework assignment/ partially filled arrays

Posted 22 April 2014 - 09:42 PM

If you have no idea where it's infinite looping or hanging, maybe you should put:
System.out.println(arrayVals[index]);


Right after you read the value into the array. Another thing (I'm not too sure about this though) is that the Scanner could be hanging on the newline characters in the file, so you could also try:
input.nextLine();


Was This Post Helpful? 1
  • +
  • -

#3 IvGotAPocket  Icon User is offline

  • D.I.C Head

Reputation: 27
  • View blog
  • Posts: 146
  • Joined: 12-August 13

Re: Default Debugging homework assignment/ partially filled arrays

Posted 22 April 2014 - 10:48 PM

I notice that the input file has 19 numbers in it but you have an array of 15.

Also i added a print statement
if  (index < arrayLength)
		        {
		        	arrayVals[index] = input.nextDouble();
		        	 System.out.println(index+" "+arrayVals[index]);
		        	
		            index++;
		          }



and shows..
0 88.0
1 80.0
2 66.0
3 99.0
4 22.0
5 88.0
6 54.0
7 100.0
8 77.0
9 29.0
10 100.0
11 98.0
12 66.0
13 11.0
14 73.0

so i dont think you are reading you whole input file thus never getting to the end thus hasNext is never false?
Was This Post Helpful? 1
  • +
  • -

#4 Banapple  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 22-March 14

Re: Default Debugging homework assignment/ partially filled arrays

Posted 22 April 2014 - 11:16 PM

View PostDimitriV, on 22 April 2014 - 09:42 PM, said:

If you have no idea where it's infinite looping or hanging, maybe you should put:
System.out.println(arrayVals[index]);


Right after you read the value into the array. Another thing (I'm not too sure about this though) is that the Scanner could be hanging on the newline characters in the file, so you could also try:
input.nextLine();




I tried what you said and it successfully printed the 15 values from the array onto the console. Someone else told me to clear the scanner buffer too and that might be what causing the problem, but if I put in input.nextLine() wouldn't I end up skipping values in my input file that I need to process?
Was This Post Helpful? 0
  • +
  • -

#5 Banapple  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 22-March 14

Re: Default Debugging homework assignment/ partially filled arrays

Posted 22 April 2014 - 11:22 PM

View PostIvGotAPocket, on 22 April 2014 - 10:48 PM, said:

I notice that the input file has 19 numbers in it but you have an array of 15.

Also i added a print statement
if  (index < arrayLength)
		        {
		        	arrayVals[index] = input.nextDouble();
		        	 System.out.println(index+" "+arrayVals[index]);
		        	
		            index++;
		          }



and shows..
0 88.0
1 80.0
2 66.0
3 99.0
4 22.0
5 88.0
6 54.0
7 100.0
8 77.0
9 29.0
10 100.0
11 98.0
12 66.0
13 11.0
14 73.0

so i dont think you are reading you whole input file thus never getting to the end thus hasNext is never false?


Yeah sorry if that was confusing I didnt make it really clear why in my op, My assignment details are that I'm supposed to fill the array until full but keep a counter of the total number of items in the input file including the ones that didnt fit into the array, and we have to use a constant for the array length.

I think I should be reading the whole input file through using .hasNext(), the putting values into the array though I have in that separate if statement, so that way i can fill the array with only 15 values while keeping track of the total number of values to process from the input file.
Was This Post Helpful? 0
  • +
  • -

#6 IvGotAPocket  Icon User is offline

  • D.I.C Head

Reputation: 27
  • View blog
  • Posts: 146
  • Joined: 12-August 13

Re: Default Debugging homework assignment/ partially filled arrays

Posted 22 April 2014 - 11:24 PM

Hey the problem is that you are not reading till the end of the file and thus
while(index.hasNext)
is never becoming false. I think you are getting something weird happening cause your array is full then you try to call input.nextDouble and put that in their but your array is already full.

Just go and set your array size to say 100 and see what i mean.
 final int arrayLength = 150; 

This post has been edited by IvGotAPocket: 22 April 2014 - 11:26 PM

Was This Post Helpful? 1
  • +
  • -

#7 Banapple  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 22-March 14

Re: Default Debugging homework assignment/ partially filled arrays

Posted 22 April 2014 - 11:30 PM

View PostIvGotAPocket, on 22 April 2014 - 11:24 PM, said:

Hey the problem is that you are not reading till the end of the file and thus
while(index.hasNext)
is never becoming false. I think you are getting something weird happening cause your array is full then you try to call input.nextDouble and put that in their but your array is already full.

Just go and set your array size to say 100 and see what i mean.
 final int arrayLength = 150; 


Ah that is what was happening your right, I couldnt saee it before. Thank you man its always something like this that stumps me. I got it to work by putting in a trash variable that holds the input in an else clause like this

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



Was This Post Helpful? 0
  • +
  • -

Page 1 of 1