Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/390.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 字符串索引超出范围错误帮助。猪拉丁语项目_Java_Indexoutofboundsexception - Fatal编程技术网

Java 字符串索引超出范围错误帮助。猪拉丁语项目

Java 字符串索引超出范围错误帮助。猪拉丁语项目,java,indexoutofboundsexception,Java,Indexoutofboundsexception,我在做一个作业,把一个字符串句子转换成拉丁语 我已经计划并编写了所有代码,但我遇到了以下错误: 字符串索引超出范围 下面是我的代码: import java.util.*; public class project1d { public static void main(String[] args){ System.out.println("This is a pig latin translator. Enter a sentence to convert.");

我在做一个作业,把一个字符串句子转换成拉丁语

我已经计划并编写了所有代码,但我遇到了以下错误:

字符串索引超出范围

下面是我的代码:

import java.util.*;
public class project1d {
    public static void main(String[] args){
        System.out.println("This is a pig latin translator. Enter a sentence to convert.");
        Scanner console = new Scanner(System.in);
        pigLat(console);
    }
    public static void pigLat(Scanner console){
        String sentence = console.next();
        int start = 0;
        int end = 0;
        int counter =0;
        while(sentence.length()>0){
            while(end<sentence.length()-1){
                while(sentence.charAt(counter+1)!=' '){
                    counter++;     
                    end = counter;
                }
                String word = sentence.substring(start,end);
                int index= 0;
                char letter= word.charAt(index);       
                while (letter != 'a' || letter != 'e' || letter != 'i' || 
                letter != 'o' || letter != 'u'){
                    index++;
                }
                System.out.print(word.substring(index,word.length()-1)+"-");
                System.out.print(word.substring(0,index-1)+"ay");
                counter++;
                start=end+1;
            }
            System.out.println("Do you wanna put in another? Press ENTER to quit");
            sentence = console.next();
        }
    }
}


非常感谢您的帮助。

不幸的是,除了代码是否达到了预期目的之外,代码还有很多问题

  • 您正在增加索引并检查值,但没有确认这些索引是否有效

  • 在整个过程的第一次迭代之后再次使用变量之前,您没有清除这些变量

  • 你在增加索引,但是你在字母检查时得到了一个无限循环,因为你在增加索引时没有改变字母
  • 字母检查是检查重叠的条件-字母!=一封信=e将始终为真,因此它也是一个无限循环
  • 正如Andy所指出的:String语句=console.next();我只能读一个单词。改用nextLine
请尝试:

public static void pigLat(Scanner console){
    String sentence = console.next();
    int start = 0;
    int end = 0;
    int counter =0;
    while(sentence.length()>0){
        start = 0; end = 0; counter = 0;
        while(end<sentence.length()-1){
            while(sentence.length()>counter+1 && sentence.charAt(counter+1)!=' '){
                counter++;     
                end = counter;
            }
        String word = sentence.substring(start,end);
        if(word.length()>0){
            int index = 0;
            char letter = word.charAt(index);       
            while(index+1<word.lenght() && letter != 'a' && letter != 'e' && letter != 'i' && letter != 'o' && letter != 'u'){
                index++;
                letter = word.charAt(index);
            }
            System.out.print(word.substring(index,word.length()-1)+"-");
            System.out.print(word.substring(0,index-1)+"ay");
        }
        counter++;
        start=end+1;
    }
    System.out.println("Do you wanna put in another? Press ENTER to quit");
    sentence = console.nextLine();
}
public static void pigLat(扫描仪控制台){
String语句=console.next();
int start=0;
int end=0;
int计数器=0;
while(句子长度()>0){
开始=0;结束=0;计数器=0;
while(endcounter+1&&句子.charAt(counter+1)!=“”){
计数器++;
结束=计数器;
}
字符串字=句子。子字符串(开始、结束);
if(word.length()>0){
int指数=0;
字符字母=word.charAt(索引);

while(index+1表示您正在尝试获取字符串的第5个字符,该字符串最多有4个字符。请先测试长度。旁白:
String-sensume=console.next();
将只读取一个单词。请使用
nextLine()
阅读整行内容。不确定要做什么。请指定代码应该实现什么?输入=>输出。