ArrayList 런타임 오류

CuriousAD

가능한 작업 중에서 선택할 수있는 주 메뉴를 사용자에게 표시하도록 arraylist 코드를 향상시키는 코드를 작성했습니다. 다음과 같이 할 수 있습니다. 1. 추가 2. 삭제 3. 정보 표시 4. 수평으로 이동 5. 수직으로 이동 6. 거리를 계산하고 컴파일러 오류가 계속 발생합니다.

내 슈퍼 클래스

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

내 하위 수업

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

내 메인 클래스

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

하지만 계속 런타임 오류가 발생합니다.

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

Reimeus의 답변 덕분에 문제가 해결되었지만 이제 옵션 (삭제, 이동 또는 거리 계산)을 선택하면 새로운 문제가 발생합니다.

이 표시

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

이견있는 사람?

Reimeus

컴파일 오류가 아닌 런타임 오류입니다. 형식 지정자 유형은 인수와 일치해야합니다.

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

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사