Link to home
Start Free TrialLog in
Avatar of georges_nassar
georges_nassar

asked on

Arrays

i have to write a method removeAt that takes three parameters: an array
of integers, the length of the array, and an integer,
say,index.the method  deletes the array element indicated
by index.if index is out of range or the array is empty,output a
message.


import java.io.* ;
import java.util.*;

 public class removeAt
{
  static BufferedReader keyboard = new       BufferedReader(new InputStreamReader(System.in));
 
 
  public static void main ( Integer[] list,int listLength,int index)throws IOException
  {
    Vector list = new Vector( 4 );

    list.addElement( new Integer(36) );
    list.addElement( new Integer(87) );
    list.addElement( new Integer(12) );
    list.addElement( new Integer(66) );
   
   
   System.out.print("enter the index that u want to delete its value");
     
   index = Integer.parseInt(keyboard.readLine());
 
   
   
   if (list[i] ==index)
    {
    list.removeElementAt(index);
    }
    else{
             system.out.println("index out of range or array empty");
       }


    for ( int i=0; i < listlength(); i++ )
      System.out.println( i + ": " +
          list.elementAt(i) );

  }
}

it is not working with me
i need a correction for the method
Avatar of Naeemg
Naeemg

correct this.
 if (index >= 0 && <= list.size)
    {
    list.removeElementAt(index);
    }
    else{
           system.out.println("index out of range or array empty");
      }
Avatar of georges_nassar

ASKER

import java.io.* ;
import java.util.*;

 public class removeAt
{
  static BufferedReader keyboard = new       BufferedReader(new InputStreamReader(System.in));


  public static void main ( Integer[] list,int listLength,int index)throws IOException
  {
    Vector list = new Vector( 4 );

    list.addElement( new Integer(36) );
    list.addElement( new Integer(87) );
    list.addElement( new Integer(12) );
    list.addElement( new Integer(66) );


   System.out.print("enter the index that u want to delete its value");

   index = Integer.parseInt(keyboard.readLine());

   if (index >= 0 && index<= list.size)
       {
       list.removeElementAt(index);
       }
       else{
              system.out.println("index out of range or array empty");
         }





    for ( int i=0; i < listlength(); i++ )
      System.out.println( i + ": " +
          list.elementAt(i) );

  }
}


still not working
sorry forgot to add parenthesis with size,

   if (index >= 0 && index<= list.size())
       {
       list.removeElementAt(index);
       }
       else{
              system.out.println("index out of range or array empty");
         }

// and also correct this one

for ( int i=0; i < list.size(); i++ )
      System.out.println( i + ": " +
          list.elementAt(i) );

import java.io.* ;
import java.util.*;

 public class removeAt
{
  static BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));


  public static void main ( Integer[] list,int listLength,int index)throws IOException
  {
    Vector list = new Vector( 4 );

    list.addElement( new Integer(36) );
    list.addElement( new Integer(87) );
    list.addElement( new Integer(12) );
    list.addElement( new Integer(66) );


   System.out.print("enter the index that u want to delete its value");

   index = Integer.parseInt(keyboard.readLine());

   if (index >= 0 && index<= list.size())
       {
       list.removeElementAt(index);
       }
       else{
              system.out.println("index out of range or array empty");
         }





    for ( int i=0; i < list.size(); i++ )
      System.out.println( i + ": " +
          list.elementAt(i) );

  }
}

can u test it please i still have errors
whats ur error report. plz post here,
line number etc.
C:\Documents and Settings\george\My Documents\removeAt.java:13: list is already defined in main(java.lang.Integer[],int,int)
    Vector list = new Vector( 4 );
           ^
C:\Documents and Settings\george\My Documents\removeAt.java:30: package system does not exist
              system.out.println("index out of range or array empty");
                    ^
2 errors

Tool completed with exit code 1
if your error is at getting input . then try by putting ur BufferedReader in the main method instead of in class .


  public static void main ( Integer[] list,int listLength,int index)throws IOException
  {
BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));

.
.
.
.
.
}
not working at all
ok use the followin complete code,
u made two mistakes, in in the main method arguments u already made object array of list, while in the main u r also make vecotr object as list.

2nd is, ur syntax of System, u r using small 's' as system, thats wrong, at
system.out.println("index out of range or array empty");


use this code

import java.io.* ;
import java.util.*;

 public class removeAt
{
  static BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));


  public static void main (int index)throws IOException
  {
    Vector list = new Vector( 4 );

    list.addElement( new Integer(36) );
    list.addElement( new Integer(87) );
    list.addElement( new Integer(12) );
    list.addElement( new Integer(66) );


   System.out.print("enter the index that u want to delete its value");

   index = Integer.parseInt(keyboard.readLine());

   if (index >= 0 && index<= list.size())
       {
       list.removeElementAt(index);
       }
       else{
              System.out.println("index out of range or array empty");
         }





    for ( int i=0; i < list.size(); i++ )
      System.out.println( i + ": " +
          list.elementAt(i) );

  }
}
when i did
run java application i get thtis  message:

Exception in thread "main" java.lang.NoSuchMethodError: main
Press any key to continue . . .
set ur main methods arguments as.
public static void main(String[] args) {
actually i m not at the computer with JDK,
did it work?
nop
It should work , what r new error of compilations?
Avatar of CEHJ
You need to resize the array, also, you need not pass the length as it's known:


public static Integer[] removeAt(Integer[] list, int index) {
      Integer[] newList = null;
      if (list != null && list.length > 0) {
            newList = new Integer[list.length - 1];
            int newListIndex = 0;
            for (int i = 0;i < list.length;i++) {
                  if (i != index) {
                        newList[newListIndex++] = list[i];
                  }
            }
      }
      return newList;
}
public static int removeAt(Integer[] list, int index) {
     Integer[] newList = null;
     if (list != null && list.length > 0) {
          newList = new Integer[list.length - 1];
          int newListIndex = 0;
          for (int i = 0;i < list.length;i++) {
               if (i != index) {
                    newList[newListIndex++] = list[i];
                    list.removeElementAt(int index);
               }
          }
     }
     return newList;
      System.out.println("index out of range or array empty");
}

it is not working with me
C:\removeAt.java:1: 'class' or 'interface' expected
public static int removeAt(Integer[] list, int index) {
              ^
1 error

Tool completed with exit code 1
That's probably because you're not including it in a class. It must be included
>>System.out.println("index out of range or array empty");

That line can't be put there after return
import java.io.* ;
import java.util.*;

 public class  vectorremoveAt
{
  static BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));



public static int removeAt(Integer[] list, int index) {


     Integer[] newList = null;
     if (list != null && list.length > 0) {
          newList = new Integer[list.length - 1];
          int newListIndex = 0;
          for (int i = 0;i < list.length;i++) {
               if (i != index) {
                    newList[newListIndex++] = list[i];
                    list.removeElementAt(int index);
               }
               System.out.println("index out of range or array empty");
          }
     }
     return newList;

}


still not working
Should be something like:

import java.io.* ;
import java.util.*;

public class  vectorremoveAt {
  static BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));

  public static Integer[] removeAt(Integer[] list, int index) {
       Integer[] newList = null;
       if (list != null && list.length > 0) {
            newList = new Integer[list.length - 1];
            int newListIndex = 0;
            for (int i = 0;i < list.length;i++) {
                 if (i != index) {
                      newList[newListIndex++] = list[i];
                 }
            }
       }
       return newList;
  }



}
if this is ur default class then y r u using

public static int removeAt(Integer[] list, int index) {

instead of

public static void main(String[] args) {
.........................................

i tested the following code here at my computer using jdk1.4, and it works fine.
u should try this one.

import java.io.* ;
import java.util.*;

 public class Temp
{
  static BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));


  public static void main(String[] args) throws IOException
  {
    Vector list = new Vector( 4 );

    list.addElement( new Integer(36) );
    list.addElement( new Integer(87) );
    list.addElement( new Integer(12) );
    list.addElement( new Integer(66) );


   System.out.print("enter the index that u want to delete its value");

   int index = Integer.parseInt(keyboard.readLine());

   if (index >= 0 && index<= list.size())
       {
       list.removeElementAt(index);
       }
       else{
              System.out.println("index out of range or array empty");
         }





    for ( int i=0; i < list.size(); i++ )
      System.out.println( i + ": " +
          list.elementAt(i) );

  }
}
import java.io.* ;
import java.util.*;

 public class VectorremoveAt
{
  static BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));


public static int removeAt(Integer[] list, int index) {

    Vector list = new Vector( 4 );

    list.addElement( new Integer(36) );
    list.addElement( new Integer(87) );
    list.addElement( new Integer(12) );
    list.addElement( new Integer(66) );


   System.out.print("enter the index that u want to delete its value");

   int index = Integer.parseInt(keyboard.readLine());

   if (index >= 0 && index<= list.size())
       {
       list.removeElementAt(index);
       }
       else{
              System.out.println("index out of range or array empty");
         }





    for ( int i=0; i < list.size(); i++ )
      System.out.println( i + ": " +
          list.elementAt(i) );

  }
}


i got these errors
C:\Documents and Settings\george\My Documents\VectorremoveAt.java:11: list is already defined in removeAt(java.lang.Integer[],int)
    Vector list = new Vector( 4 );
           ^
C:\Documents and Settings\george\My Documents\VectorremoveAt.java:21: index is already defined in removeAt(java.lang.Integer[],int)
   int index = Integer.parseInt(keyboard.readLine());
       ^
2 errors

Tool completed with exit code 1
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
did u try the whole class i provide in my above comment? i tested that works, and in ur last comment u r using two different objects with same name "list".
>> did u try the whole class i provide in my above comment?

The problem is that the code does not use the spec that was provided in the question
check out the first comment of author.
>>
 i have to write a method removeAt that takes three parameters: an array
of integers, the length of the array, and an integer,
say,index.the method  deletes the array element indicated
by index.if index is out of range or the array is empty,output a
message.
>>
import java.io.* ;
import java.util.*;

public class  vectorremoveAt {
  static BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));

  public static void main(String[] args) {
       setUpDataThenPromptRemoval();
  }

  static void setUpDataThenPromptRemoval() {
       Integer[] ints = new Integer[4];
       for (int i = 0; i < ints.length;i++) {
            ints[i] = new Integer((int)(Math.random() * 100));
       }
       System.out.println("Current array is:");
       System.out.println(Arrays.asList(ints));


       String input = null;
       System.out.println();
       do {
            try {
                 System.out.print("Which element would you like to remove? (0-3 'q' to quit): ");
                 input = keyboard.readLine();
                 if ("q".equalsIgnoreCase(input) == false) {
                      int index = Integer.parseInt(input);
                      ints = removeAt(ints, index);
                      System.out.println("Current array is:");
                      System.out.println(Arrays.asList(ints));
                 }
            }
            catch(Exception e) {
                 input = "error";
            }
       } while ("error".equals(input));

  }

  static boolean continueToPromptForInput(String input) {
       return "q".equalsIgnoreCase(input) == false || "error".equals(input);
  }

  public static Integer[] removeAt(Integer[] list, int index) {
       Integer[] newList = null;
       if (list != null && list.length > 0) {
            newList = new Integer[list.length - 1];
            int newListIndex = 0;
            for (int i = 0;i < list.length;i++) {
                 if (i != index) {
                      newList[newListIndex++] = list[i];
                 }
            }
       }
       return newList;
  }



}


i got this error

C:\Documents and Settings\george\Desktop\New Folder\vectorremoveAt .java:4: class vectorremoveAt is public, should be declared in a file named vectorremoveAt.java
public class  vectorremoveAt {
       ^
1 error

Tool completed with exit code 1





public class IntClass
{
    private int x;

    public IntClass()
    {
        x = 0;
    }
    public IntClass(int num)
    {
        x = num;
    }
   
    public void setNum(int num)
    {
        x = num;
    }

    public int getNum()
    {
        return x;
    }

    public void addToNum(int num)
    {
        x = x + num;
    }

    public void multiplyToNum(int num)
    {
        x = x * num;
    }

    public int compareTo(int num)
    {
        return (x - num);
    }

    public boolean equals(int num)
    {
        if(x == num)
          return true;
        else
          return false;
    }

    public String toString()
    {
        return (String.valueOf(x));
    }
}
import java.io.* ;
import java.util.*;

public class Ch10_PrExercise2 {
  static BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));

  public static void main(String[] args) {
       setUpDataThenPromptRemoval();
       boolean continueToPromptForInput;
       Integer[] removeAt;

  }

  static void setUpDataThenPromptRemoval() {
       Integer[] ints = new Integer[4];
       for (int i = 0; i < ints.length;i++) {
            ints[i] = new Integer((int)(Math.random() * 100));
       }
       System.out.println("Current array is:");
       System.out.println(Arrays.asList(ints));


       String input = null;
       System.out.println();
       do {
            try {
                 System.out.print("Which element would you like to remove? (0-3 'q' to quit): ");
                 input = keyboard.readLine();
                 if ("q".equalsIgnoreCase(input) == false) {
                      int index = Integer.parseInt(input);
                      ints = removeAt(ints, index);
                      System.out.println("Current array is:");
                      System.out.println(Arrays.asList(ints));
                 }
            }
            catch(Exception e) {
                 input = "error";
            }
       } while ("error".equals(input));

  }

  static boolean continueToPromptForInput(String input) {
       return "q".equalsIgnoreCase(input) == false || "error".equals(input);
  }

  public static Integer[] removeAt(Integer[] list, int index) {
       Integer[] newList = null;
       if (list != null && list.length > 0) {
            newList = new Integer[list.length - 1];
            int newListIndex = 0;
            for (int i = 0;i < list.length;i++) {
                 if (i != index) {
                      newList[newListIndex++] = list[i];
                 }
            }
       }
       return newList;
  }



}


it works

results:
Current array is:
[8, 90, 17, 84]

Which element would you like to remove? (0-3 'q' to quit): 2
Current array is:
[8, 90, 84]
Press any key to continue . . .
Mr. georges_nassar, wasnot i deserve points? as answer posted to by me,  while i've tested that code on my machine, and it works. and in ur first comment, u were showing that u r making class with main method to get arguments.
this is really unfair.