运行时错误_字符串索引超出范围异常_打印字符串奇偶索引

huangapple go评论58阅读模式
英文:

Run Time Error_String index out of bound Exception_Printing string odd and even indexes

问题

import java.io.*;
import java.util.*;

public class Solution {

    public static void main(String[] args) {
        
        Scanner sc = new Scanner(System.in);

        int n = sc.nextInt();
        String[] sa = new String[n];
        for(int i=0;i<n;i++){

            sa[i] = sc.nextLine();
        }
        String odd = "";
        String even = "";
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<sa[i].length();j++)
            {
                if(j%2!=0){
                   odd = odd + sa[j].charAt(j);
                }
                else {
                    even = even + sa[j].charAt(j);
                }
            }
            System.out.println(odd + " " + even);
        }
    }
}

ISsue : Getting run time exception while running the code. --> String index out of bound exception
英文:

Code :

import java.io.;
import java.util.
;

public class Solution {

public static void main(String[] args) {
    
    Scanner sc = new Scanner(System.in);

    int n = sc.nextInt();
    String[] sa = new String[n];
    for(int i=0;i&lt;n;i++){

        sa[i] = sc.nextLine();
    }
        String odd=&quot;&quot;;
        String even=&quot;&quot;;
        for(int i=0;i&lt;n;i++)
        {
            for(int j=0;j&lt;sa[i].length();j++)
            {
             if(j%2!=0){
               odd = odd+sa[j].charAt(j);
            }
            else {
                even = even+sa[j].charAt(j);
            }
       
            }
            System.out.println(odd+&quot; &quot;+even);
            }
}

}

ISsue : GEtting run time exception while running the code. --> String index out of bound exception

答案1

得分: 1

你可以尝试以下代码。这是因为在调用 sc.nextLine() 之前调用了像 nextInt() 这样的方法。

问题在于 nextInt() 不会消耗掉 \n,所以下一次对 nextLine() 的调用会消耗它,然后它会等待读取下一个元素的输入。

在调用 nextLine() 之前,你需要消耗掉 \n,或者你也可以直接在数组大小那一步调用 nextLine()

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.println("输入数组大小");
    int n = Integer.parseInt(sc.nextLine());
    String[] sa = new String[n];
    for (int i = 0; i < n; i++) {
        System.out.println("输入元素 " + i);
        String val = sc.nextLine();
        sa[i] = val;
    }
    String odd = "";
    String even = "";
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < sa[i].length(); j++) {
            if (j % 2 != 0) {
                odd = odd + sa[j].charAt(j);
            } else {
                even = even + sa[j].charAt(j);
            }
        }
        System.out.println(odd + " " + even);
    }
}
英文:

You can try below code. It is because of calling a method like nextInt() before sc.nextLine()

The problem is that nextInt() does not consume the '\n', so the next call to nextLine() consumes it and then it's waiting to read the input for next element.

You need to consume the '\n' before calling nextLine() or You can directly call nextLine() for array size as well.

public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println(&quot;Enter Array size&quot;);
        int n = Integer.parseInt(sc.nextLine());
        String[] sa = new String[n];
        for (int i = 0; i &lt; n; i++) {
            System.out.println(&quot;Enter Element &quot;+i);
            String val = sc.nextLine();
            sa[i]=val;
        }
        String odd = &quot;&quot;;
        String even = &quot;&quot;;
        for (int i = 0; i &lt; n; i++) {
            for (int j = 0; j &lt; sa[i].length(); j++) {
                if (j % 2 != 0) {
                    odd = odd + sa[j].charAt(j);
                } else {
                    even = even + sa[j].charAt(j);
                }

            }
            System.out.println(odd + &quot; &quot; + even);
        }
    }

huangapple
  • 本文由 发表于 2020年8月17日 20:06:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/63450459.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定