나는 각 색상의 가장 가치있는 보석으로 워크래프트 보석 공예 경매 데이터의 세계를 조직하는 응용 프로그램을 만들고 있습니다. 이렇게하려면 json 데이터베이스를 배열로 파싱하려고 시도하고 있습니다. gson API와 같은 간단한 작업이지만이 항목은 자바 클래스의 프로젝트이므로 내 교수가 언급했습니다. 내가 데이터를 가져 오기 위해 수업에서 배운 것들을 사용해야한다고 말하면서 json 데이터를 파싱하고 화면에 인쇄하는 코드 (여전히 배열로 파싱하는 작업)를 가지고 있다고 말하면서 I've uploaded my data.json to here 코드를 포함하고 있습니다. 지금까지 아래 : ~텍스트 파일에서 데이터 요소 추출하기
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -9 at java.lang.String.substring(String.java:1958) at jcutil.jcFormat.main(jcFormat.java:40) Java Result: 1
내가 처음에 내 코드를 테스트하는 경우 :
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class jcFormat {
public static void main(String[] args) {
try {
File f = new File("c:\\ProgramData\\jcUtil\\data.json");
Scanner sc = new Scanner(f);
List<Auction> ahdata = new ArrayList<Auction>();
sc.nextLine();//eats line
sc.nextLine();//eats line
sc.nextLine();//eats line
while (sc.hasNextLine()) {
String line = sc.nextLine();
String[] details = line.split(",");
//get item as string
String itemz = details[1];
itemz = itemz.substring(7, itemz.length());
//convert itemz string to item int
int item = Integer.parseInt(itemz);
//get buyout as string
String buyoutz = details[4];
buyoutz = buyoutz.substring(9, buyoutz.length());
//convert buyoutz string to buyout int
int buyout = Integer.parseInt(buyoutz);
//get quantity as string
String quantityz = details[5];
quantityz = quantityz.substring(11, quantityz.length());
//convert quantityz string to quantity int
int quantity = Integer.parseInt(quantityz);
Auction a = new Auction(item, buyout, quantity);
ahdata.add(a);
}
for (Auction a : ahdata) {
System.out.println(a.toString());
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
class Auction {
private int item;
private int buyout;
private int quantity;
public Auction(int item, int buyout, int quantity) {
this.item = item;
this.buyout = buyout;
this.quantity = quantity;
}
/**
* @return the item
*/
public int getItem() {
return item;
}
/**
* @param item the item to set
*/
public void setItem(int item) {
this.item = item;
}
/**
* @param buyout the buyout to set
*/
public void setBuyout(int buyout) {
this.buyout = buyout;
}
/**
* @return the buyout
*/
public int getBuyout() {
return buyout;
}
/**
* @return the quantity
*/
public int getQuantity() {
return quantity;
}
/**
* @param quantity the quantity to set
*/
public void setQuantity(int quantity) {
this.quantity = quantity;
}
@Override
public String toString() {
return this.item + "\t" + this.buyout + "\t" + this.quantity;
}
}
나는 현재 실행 해요 문제이 오류입니다 10 줄의 data.json만으로도 문제가 없으며 Java에 대한 새로운 사람으로서 디버깅 기술이 그리 좋지 않으므로 어떤 도움이 필요한지 알아 내려고 노력하고 있습니다. 이 오류가 발생하면 크게 감사하겠습니다. 당신은 이클립스를 사용하는 가정
jcFormat.java의 40 행은 어디에 있습니까? –
JSON 구문 분석기를 사용하지 않는 이유는 무엇입니까? –
라인 40은 quantityz = quantityz.substring (11, quantityz.length())입니다. – Sinatics