?
Solved

Arrays

Posted on 2005-03-26
31
Medium Priority
?
575 Views
Last Modified: 2013-12-14
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
Comment
Question by:georges_nassar
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 13
  • 11
  • 7
31 Comments
 
LVL 5

Expert Comment

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

Author Comment

by:georges_nassar
ID: 13636676
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
 
LVL 5

Expert Comment

by:Naeemg
ID: 13636693
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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 

Author Comment

by:georges_nassar
ID: 13636732
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
 
LVL 5

Expert Comment

by:Naeemg
ID: 13636741
whats ur error report. plz post here,
line number etc.
0
 

Author Comment

by:georges_nassar
ID: 13636748
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
 
LVL 5

Expert Comment

by:Naeemg
ID: 13636752
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
 

Author Comment

by:georges_nassar
ID: 13636763
not working at all
0
 
LVL 5

Expert Comment

by:Naeemg
ID: 13636771
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
 

Author Comment

by:georges_nassar
ID: 13636781
when i did
run java application i get thtis  message:

Exception in thread "main" java.lang.NoSuchMethodError: main
Press any key to continue . . .
0
 
LVL 5

Expert Comment

by:Naeemg
ID: 13636802
set ur main methods arguments as.
public static void main(String[] args) {
0
 
LVL 5

Expert Comment

by:Naeemg
ID: 13636807
actually i m not at the computer with JDK,
0
 
LVL 5

Expert Comment

by:Naeemg
ID: 13636839
did it work?
0
 

Author Comment

by:georges_nassar
ID: 13636845
nop
0
 
LVL 5

Expert Comment

by:Naeemg
ID: 13636860
It should work , what r new error of compilations?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13637024
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
 

Author Comment

by:georges_nassar
ID: 13640545
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 13640574
That's probably because you're not including it in a class. It must be included
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13640580
>>System.out.println("index out of range or array empty");

That line can't be put there after return
0
 

Author Comment

by:georges_nassar
ID: 13640600
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 13640628
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
 
LVL 5

Expert Comment

by:Naeemg
ID: 13641868
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
 

Author Comment

by:georges_nassar
ID: 13644049
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
 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
ID: 13651615
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
 
LVL 5

Expert Comment

by:Naeemg
ID: 13651651
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 13651683
>> 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
 
LVL 5

Expert Comment

by:Naeemg
ID: 13651701
check out the first comment of author.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13651727
>>
 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
 

Author Comment

by:georges_nassar
ID: 13701761
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
 

Author Comment

by:georges_nassar
ID: 13702271
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
 
LVL 5

Expert Comment

by:Naeemg
ID: 13704249
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org Go to that link and select download selenium in the right hand column That will then direct you to their download page. From that p…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month14 days, 18 hours left to enroll

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question