java.lang.ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 1

Moliay 2018-12-02 06:13:20
package javaPractice;

import java.util.Scanner;

public class TestScanner {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.next();
char[] a = new char[str.length()];
a = str.toCharArray();
int i = 0;
while(a[i]!='#') {
System.out.println((int)a[i]);
i ++;
}
sc.close();
}
}

...全文
4087 15 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_39936465 2018-12-10
  • 打赏
  • 举报
回复


import java.util.Scanner;

public class test1 {

public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
char[] a;
boolean flag = true;
while (flag) {
System.out.println("请输入字符,输入'#'结束:");
String str = sc.next();
a = new char[str.length()];
a = str.toCharArray();
for (char i : a) {
if (i == '#') {
flag = false;
break;
} else {
System.out.println((int) i);
}
}

}

}

}
  • 打赏
  • 举报
回复


import java.util.Scanner;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class TestScanner
{
    private static final Logger LOGGER = LoggerFactory.getLogger(TestScanner.class);
    
    public static void main(String[] args)
    {
        String input = null;
        Scanner sc = new Scanner(System.in);
        do
        {
            LOGGER.info("------------输入#退出,其他值继续------------");
            input = StringUtils.trimToEmpty(sc.nextLine());
            char[] a = input.toCharArray();
            int i = 0;
            while (i < a.length && a[i] != '#')
            {
                System.out.println((int)a[i]);
                i++;
            }
        } while (!StringUtils.contains(input, "#"));
        LOGGER.info("------------成功退出------------");
        sc.close();
    }
} 
Moliay 2018-12-05
  • 打赏
  • 举报
回复
引用 13 楼 qq_39936465 的回复:
[quote=引用 11 楼 Moliay 的回复:]
可是这样只能输入一次就满了,判断是否输入#也就没有意义了,不符合本意啊



你先说说你本意要干啥?你一段程序我们只能看出其中的错误,并不能完全看出你要干啥![/quote]对于以上猿兄们说句抱歉,智障儿童的问题。
我的目的是每当我输入单个非‘#’的字符,输出对应的整数,当输入‘#’时,结束输入。
qq_39936465 2018-12-05
  • 打赏
  • 举报
回复
引用 11 楼 Moliay 的回复:
可是这样只能输入一次就满了,判断是否输入#也就没有意义了,不符合本意啊
你先说说你本意要干啥?你一段程序我们只能看出其中的错误,并不能完全看出你要干啥!
  • 打赏
  • 举报
回复
不清楚你的要求,参考这个改造下代码吧


public void test()
{
String input = null;
Scanner sc = new Scanner(System.in);
do
{
LOGGER.info("------------输入x退出,回车换行继续------------");



input = StringUtils.trimToEmpty(sc.nextLine());
} while (!StringUtils.equalsIgnoreCase(input, "x"));
LOGGER.info("------------成功退出------------");
sc.close();
}


Moliay 2018-12-04
  • 打赏
  • 举报
回复
引用 10 楼 叶遮沉阳 的回复:

public class ScannerTest {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
char[] a = str.toCharArray();
int i = 0;
while(i < a.length && a[i]!='#') {
System.out.println(a[i]);
i ++;
}
sc.close();
}
}
可是这样只能输入一次就满了,判断是否输入#也就没有意义了,不符合本意啊
叶遮沉阳 2018-12-04
  • 打赏
  • 举报
回复

public class ScannerTest {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String str = sc.nextLine();
        char[] a = str.toCharArray();
        int i = 0;
        while(i < a.length && a[i]!='#') {
            System.out.println(a[i]);
            i ++;
        }
        sc.close();
    }
}
Moliay 2018-12-04
  • 打赏
  • 举报
回复
真实
  • 打赏
  • 举报
回复
引用 6 楼 sk815 的回复:
引用类型不能用!= ··这个条件永远不满足
只说不练(运行)假把式 !
sk815 2018-12-04
  • 打赏
  • 举报
回复
引用类型不能用!= ··这个条件永远不满足
Moliay 2018-12-04
  • 打赏
  • 举报
回复
引用 4 楼 MoShaoBBC的回复:
那个是因为输入的字符串中, 没有输入#,也就是说,找不到结束的条件,while循环里面的i++,最终超出数组a的长度,报此异常。
代码很好其实狗屎的日常
MoShaoBBC 2018-12-03
  • 打赏
  • 举报
回复
那个是因为输入的字符串中, 没有输入#,也就是说,找不到结束的条件,while循环里面的i++,最终超出数组a的长度,报此异常。
Moliay 2018-12-02
  • 打赏
  • 举报
回复
package javaPractice;

import java.util.*;

public class TestScanner {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
ArrayList<Character> a = new ArrayList<Character>(str.length());
int i = 0;
while(str.charAt(i)!='#') {
a.add(str.charAt(i));
System.out.println((int)a.get(i));
i ++;
}
sc.close();
}
}

智障儿童不能想到为什么i<a.getSize()
  • 打赏
  • 举报
回复
while(a[i]!='#') 改为
while (i < a.length && a[i] != '#')

原因很明显!

Moliay 2018-12-02
  • 打赏
  • 举报
回复
package javaPractice;

import java.util.ArrayList;
import java.util.Scanner;

public class TestScanner {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.next();
ArrayList<Character> a = new ArrayList<Character>();
int i = 0;
while((char)a.get(i)!='#') {
a.add(str.charAt(i));
System.out.println((int)a.get(i));
i ++;
}
sc.close();
}
}

62,634

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧