/ / जावा- ArrayIndexOutOfBoundsException त्रुटि - जावा, सरणियाँ

जावा- ArrayIndexOutOfBoundsException त्रुटि - जावा, सरणियाँ

जब मैं निम्नलिखित कार्यक्रम चलाता हूं तो मुझे लाइन 20 पर एक त्रुटि मिलती है, और यह मेरा कोड है:

package J1;
import java.util.Scanner;

public class SpeedLimit {

public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
int input = keyboard.nextInt();

String[] tab = new String[2];
String output="";
int speed = 0;
while(input!=-1){
int last =0;
for (int i=0; i<input ; i++){

String pair = keyboard.next();

tab = pair.split(" ");
speed = speed + Integer.parseInt(tab[0])*(Integer.parseInt(tab[1])-last);
last = Integer.parseInt(tab[1]);
}
output = output +speed + "miles" + "n";
speed =0;
input = Integer.parseInt(keyboard.nextLine());
}
System.out.println(output);
}

}

जब मैं कोड चलाता हूं, तो मैं कीबोर्ड से निम्नलिखित इनपुट दर्ज करता हूं:

 3
20 2
30 6
10 7
2
60 1
30 5
4
15 1
25 2
30 3
10 5
-1

इस परिणाम को आउटपुट के रूप में प्राप्त करने के लिए: 170 मील 180 मील 90 मील

लेकिन जब मैं कोड चलाता हूं तो मुझे निम्न त्रुटि मिलती है

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
at J1.SpeedLimit.main(SpeedLimit.java:20)

उत्तर:

जवाब के लिए 2 № 1

String pair = keyboard.next(); यह केवल एक टोकन को पढ़ता है जो अलग हो जाते हैं " " इसलिए जब आप अलग हो जाते हैं pair द्वारा " "। इसमें केवल एक तत्व होगा, द स्ट्रिंग। तो आपको पूरी लाइन पढ़ने की जरूरत है और फिर इसे सीमांकित करके विभाजित करें " ".

एक और गलती यह है कि जब आप उस लाइन को बदलते हैं String pair = keyboard.nextLine(); , फिर भी आपको त्रुटि मिलेगी क्योंकि सिस्टम मानता है Enter इनपुट के रूप में कुंजी .nextLine() तरीका। तो आपको उस अतिरिक्त अनावश्यक इनपुट को त्यागने की आवश्यकता है।

while(input!=-1){
int last =0;
for (int i=0; i<input ; i++){

int ip1=keyboard.nextInt();
int ip2=keyboard.nextInt();

speed = speed + ip1*(ip2-last);
last = ip2;
}
output = output +speed + "miles" + "n";
speed =0;
input = keyboard.nextInt();
}

जवाब के लिए 0 № 2

आप चर जोड़ी को गलत तरीके से पढ़ रहे हैं और फिर आप इसे विभाजित करते हैं और इसे टैब पर असाइन करते हैं जो स्वचालित रूप से अनुक्रमणिका को लाने में विफल रहता है क्योंकि जोड़ी चर को एक समस्या मिली है।

* अगली लाइन (): खाली होने पर भी वर्तमान रेखा के शेष भाग को पढ़ता है।

        keyboard.nextLine(); //To avoid the exception you commented
String pair = keyboard.nextLine(); //here is solved
tab = pair.split(" ");

जवाब के लिए 0 № 3

कीबोर्ड।अगला () केवल अंतरिक्ष तक इनपुट पढ़ेगा, इसलिए जोड़ी और सरणी में केवल एक ही नंबर होगा, इसलिए टैब [1] में arrayOutOfBound अपवाद होता है। अंतरिक्ष के साथ आदानों को पढ़ने के लिए अगली विधि () का उपयोग करें।


जवाब के लिए 0 № 4

आप अपने कोड में बदलाव के लिए कोशिश कर सकते हैं:

public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
int input = Integer.parseInt(keyboard.nextLine());

String[] tab = new String[2];
String output="";
int speed = 0;
while(input!=-1){
int last =0;
for (int i=0; i<input ; i++){
String pair = keyboard.nextLine();
tab = pair.split(" ");
speed = speed + Integer.parseInt(tab[0].trim())*(Integer.parseInt(tab[1].trim())-last);
last = Integer.parseInt(tab[1]);
}
output = output +speed + " miles " + "n";
speed =0;
input = Integer.parseInt(keyboard.nextLine());
}
System.out.println(output);
}

जवाब के लिए 0 № 5

मैंने "वास्तव में समझा कि आप कैसे प्रदान कर रहे हैंइनपुट्स लेकिन, अगर "3" आपकी पहली पंक्ति होती है, तो विभाजित ("") लंबाई की एक सरणी 1. लौटेगी। इस प्रकार, टैब [0] 3 वापस आ जाएगा और टैब [1] आपको एक nullPointerException देगा।

अपनी लाइन 20 निष्पादित करने से पहले टैब की लंबाई के लिए एक चेक जोड़ने का प्रयास करें।

यह काम कर जाना चाहिए:

if(tab.length() > 1){

speed = speed + Integer.parseInt(tab[0])*(Integer.parseInt(tab[1])-last);

}