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

java array problem

Options
  • 27-03-2002 11:56am
    #1
    Registered Users Posts: 11,397 ✭✭✭✭


    import java.io.*;
    
    public class Assignment2
    {
      public static void main (String [] args) throws IOException
      {
       BufferedReader kbd = new BufferedReader (new InputStreamReader(System.in));
       
       int i = -1;
       int number =0;
       int choice =0;
       int num1 =0;
       int [] array;
    
       System.out.println("Please enter a number between 10 and 20");
        number = Integer.parseInt(kbd.readLine());
    
          while (number < 10 || number > 20)
    
           {
            System.out.println("Invalid number entered please enter a number between 10 and 20");
            number = Integer.parseInt(kbd.readLine());
           }
             array = new int [number]; 
            do
             {
           
    
            System.out.println(" 1. Insert a number");
            System.out.println(" 2. Remove a number");
            System.out.println(" 3. Display the array");
            System.out.println(" 4. Exit");
            choice = Integer.parseInt(kbd.readLine());
             if (choice == 1)
             {
             System.out.println("Please insert a number");
             num1 = Integer.parseInt(kbd.readLine());
             i = insert(array, number, i, num1);
             insert(array, number, i, num1);
    
             }
             if (choice == 2)
             {
             System.out.println("What number do you wish to remove?");
             num1 = Integer.parseInt(kbd.readLine());
             
             }
              if (choice == 3)
              {
              dispArray (array, number);
              }
             }
    
              while (choice != 4);
       }
        public static void dispArray(int [ ] array, int number)
      {
    
        for (int i = 0; i < number; i++)
        {
          System.out.println("The value at position " + i + " is " + array[i]);
        }
    
       }
       public static int insert(int [ ] array, int number, int i, int num1)
       {
       if(i < number -1)
       {
         i++;
         array[i] = array[num1];
         System.out.println("Number " + num1+  " at position" +i);
        }
        else
        {
        System.out.println("Array is full");
        }
        return i;
     }  {
    
    
          }
    }
    

    It doesn't save the numbers entered, and i don't have a clue how to do the remove method!

    Any help appriciated :)


Comments

  • Closed Accounts Posts: 1,651 ✭✭✭Enygma


    OK, first off you should really declare array as a global i.e. outside the main method.
    Then don't bother to pass it as an argument in your method calls.

    When you pass an array of ints to a method it creates it's own private copy. This is what was happening here. The insert method created it's own copy of array and inserted the values, when the method finished the array died.

    It's different with objects though, when you pass them to a method a reference is passed so the original one is changed.
    But that's neither here nor there.

    Watch what you're doing with the i variable too, you should really ask the user for a position and a value. Then for the insert method just pass those two values.

    Also there is a set of empty braces at the bottom, you can get rid of those.


  • Registered Users Posts: 11,397 ✭✭✭✭azezil


    i don't understand, i suck at java ;)


  • Closed Accounts Posts: 1,651 ✭✭✭Enygma


    Hmm now I'm confused, ignore what I said above about the method creating a private copy, I've tested that and it doesn't.

    Ahhh, it's your insert method, you're saying:
    array[i] = array[num1];
    

    which is essentially:
    array[i] = 0;
    

    try this:
    array[i] = num1;
    


  • Registered Users Posts: 11,397 ✭✭✭✭azezil


    cheers that sorted it, now how do i do a remove method?


  • Registered Users Posts: 68,317 ✭✭✭✭seamus


    Remove method hmmm......
    Assuming you want to remove from anywhere in the array, you would have to implement a search algorithm based on the size of the array. Normally you would do some fancy programming, but seeing as you have limited yourself to a small array (< 21 objects), a linear search will do just fine, and won't make a huge time difference. Something like:
    public static int remove(int[] array, int a) {
       for(int i = 0; i < array.length; i++) {
            if(array[i] == a) {
               array[i] = 0;
               return i;
            } 
       }
       return -1; 
    }
    

    Dunno if it works, just lashed it out there. Basically it iterates over each number, comparing them, and then replacing the first instance of a that it finds with a zero. You may be able to replace it with null but I'm too lazy to start up JBuilder to find out :p.

    It returns the position of the removed number, or a -1 if it does not find it.

    :)


  • Advertisement
  • Registered Users Posts: 11,397 ✭✭✭✭azezil


    well i check in JBuilder n there's nothing wrong with your code, but i can't initialize it :o (a little help here!)


  • Registered Users Posts: 68,317 ✭✭✭✭seamus


    Can't initialize it? Donno whatcha mean.......

    In you main method:
    .................
    if (choice == 2)
             {
                  System.out.println("What number do you wish to remove?");
                  num1 = Integer.parseInt(kbd.readLine());
                  [b]if ((int i = remove(array, num1)) == -1) {
                     System.out.println("Error: Number not present in array!!"); 
                 } else {
                       System.out.println("The number " + num1 + " was removed from position " + i); }[/b] 
             }
    .................
    

    That should be enough. No?

    :)


  • Registered Users Posts: 11,397 ✭✭✭✭azezil


    oh i got it going since i posted that, although i didn't include the extra functionality you have there, K.I.S.S. ;)


  • Registered Users Posts: 68,317 ✭✭✭✭seamus


    Originally posted by azezil
    oh i got it going since i posted that, although i didn't include the extra functionality you have there, K.I.S.S. ;)

    You must wear a Large Sign on your chest at the boards beer, so I know exactly who to avoid.


  • Registered Users Posts: 11,397 ✭✭✭✭azezil


    Originally posted by seamus


    You must wear a Large Sign on your chest at the boards beer, so I know exactly who to avoid.
    What u don't greet your friends with a passionite kiss, with much thong n roaming hands!!

    Actually i was refering to the Keep It Simple Stupid!!


  • Advertisement
Advertisement