Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Array Problem

Options
  • 25-03-2004 3:07pm
    #1
    Registered Users Posts: 488 ✭✭


    Anyone tell me how to reset an array

    i set the array to 100..but the if the user enters only fills ten elements...how can i print these without getting an error like


    Console: IO Exception in readInt();



    Please


Comments

  • Registered Users Posts: 4,666 ✭✭✭Imposter


    What?
    The middle line above makes no sense.
    Also if you're getting an exception it doesn't neccesarily mean that you need to reset the array. What exactly are you trying to do? Possibly post some code and the exception you get.


  • Registered Users Posts: 488 ✭✭lad12


    Console: IO Exception in readInt();

    is the exception i get....


    i use a

    Test.get();
    while(!Console.EndOfFile())
    {
    Test.get();
    }
    Test.sort();
    Test.print();


    and i have set Test to 100 in

    Tester test = new Tester(100)

    where Tester is a class that sets test up as an array

    when i want the program to finish by exiting the while loop i want it to then sort and print but i just get an error of

    Console: IO Exception in readInt();


  • Registered Users Posts: 4,666 ✭✭✭Imposter


    Try this:

    Test.get();
    try{
    while(!Console.EndOfFile())
    {
    Test.get();
    }
    }catch(IOException ioe){}
    Test.sort();
    Test.print();

    If this is the same assignment as yesterday it was more or less done for you then!


  • Registered Users Posts: 6,306 ✭✭✭OfflerCrocGod


    Or dont cause that will print out ten numbers and cause 90 exceptions!!!. lad12 use a int n=0; and increase n++ everytime you get(); something from the user, then let .print(); take in that n and do a loop 0 to n printing out the nubers the person inputed and then ingnoring the empty array spaces.


  • Registered Users Posts: 488 ✭✭lad12


    Yeah ive tried that

    like in class Tester i declare
    private int n = 0;

    then in my get method i place n++, and ive reset the 2 for loops to
    for(int i = 0; i < n; i++)
    test.print(i);

    but i still get Console: IO Exception in readInt();

    and Excetion in thread "main" java.lang.nullPointerException


  • Advertisement
  • Registered Users Posts: 4,666 ✭✭✭Imposter


    Originally posted by OfflerCrocGod
    Or dont cause that will print out ten numbers and cause 90 exceptions!!!.
    True.

    But the next question would be fun then!


  • Registered Users Posts: 1,636 ✭✭✭henbane


    Originally posted by lad12
    Test.get();
    while(!Console.EndOfFile())
    {
    Test.get();
    }
    Test.sort();
    Test.print();
    This is almost certainly a Joe Morris course. Read the next set of notes. If I remember correctly, he advised to increment a counter every time you add to the array and then use that to make sure you don't throw exceptions cos you won't read any index above the size of the counter.

    You really should just read his notes - they're rather good.


  • Registered Users Posts: 488 ✭✭lad12


    eh hem..yes


  • Registered Users Posts: 488 ✭✭lad12


    Thanks for the advise henbane....got it working perfectly..cheers


  • Registered Users Posts: 6,306 ✭✭✭OfflerCrocGod


    Use the n++ then print out the value for n, after you have inputed 10 numbers. It should say 10, if not your problem is there, is if does then your prob is elsewere. Actually print n every time you get a number in just to make sure you are getting the n part right. wait....what does sort() do?.

    <edit>Whatever:rolleyes:.


  • Advertisement
  • Registered Users Posts: 19,396 ✭✭✭✭Karoma


    why not let the user specify how many elements that they want to put in?


    quick'n'durtay:
    import java.util.*;
    import java.io.*;
    
    public class Test
    {
    
      
    
      public static void main(String[] args)
      {
    
        int arrLength = 0; // the length of the array.set by user below..
        int i; //counter
        
        // reading in
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    
    
    
        try 
        { //get user to set array length (from keyboard)
    
            
            String str = "";
     	i = 0;
    
            while ( i != 1) 
            {
                System.out.print("> How many elements in the array? ");
                str = in.readLine(); //read
                System.out.println("you input: " +str); //for test..
    
                arrLength = Integer.parseInt( str); //change to int
    
    	    i++; //get out of loop
                
            }
    
    
        } 
        catch (IOException e) 
        {
           //err 
        }
    
    
    
    
    
        //create the array. size = what the user said.
        int[] testArr = new int[ arrLength ];
    
    
        try 
        {
    
    	int inpt;
    	i = 0; //reset our counter
    
            while ( i != arrLength ) 
            { // get input from user (to put in the array)
    
                System.out.print("Input element in the array > "); //prompt
                inpt = Integer.parseInt( in.readLine() ); //read in
                System.out.println("you input: " + inpt); // test
     
                testArr[i] = inpt; //put into array
    	    i++; //loop counter inc.
            }
       }
       catch(IOException ioe){ }
    
    
        
        //test: print elements in array
        System.out.println( "The array length is: " + testArr.length); 
        for ( int n =0; n < testArr.length; n++)
        {
          System.out.println("element at " + n + " equals" + testArr[n] );
        }
    
    
      }
    
    
    }
    
    


  • Registered Users Posts: 491 ✭✭Silent Bob


    The real problem is that you're using an array where you really want to be using a vector.

    Vectors are designed for situations where you don't know at compile time how many items need to be stored in them.


  • Registered Users Posts: 19,396 ✭✭✭✭Karoma


    *grumbles*
    these discussions are coming up a lot lately... "change array to vector" "vector to arraylist" ...
    He asked for help with an array so we gave him help with an array; but SB is correct- you'd be better off using either a vector or arraylist...


Advertisement