Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/342.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 - Fatal编程技术网

Java 如何更改字符串,如';圣玛丽亚';变成;圣玛丽亚;?

Java 如何更改字符串,如';圣玛丽亚';变成;圣玛丽亚;?,java,Java,我的程序正在运行,但不是以我想要的方式运行。这是我想要的输出: C:\Documents and Settings\Joe King\My Documents\418.85A Java\Projects\Day 6>java Project3 nina pinta "santa maria" The Nina is decreasing throttle! The Nina is ready to launch... but the throttl

我的程序正在运行,但不是以我想要的方式运行。这是我想要的输出:

C:\Documents and Settings\Joe King\My Documents\418.85A Java\Projects\Day 6>java Project3 nina pinta "santa maria"



The Nina is decreasing throttle!

    The Nina is ready to launch...

            but the throttle is down, increase throttle!


The Pinta is lowering the main!

    The Pinta is ready to launch...

            but the sail is down, hoist the main!


The Santa Maria is hoisting the main!

    The Santa Maria is ready to launch...

            the sail is up, ahead full!



(press ENTER to exit)
正如你所看到的,圣玛丽亚已经被资本化了。以下是我得到的输出:

C:\Documents and Settings\Joe King\My Documents\418.85A Java\Projects\Day 6>java Project3 nina pinta "santa maria"



The Nina is decreasing throttle!

    The Nina is ready to launch...

            but the throttle is down, increase throttle!


The Pinta is lowering the main!

    The Pinta is ready to launch...

            but the sail is down, hoist the main!


The santa maria is hoisting the main!

    The santa maria is ready to launch...

            the sail is up, ahead full!



(press ENTER to exit)
我的代码无法将名称字符串大写,比如Santa Maria。这是我的密码:

class Project3{

public static void main(String[] args){

    Boat[] boatArray;
    String result = " "; 
    char firstChar;
    char firstLetter;
    char secondLetter;
    int i;

    boatArray = new Boat[args.length];

    if(args.length > 0){

        for(i = 0 ; i < args.length ; i++){

            String delimiters = "[ ]";
            int limit = -1;

            String[]tokens = args[i].split(delimiters, limit);

            for( int k = 0 ; k < tokens.length ; ++k ){

                if( tokens[k].length() > 1 ){

                    tokens[k] = tokens[k].trim();

                }else{

                    tokens[k] = " ";

                }

                firstChar = tokens[k].charAt(0);

                if(firstChar == ' '){

                    break;

                }else{

                    if(Character.isUpperCase(firstChar)){

                        break;

                    }else{

                        firstChar = Character.toUpperCase(firstChar);
                        char[] tokenArray = tokens[k].toCharArray();                            
                        String text = new String(tokenArray, 1, (tokenArray.length - 1) );                          
                        tokens[k] = firstChar + text;

                    }

                    result = result + tokens[k];

                    if( k != tokens.length - 1 ){

                        break;

                    }else{

                        result = result.trim();
                        args[i] = result;
                        result = " ";

                    }
                }
            }       

            firstLetter = args[i].charAt(0);

            if((firstLetter == 'B') || (firstLetter == 'C') || (firstLetter == 'N')){

                boatArray[i] = new RaceBoat();
                boatArray[i].christenBoat(args[i]);

            }else{

                boatArray[i] = new SailBoat();
                boatArray[i].christenBoat(args[i]);

            }


        }

        System.out.println("\n");

        for(i = 0 ; i < args.length ; i++){         

            secondLetter = Character.toUpperCase(args[i].charAt(1));

            if((secondLetter == 'A') || (secondLetter == 'E')){

                boatArray[i].increaseSpeed();
                boatArray[i].goFast();

            }else{

                boatArray[i].decreaseSpeed();
                boatArray[i].goSlow();

            }           

            boatArray[i].launchBoat();
            boatArray[i].whatIsBoatState();

        }

    }else{

        System.out.println("\n\nArgh!... you forgot to enter ship names scalawag!" +
            "\n\n\n\tPlease try again!");

    }

    System.out.println("\n\n(press ENTER to exit)\n\n");

    try{

        System.in.read();

    } catch(IOException e){

        return;
    }
}
我以为这段代码会把它删掉:

if( tokens[k].length() > 1 ){

                    tokens[k] = tokens[k].trim();

                }else{

                    tokens[k] = " ";

                }

但显然不是。如何实现这一点?

看看Commons Lang
StringUtils.capitalize
函数

如果您想了解pro是如何制作的,您可以使用源代码:)

这可能会使你的话大写

StringBuilder b = new StringBuilder();
String[] parts = originalWord.split(" ");
for(String part : parts) {
    String capitalized = part.substring(0, 1).toUppercase() + part.substring(1);
    b.append(capitalized).append(" ");
}
//and then you can remove the last space if it is your taste.
return b.toString();
它缺少基本检查,但可以:)

祝你好运

我以为这个代码会把它删掉

实际上,该代码还将用空格替换任何一个字母单词

另一个问题是您使用的正则表达式。您希望将一个或多个空格视为单个分隔符。关于如何编写正则表达式,请参阅javadocs和/或您的教科书和课堂讲稿。(注意:如果你做对了,就不需要删减空格。Split会帮你的。)

足够的提示:-)



我也考虑过这一点,但大多数船名在英语中没有一个字母


你不应该把这样的假设硬连接到你的代码中。尤其是当你不需要的时候。硬连线的假设导致脆弱的代码;i、 e.当你的假设被证明不正确时,代码会中断。

你的问题在于你正在使用中断;在许多地方,这将终止循环,因此根本不会处理下面的字。可能您只想跳过当前单词/arg,因此应该使用continue,它将继续for循环和下一个索引


您可以使用调试器或添加一些中间printf语句来更好地理解代码流。

感谢您的帮助Kristopher!谢谢你的回复,我将尝试使用此代码!我已经添加了一个简单且不完整但功能强大的示例,如果您想让它发挥作用的话。作为一名专业程序员,我的一条建议是“保持简单”。如果你需要大量代码来完成一项简单的任务,那么你要么工作过度,要么思考不好。祝你好运再次感谢你的帮助,希望不久的将来我也能成为一名专业人士。我也考虑过这一点,但大多数船名的英文字母都没有。我知道在西班牙语中,你可以用“y”作为例子,但正如你所见,这不值得制作:)。再想一想,可以使用“A”,这样就可以考虑其他事情了!谢谢你的提示,我会调查的!你当然是绝对正确的!谢谢,我删除了代码!我删除了break语句,并将其替换为continue。非常感谢。
StringBuilder b = new StringBuilder();
String[] parts = originalWord.split(" ");
for(String part : parts) {
    String capitalized = part.substring(0, 1).toUppercase() + part.substring(1);
    b.append(capitalized).append(" ");
}
//and then you can remove the last space if it is your taste.
return b.toString();