简体   繁体   English

如果String.charAt(i)遇到空格,则会中断程序

[英]if String.charAt(i) encounters a space, it breaks the program

Following code basically replaces characters in a string with reverse characters in ABC.. 以下代码基本上用ABC中的反向字符替换字符串中的字符。

Eg. 例如。 if user inputs- AbC 如果用户输入-AbC

Then result would be ZyX 那么结果就是ZyX

My Problem is, if the input contains a space then it breaks the code and does not continue the execution. 我的问题是,如果输入包含空格,那么它将破坏代码并且不继续执行。

import java.util.*;

public class HelloWorld{

     int[] capitals = new int[26];
     int[] smalls = new int[26];
     public static void main(String []args){
         HelloWorld rs=new HelloWorld();
         rs.Initialize();

         rs.Encode();
     }

     public void Encode()
     {
         Scanner reader = new Scanner(System.in);
         System.out.println("Enter string: ");
         String input = reader.next();

         //System.out.println("User entered: " + input);
         String newString="";
         int pos=0;
         for (int in = 0; in < input.length(); in++)
         {
             if(input.charAt(in) == ' ')
             {
                newString += " ";
             }
             else
             {
                if((int)input.charAt(in) >= 65 && (int)input.charAt(in) <= 90)
                {
                    for(int i = 0; i< 26; i++) {
                        if((int)input.charAt(in) == capitals[i]) {
                            newString += (char)capitals[25-i];
                        }
                    }
                }
                else if((int)input.charAt(in)>=97 && (int)input.charAt(in) <= 122)
                {
                    for(int i = 0; i< 26; i++) {
                        if((int)input.charAt(in) == smalls[i]) {
                            newString += (char)smalls[25-i];
                        }
                    }
                }
                else
                {
                    if(input.charAt(in) == ' ')
                        newString += " ";
                    else
                        newString += input.charAt(in);
                }

             }
            pos = 0;
         }
         System.out.println(newString);
     }


     public void Initialize()
     {
        int pos=0;
        for (int i=65;i<=90;i++)
        {
             capitals[pos] = i;
             smalls[pos]= i + 32;
             pos++;
        }
     }
}

What wrong am I doing? 我在做什么错?

Calling Scanner.next() 调用Scanner.next()

Finds and returns the next complete token from this scanner. 查找并返回此扫描仪的下一个完整令牌。

Normally, a Scanner 's token is delineated by the space character. 通常, Scanner的令牌由空格字符划定。 From that same page: 在同一页面上:

The default whitespace delimiter used by a scanner is as recognized by Character.isWhitespace. 扫描仪使用的默认空格分隔符由Character.isWhitespace识别。

So when you call Scanner.next() , it's only reading up until the first space and, since you never read from it again, the rest of the input is discarded when the program ends. 因此,当您调用Scanner.next() ,它只会读取直到第一个空格为止,并且由于您再也不会从它读取了,因此其余的输入将在程序结束时被丢弃。

If you want everything the user enters, use nextLine instead. 如果您希望用户输入所有内容,请改用nextLine

According to the Javadoc 根据Javadoc

next() Finds and returns the next complete token from this scanner. next()查找并返回此扫描器的下一个完整令牌。

The space is a delimter, so the next token is always the first part of the string before the space character. 空格是分行符,因此下一个标记始终是空格字符之前字符串的第一部分。

You can set up the delimter pattern as explained here: 您可以按照以下说明设置除臭模式:

http://docs.oracle.com/javase/7/docs/api/index.html http://docs.oracle.com/javase/7/docs/api/index.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM