如何将数字作为String类型读取并返回等效Integer类型的输出

时间:2018-03-10 15:38:49

标签: java

输入是一个用户字符串,例如 - > " ONE"输出应该是' 1'              或者," NINE"输出=' 9'

   public int check(String s)
   {
    int a=0;
    switch(s)
    {
        case "ZERO": a= 0;
        break;
        case "ONE": a=  1;
        break;
        case "TWO": a= 2;
        break;
        case "THREE": a= 3;
        break;
        case "FOUR": a= 4;
        break;
        case "FIVE": a= 5;
        break;
        case "SIX": a= 6;
        break;
        case "SEVEN": a= 7;
        break;
        case "EIGHT": a= 8;
        break;
        case "NINE": a= 9;
    }
    return a;
}

主要方法

public static void main(String args[])throws IOException
{
    int k=0,i;
    String wrd="";
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    String s = br.readLine();
    String str="ONE,ZERO,TWO,THREE,FOUR,FIVE,SIX,SEVEN,EIGHT,NINE,";
    int l = s.length();
    for(i=0;i<l;i++)
    {
        while(str.charAt(k)!=s.charAt(i))k++;
        if(str.charAt(k)==s.charAt(i))
        {
            while(!(str.charAt(k)==',') && (s.charAt(i)==(str.charAt(k))))
            {
                wrd=wrd+s.charAt(i++);
                k++;
            }
        }
        else
        while(str.charAt(k)!=s.charAt(i))k++;
    }
    System.out.println(wrd);
    Friend obj = new Friend();
    int a = obj.check(wrd);
    System.out.println(a);
}

上面写的代码将所有其他输入转换为整数,除了&#34; FIVE&#34; &#34; SEVEN&#34; &#34; EIGHT&#34; &#34; NINE&#34;对于这些输入,输出为&#39; 0&#39;

可能还有其他一些方法可以将I / P字符串转换为整数但是 我需要为其他一些目的而使用这种方法。

4 个答案:

答案 0 :(得分:0)

我会像这样使用枚举:

Array ( [blowfish_secret] => l2WkFOENKtuMAyJ7IR3m2JsU [Servers] => Array ( [1] => Array ( [auth_type] => config [host] => localhost [extension] => mysqli [controluser] => root [controlpass] => rootroot [pmadb] => phpmyadmin [bookmarktable] => pma__bookmark [relation] => pma__relation [table_info] => pma__table_info [table_coords] => pma__table_coords [pdf_pages] => pma__pdf_pages [column_info] => pma__column_info [history] => pma__history [table_uiprefs] => pma__table_uiprefs [tracking] => pma__tracking [userconfig] => pma__userconfig [recent] => pma__recent [favorite] => pma__favorite [users] => pma__users [usergroups] => pma__usergroups [navigationhiding] => pma__navigationhiding [savedsearches] => pma__savedsearches [central_columns] => pma__central_columns [designer_settings] => pma__designer_settings [export_templates] => pma__export_templates ) ) ) 

并使用它......

public enum MyNumber {

    ONE(1),
    TWO(2),
    THREE(3);

    private final int value;

    MyNumber(int value){
        this.value = value;
    }

    int getValue(){
        return this.value;
    }
}

请注意,如果你写

public static void main(String[] args) {
    //test
    System.out.println(MyNumber.ONE.getValue());
    System.out.println(MyNumber.valueOf("ONE"));
}

您将获得运行时异常。因此,您必须在传递参数之前检查字符串

答案 1 :(得分:0)

str变量的目的是什么?实现目标的简单方法可以是:

 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
 String s = br.readLine();
 System.out.println(s);
 Friend friend = new Friend();
 int a = friend.check(s);
 System.out.println(a);

答案 2 :(得分:0)

如果性能不是问题,请使用Map<String, Integer>

private static Map<String, Integer> map = new HashMap<>();

static {
    map.put("zero", 0);
    //do this for 1..9 here too
}

public static int check(String string) {
    Integer number = map.get(string.toLowerCase());
    return number == null ? -1 : number;
}

答案 3 :(得分:0)

你有没有理由不使用.split()?如果允许,您可以尝试:

Friend friend = new Friend();
String[] numbers = str.split(",");
for(int i = 0; i < numbers.length; i++){
    if(s.equals(numbers[i]){      //You could also do s.toUpperCase().equals(numbers[i])
        System.out.println(s);
        System.out.println(friend.check(s));
        break;
    }
}

但是,如果你不能使用.split(),你的解决方案的问题是多个while循环嵌套在for循环中。您可以使用if语句来解决此问题。试试这个:

 for(i = 0; i < l; i++)
    {
        while(str.charAt(k)!=s.charAt(i)){
        k++;
        }
        if((str.charAt(k) != ',') && (s.charAt(i)==(str.charAt(k))))
        {
            wrd=wrd+s.charAt(i);      //You don't need to use while since 
            k++;                      //you are already in for loop. So i++ is already done
        }
    }

如果您不想遇到错误,最好使用try catch块或异常。