CuriousAD
CuriousAD

Reputation: 13

ArrayList Runtime error

I've been writing a code to enhance an arraylist code so it'd display a main menu to the user to choose from the possible operations, he can do as follows: 1. Add 2. Delete 3. Display Info 4. Move horizontally 5. Move vertically 6. Compute distance and I keep getting a compiler error.

my super class

package javaapplication33;
import java.util.*;
public class MyPoint {
private int x;
private int y;
public MyPoint(){
    setPoint(0,0);
}
public MyPoint(int xCoord, int yCoord){
    setPoint(xCoord, yCoord);
}
public void setPoint(int xCoord, int yCoord){
    x=xCoord;
    y=yCoord; 
}
public void setX( int xCoord){
    x=xCoord;
}
public void setY( int yCoord){
y= yCoord; 
}
public int getX(){
return x;
}
public int getY(){
return y; }
public void HMove(int val)
{
    x=x+val;
}
public void VMove(int val)
{
    y=y+val;
}
public double ComputeDistance( MyPoint P)
{
double powX , powY;
powX= Math.pow( (this.x - P.getX() ) , 2);
powY= Math.pow( (this.y - P.getY() ) , 2);
return Math.sqrt( powX + powY);
}
public String toString()
{
return String.format( "\n x-coordinate = %d, y-coordinate = %d ", x , y );}}

my sub class

package javaapplication33;

import java.util.*;
public class MyPoint3D extends MyPoint {
private int z;
public MyPoint3D(){
setPoint(0,0,0);}
public MyPoint3D( int xCoord, int yCoord, int zCoord )
{super(xCoord, yCoord);
setZ(zCoord);}
public void setZ( int zCoord){
z= zCoord; }
public int getZ(){
return z;}
public void setPoint( int xCoord, int yCoord , int zCoord){
super.setPoint( xCoord, yCoord);
setZ(zCoord);}
public double ComputeDistance( MyPoint3D P){
double powX , powY , powZ;
powX= Math.pow( (getX() - P.getX() ) , 2);
powY= Math.pow( (getY() - P.getY() ) , 2);
powZ= Math.pow( (getZ() - P.getZ() ) , 2);
return Math.sqrt( powX + powY + powZ);}
public String toString()
{
return String.format( "%s %s %d ", super.toString(), " z-coordinate= ", getZ() + "\n" );}}

My main class

package javaapplication33;



import java.util.*;

public class JavaApplication33 {

public static void main(String[] args) {
    Scanner input = new Scanner( System.in );
    ArrayList<MyPoint3D>MyList = new ArrayList<MyPoint3D>();
    int choice = getMenuChoice();
while ( choice != 7 ) 
{ switch ( choice )
{ case 1: System.out.println( "Enter the object's data (x,y,z) to added in the list:" );
int x=input.nextInt();
int y= input.nextInt();
int z=input.nextInt();
MyList.add(new MyPoint3D (x,y,z));
System.out.println(MyList);
break;
case 2: System.out.println( "Enter the index of the object to be deleted, Reminder: the index begin with zero");
int ind=input.nextInt();
MyList.remove(ind);
System.out.println(MyList);
break;

case 3: System.out.println( "The data in MyList are:");
System.out.println(MyList);
break;
case 4: System.out.println("Enter the index of the point you want to move horizontally:");
int ind1=input.nextInt();
System.out.println("Enter the units it is supposed to move in the horizontal direction:");
int val= input.nextInt();
MyList.get(ind1).HMove(val);
System.out.println(MyList);
break;
case 5:System.out.println("Enter the index of the point you want to move vertically:");
int ind2=input.nextInt();
System.out.println("Enter the units it is supposed to move in the vertical direction:");
int val2= input.nextInt();
MyList.get(ind2).VMove(val2);
System.out.println(MyList);
break;
case 6:System.out.println("Enter the indexes of the two points to display the distance between them:");
int ind3=input.nextInt();
int ind4= input.nextInt();
System.out.println("The distance between 2 points are:"+MyList.get(ind3).ComputeDistance(MyList.get(ind4)));
break; } 
choice=getMenuChoice();}}
public static int getMenuChoice() 
{ Scanner input = new Scanner( System.in );
System.out.println( "1. Add " );
System.out.println( "2. Delete" );System.out.println( "3. Display Info" );
System.out.println( "4. Move Horizontally" );
System.out.println( "5. Move Vertically" );
System.out.println( "6. Compute Distance" );
System.out.println( "7. Exit" );
System.out.println( " Enter Your Choice" );
return input.nextInt(); } }

but I keep gettin a runtime error that says

Exception in thread "main" java.util.IllegalFormatConversionException: d != java.lang.String
at java.util.Formatter$FormatSpecifier.failConversion(Formatter.java:4045)
at java.util.Formatter$FormatSpecifier.printInteger(Formatter.java:2748)
at java.util.Formatter$FormatSpecifier.print(Formatter.java:2702)
at java.util.Formatter.format(Formatter.java:2488)
at java.util.Formatter.format(Formatter.java:2423)
at java.lang.String.format(String.java:2797)
at javaapplication33.MyPoint3D.toString(MyPoint3D.java:44)
at java.lang.String.valueOf(String.java:2854)
at java.lang.StringBuilder.append(StringBuilder.java:128)
at java.util.AbstractCollection.toString(AbstractCollection.java:458)
at java.lang.String.valueOf(String.java:2854)
at java.io.PrintStream.println(PrintStream.java:821)
at javaapplication33.JavaApplication33.main(JavaApplication33.java:31)
Java Result: 1

Thanks to Reimeus answer my problem is solved, but now I get a new issue when I choose any of the options (delete, move, or compute distance)

Which displays this

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 5, Size: 1
at java.util.ArrayList.rangeCheck(ArrayList.java:635)
at java.util.ArrayList.get(ArrayList.java:411)
at javaapplication33.JavaApplication33.main(JavaApplication33.java:46)
Java Result: 1

Any thoughts?

Upvotes: 0

Views: 1123

Answers (1)

Reimeus
Reimeus

Reputation: 159754

Its a runtime error rather than a compile error. The format specifier types need to match the arguments

return String.format("%s %s %d%n", super.toString(), " z-coordinate= ", getZ());

Upvotes: 1

Related Questions