0

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?

asked Nov 18, 2014 at 14:23
1
  • Open up JavaApplication33.java in a text editor and go to line 31. That's where your problem is. Read the stack trace. javaapplication33.JavaApplication33.main(JavaApplication33.java:31) Commented Nov 18, 2014 at 14:26

1 Answer 1

1

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());
answered Nov 18, 2014 at 14:25
Sign up to request clarification or add additional context in comments.

1 Comment

It is not that case that I would prefer this String.format("%s %s %d%n", super.toString(), " z-coordinate= ", getZ());, but I guess it is the "cleaner" version (without String concatenation).

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.