Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 586
  • Last Modified:

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
0
georges_nassar
Asked:
georges_nassar
  • 13
  • 11
  • 7
1 Solution
 
NaeemgCommented:
correct this.
 if (index >= 0 && <= list.size)
    {
    list.removeElementAt(index);
    }
    else{
           system.out.println("index out of range or array empty");
      }
0
 
georges_nassarAuthor Commented:
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
0
 
NaeemgCommented:
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) );

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
georges_nassarAuthor Commented:
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
0
 
NaeemgCommented:
whats ur error report. plz post here,
line number etc.
0
 
georges_nassarAuthor Commented:
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
0
 
NaeemgCommented:
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));

.
.
.
.
.
}
0
 
georges_nassarAuthor Commented:
not working at all
0
 
NaeemgCommented:
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) );

  }
}
0
 
georges_nassarAuthor Commented:
when i did
run java application i get thtis  message:

Exception in thread "main" java.lang.NoSuchMethodError: main
Press any key to continue . . .
0
 
NaeemgCommented:
set ur main methods arguments as.
public static void main(String[] args) {
0
 
NaeemgCommented:
actually i m not at the computer with JDK,
0
 
NaeemgCommented:
did it work?
0
 
georges_nassarAuthor Commented:
nop
0
 
NaeemgCommented:
It should work , what r new error of compilations?
0
 
CEHJCommented:
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;
}
0
 
georges_nassarAuthor Commented:
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
0
 
CEHJCommented:
That's probably because you're not including it in a class. It must be included
0
 
CEHJCommented:
>>System.out.println("index out of range or array empty");

That line can't be put there after return
0
 
georges_nassarAuthor Commented:
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
0
 
CEHJCommented:
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;
  }



}
0
 
NaeemgCommented:
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) );

  }
}
0
 
georges_nassarAuthor Commented:
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
0
 
CEHJCommented:
Try the following:


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;
  }



}
0
 
NaeemgCommented:
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".
0
 
CEHJCommented:
>> 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
0
 
NaeemgCommented:
check out the first comment of author.
0
 
CEHJCommented:
>>
 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.
>>
0
 
georges_nassarAuthor Commented:
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));
    }
}
0
 
georges_nassarAuthor Commented:
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 . . .
0
 
NaeemgCommented:
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.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 13
  • 11
  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now