मूल रूप से मैं जो करने की कोशिश कर रहा हूं वह एक फाइल है जिसमें कुछ प्रश्न हैं। फ़ाइल इस तरह व्यवस्थित है:
//The category of the question
Questions
//The question
Possible Answer
//Answer #1
//Answer#2
//Answer#3
//Answer#4
Right answer
//The right answer
Image
//The image path if it exists
यह केवल एक प्रश्न के लिए है। इस तरह के कई सवाल हैं।
private boolean readQuestion() {
try {
String line = this.reader.readLine();
if (line == null) {
return false;
}
String category = "";
String question = "";
String[] possibleAnswers = {
"",
"",
"",
""
};
String rightAnswer = "";
String image = "";
boolean imageQuestion = false;
if (line.compareTo("Category") == 0) {} else {
//read the category
category = this.reader.readLine();
}
if (line.compareTo("Question") == 0) {
//read the question (String question = ....)
question = this.reader.readLine();
}
line = this.reader.readLine();
if (line.compareTo("Possible answers") == 0) {
//read four lines with the possible answers(String [] possibleAnswers = ...)
for (int i = 0; i < 4; i++) {
possibleAnswers[i] = this.reader.readLine();
}
}
line = this.reader.readLine();
if (line.compareTo("Right answer") == 0) {
//read the right answer(String rightAnswer = ...)
rightAnswer = this.reader.readLine();
}
line = this.reader.readLine();
if (line.compareTo("Image") == 0) {
//read the image name
image = this.reader.readLine();
imageQuestion = true;
}
BaseQuestion question1 = new BaseQuestion(question, possibleAnswers, rightAnswer, category);
if (imageQuestion) {
ImageQuestion imageQuestion_1 = new ImageQuestion(question1, image);
this.questions.add(imageQuestion_1);
} else {
this.questions.add(question1);
}
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
private void readFile() {
while (this.readQuestion());
}
मैंने readQuestion बूलियन बना दिया है ताकि यदि कोई अन्य प्रश्न हो तो वह वापस आ जाए। पाठक भी एक BufferedReader वस्तु है। बेसप्रश्न एक वर्ग है जिसमें एक श्रेणी, एक प्रश्न, 4 संभावित उत्तर और एक सही उत्तर होता है। ImageQuestion में BaseQuestion की सभी विशेषताएँ हैं, लेकिन एक इमेजपाथ भी है।
1 उत्तर
ऐसा इसलिए है क्योंकि आपके पास यदि stmt पर NullPointerException (आपके लूप के तीसरे पुनरावृत्ति के दौरान) है। क्योंकि रेखा = शून्य
line = this.reader.readLine();
if (line.compareTo("Image") == 0)
java.lang.NullPointerException: "String.compareTo(String)" का आह्वान नहीं कर सकता क्योंकि "लाइन" शून्य है
public static void main(String[] args) throws IOException {
Demo d = new Demo();
d.readFile();
System.out.println(d.questions.size());
for (Question q : d.questions) {
System.out.println(q.toString());
जहां प्रश्न सिर्फ एक डमी इंटरफ़ेस है
interface Question {}
class BaseQuestion implements Question { ... }
यह वह आउटपुट है जो मैं आपकी कक्षा से प्राप्त करने में सक्षम हूं:
२ बेस प्रश्न {प्रश्न = '', सही उत्तर = '', श्रेणी = '', संभव उत्तर = [शून्य, शून्य, शून्य, शून्य]} बेस प्रश्न {प्रश्न = '', सही उत्तर = '', श्रेणी = '// उत्तर #1 ', संभव उत्तर = [अशक्त, अशक्त, अशक्त, अशक्त]}
ऊपर प्रस्तुत आउटपुट देखने के लिए इन विधियों को अपने बेस प्रश्न और छवि प्रश्न वर्ग में जोड़ें:
@Override
public String toString() {
return "ImageQuestion{" +
"image='" + image + '\'' +
", question1=" + question1 +
'}';
}
तथा
@Override
public String toString() {
return "BaseQuestion{" +
"question='" + question + '\'' +
", rightAnswer='" + rightAnswer + '\'' +
", category='" + category + '\'' +
", possibleAnswers=" + Arrays.toString(possibleAnswers) +
'}';
}
आप यहाँ एक ही पंक्ति को दो बार पढ़ रहे हैं:
if (line.compareTo("Category") == 0) {} else {
//read the category
category = this.reader.readLine();
}
if (line.compareTo("Question") == 0) {
//read the question (String question = ....)
question = this.reader.readLine();
}
श्रेणी और प्रश्नों के बीच एक अनुपलब्ध रीडलाइन () है, इसलिए वर्तमान लाइन आपके वांछित इनपुट से मेल नहीं खाती है
if (line.compareTo("Category") == 0) {} else {
//read the category
category = this.reader.readLine();
}
line = this.reader.readLine();
if (line.compareTo("Question") == 0) {
//read the question (String question = ....)
question = this.reader.readLine();
}
... और कोड के इस टुकड़े का उद्देश्य क्या है? क्या आप सुनिश्चित हैं कि यह अपेक्षा के अनुरूप काम करता है?
if (line.compareTo("Category") == 0) {} else {
//read the category
category = this.reader.readLine();
}
संबंधित सवाल
नए सवाल
java
जावा एक उच्च स्तरीय प्रोग्रामिंग भाषा है। इस टैग का उपयोग तब करें जब आपको भाषा का उपयोग करने या समझने में समस्या हो। इस टैग का उपयोग शायद ही कभी किया जाता है और इसका उपयोग अक्सर [वसंत], [वसंत-बूट], [जकार्ता-ई], [Android], [javafx], [हडूप], [श्रेणी] और [मावेन] के साथ किया जाता है।