i have this json String in json file
{"Q":[0,1,2,3],"q_0":0,"F":[3],"delta":[[0,1],[0,2],[3,2],[0,1]],"segma":[0,1]}
and i want to read the segma array and store it as arraylist of Characters
static ArrayList<Long> Q;
static ArrayList<Character> segma;
static ArrayList<Long> F;
static Long q_0;
static Long deadStat;
static ArrayList<ArrayList<Integer>> delta;
the parseDFAObject function
private static void parseDFAObject(JSONObject dfa) {
//Get employee object within list
Automat.Q = (ArrayList<Long>) dfa.get("Q");
System.out.println(Q);
//Get employee first name
Automat.q_0 = (Long) dfa.get("q_0");
System.out.println(q_0);
Automat.F = (ArrayList<Long>) dfa.get("F");
System.out.println(F);
Automat.delta = (ArrayList) dfa.get("delta");
System.out.println(delta);
JSONArray ja;
ja = (JSONArray)dfa.get("segma");
Character[] ch = (Character[]) ja.toArray();
for(int s=0;s<ch.length;s++){
Automat.segma.add(s,ch[s]);}
System.out.println(segma);
}
the checkAccepte function
public static boolean checkAccepte(String s) {
int qs;
qs = q_0.intValue();
int qalpha;
for (int i = 0; i <= s.length(); i++) {
Character c = (char) s.charAt(i);
qalpha = segma.indexOf(c);
qs =Q.indexOf(delta.get(qs).get(qalpha));
}
return false;
}
the input is 0110
the main function
public static void main(String[] args) {
Scanner input =new Scanner(System.in);
//JSON parser object to parse read file
JSONParser jsonParser = new JSONParser();
try (FileReader reader = new FileReader("DFA.json")) {
//Read JSON file
Object obj = jsonParser.parse(reader);
JSONObject DFA1 = (JSONObject) obj;
System.out.println(DFA1);
parseDFAObject(DFA1);
if( checkAccepte(input.next()){
System.out.println("ACCEPTED ^_^");
}
else{
System.out.println("NOT ACCEPTED !!");
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
}
}
i have this Exception
Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.Character;
at automat.Automat.parseDFAObject(Automat.java:150)
at automat.Automat.main(Automat.java:111)
i tried to do this
Automat.segma = (ArrayList<Character>) dfa.get("segma");
instead of this code in parseDFAObject function
JSONArray ja;
ja = (JSONArray)dfa.get("segma");
Character[] ch = (Character[]) ja.toArray();
Automat.segma.add(s,ch[s]);
and i have this Exception when i use java segma.indexOf(c);
in checkAccepte function , and it returns -1
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
at java.util.ArrayList.elementData(ArrayList.java:422)
at java.util.ArrayList.get(ArrayList.java:435)
at automat.Automat.checkAccepte(Automat.java:40)
at automat.Automat.main(Automat.java:113)
``
Here:
"segma":[0,1]
and
ja = (JSONArray)dfa.get("segma");
Character[] ch = (Character[]) ja.toArray();
What exactly makes you think that segma will be an array of Characters? As written right now, it would be an array of numbers . Your Q array has almost the same example data, and that you treat as List<Long>
. Don't expect that another array coming out of the JSON parser has a different type!
If, for some reason you want to treat [0,1]
as array of characters, then you have to transform the incoming List of Long into that representation. The JSON parser itself doesn't do that for you!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.