编码器不能使用多个空格

时间:2013-03-17 17:37:48

标签: java string char system.err

我正在制作一个java编码器,根据所要求的内容添加或减去字母,但每当我调用一个包含多个空格的字符串时(不必连续),代码就不会工作它返回一个错误。对不起,我的代码有点长,但我认为它非常普遍,因为它显示了我放置错误的位置。 (我得到的错误来自我的System.err.print ...语句,而不是编译器错误,但可以在自己的IDE上试用它

import java.util.InputMismatchException;
import java.util.Scanner;
public class Chap8_12 {
    static Scanner userInput = new Scanner(System.in);
    public static void main(String[] args)
    {
        String captain = getInput();
        if(captain == "Error")
        {
            System.err.println("Error - Invalid");
        }
        else
        {
            System.out.println("Your new word is: " + captain);
        }
    }
    public static String encode(String word, int n)
    {
        String newMessage = "";
        int index = 0;
        int length = word.length();
        int newVal = 0;
        while(index<length)
        {
            int val = word.charAt(index);
            if(val>'a' && val<'z')
            {
                newVal = val + n;

                if(newVal > 'z')
                {
                    newVal -= 26;
                }
            }
            else if(val>'A' && val<'Z')
            {
                newVal = val + n;

                if(newVal > 'Z')
                {
                    newVal -= 26;
                }
            }
            else if(val == ' ')
            {
                newVal = val;
            }
            else
            {
                return "Error";
            }
            char newLetter = (char) newVal;
            newMessage += newLetter;
            index++;
        }
        return newMessage;
    }
    public static String decode(String word, int n)
    {
        String newMessage = "";
        int index = 0;
        int length = word.length();
        int newVal = 0;
        while(index<length)
        {
            int val = word.charAt(index);
            if(val>'a' && val<'z')
            {
                newVal = val - n;
                if(newVal < 'a')
                {
                    newVal += 26;
                }
            }
            else if(val>'A' && val<'Z')
            {
                newVal = val - n;

                if(newVal < 'A')
                {
                    newVal += 26;
                }
            }
            else if(val == ' ')
            {
                newVal = val;
            }
            else
            {
                return "Error";
            }

            char newLetter = (char) newVal;
            newMessage += newLetter;
            index++;
        }
        return newMessage;
    }
    public static String getInput()
    {
        System.out.println("Type a word to test");
        String word = userInput.nextLine();
        System.out.println("Type e for encode and d for decode");
        String eord = userInput.nextLine();
        System.out.println("How many would you like to encode/decode by? ");
        int n = checkValidInt();
        if(n == 0)
        {
            return "Error";
        }
        if(eord.equals ("e") || eord.equals ("E"))
        { 
            return encode(word, n);
        }
        else if(eord.equals ("d") || eord.equals ("D"))
        {
            return decode(word, n);
        }
        else
        {
            return "Error";
        }
    }
    public static int checkValidInt()
    {
        try
        {
            return userInput.nextInt();
        }
        catch (InputMismatchException e)
        {
            return 0;
        }
    }
}

0 个答案:

没有答案