在方法java中将字符串转换为int

时间:2014-11-21 04:14:02

标签: java string int

所以我试图将int转换为字符串,然后将charAt(0),charAt(1)和charAt(2)转换为。 我这样做是为了将3位int分成3个不同的整数。我想将这些单个整数转换为字符串。

我要做的是从101及以上取数字并用文字打印。我有数百种,数十种和一种方法。我试图取第一个整数并将其应用于数百个方法,第二个整数并将其应用于数十和第三个整数方法。

这是> = 101

的方法
import java.util.Scanner;


public class rough {

    public static void main(String args[]) {
        int number = 0;
        Scanner scanner = new Scanner(System.in);
        System.out.print("Please type a number between 0 and 999 OR type -1 to exit:  ");
        number = scanner.nextInt();
        if (number >= 101) {
            System.out.println(hundred(first) + " AND" + tens(second) + "" + From1To19(third));
        } else {
            System.out.println("please input a number from 101: ");
        }
        //this is what i have so far(might be junk).

    public static void From101(int num) {
        String SNumber = Integer.toString(num);
        char First = SNumber.charAt(0);
        char Second = SNumber.charAt(1);
        char Third = SNumber.charAt(2);
        int num1 = Integer.parseInt(first);
    }
}

现在我正在尝试打印这些单词,我收到3个错误。

System.out.println(hundred(first) + " AND" + tens(second) + "" + From1To19(third));

我在if / else语句中添加了该行,错误是:

 ----jGRASP exec: javac -g rough.java

rough.java:27: error: 'void' type not allowed here
                           System.out.println(hundred(first) + " AND" + tens(second) + "" + From1To19(third));
                                                     ^
rough.java:27: error: 'void' type not allowed here
                           System.out.println(hundred(first) + " AND" + tens(second) + "" + From1To19(third));
                                                             ^
rough.java:27: error: 'void' type not allowed here
                           System.out.println(hundred(first) + " AND" + tens(second) + "" + From1To19(third));
                                                                                     ^
3 errors

 ----jGRASP wedge2: exit code for process is 1.
 ----jGRASP: operation complete.

4 个答案:

答案 0 :(得分:1)

现在您将int转换为String,将String转换为char,将char转换回int

您可以跳过所有这些并直接从int - > int,使用modular division

例如,要获取12345的个别数字:

int a = 12345;
int b = a%10; //b = 5
a = a / 10; //now a = 1234
int c = a%10; //c = 4
a = a / 10; //now a = 123
int d = a%10; //d = 3
a = a / 10; //now a = 12
int e = a%10; //e = 2

答案 1 :(得分:0)

这对你有帮助。

public static void main(String args[]) {
        int number = 0;
        Scanner scanner = new Scanner(System.in);
        System.out.print("Please type a number between 0 and 999 OR type -1 to exit:  ");
        number = scanner.nextInt();
        if (number >= 101) {
            From101(number);
        } else {
            System.out.println("please input a number from 101: ");
        }

    }

    private static void From101(int num) {
        String SNumber = Integer.toString(num);
        int num1 = Character.getNumericValue(SNumber.charAt(0));
        int num2 = Character.getNumericValue(SNumber.charAt(1));
        int num3 = Character.getNumericValue(SNumber.charAt(2));
        System.out.println(num1 + " " + num2 + " " + num3);
    }

<强>输出

Please type a number between 0 and 999 OR type -1 to exit:  101
1 0 1

参考this

答案 2 :(得分:0)

这对你来说很好,而....在这个你可以增加你的数字范围......

import java.util.Scanner;

public class rough {

    public static void main(String args[]) {
        int number = 0;
        Scanner scanner = new Scanner(System.in);
        System.out.print("Please type a number between 0 and 999 OR type -1 to exit:  ");
        number = scanner.nextInt();
        if (number >= 101) {
            From101(number);
        } else {
            System.out.println("please input a number from 101: ");
        }
        //this is what i have so far(might be junk).

    public static void From101(int num) {
       while(num>0)
       {
         d=num%10;
         System.out.print(d + " ");
         num=num/10;
       }
    }
}

答案 3 :(得分:0)

如果您使用API​​,这实际上非常简单:

public static void main(String[] args)
{
    Scanner scanner = new Scanner(System.in);
    System.out.print("Please type a number between 0 and 999 OR type -1 to exit:  ");
    int number = -1;
    try
    {
        number = scanner.nextInt();
        String numString = String.valueOf(number);
        char[] digits = numString.toCharArray();
        System.out.println(numString);
        for (char digit : digits)
        {
            System.out.println(digit);
        }
    }
    catch (InputMismatchException e)
    {
        System.err.println("Entered string is not numeric. Exiting program...");
    }
    finally
    {
        scanner.close();
    }
}