如何从一串数字中提取单个数字?

时间:2011-03-02 00:47:59

标签: java string

示例:

String number = "1234567890123456";

如何在int类型变量中自己存储倒数第二个数字?

详细说明:

for (int i = number.length()-2; i>-1; i-=2)
    int x = (Extract the  number) at i;

我该怎么做?

以下是代码:

package creditcard;
import java.io.FileWriter;
import java.io.BufferedWriter;
import java.io.IOException;

public class Card {

    //Declarations
    private String cardNumber;
    private boolean test = false;
    private String cardType = "Unknown";

    public Card(){
        cardNumber = "0";
    }//end Default Constructor

    public Card(String input){
        cardNumber = input;
    }//end Constructor

    //
    public void typeMatcher(){
        if (cardNumber.startsWith("37"))
            cardType = "American Express";
        if (cardNumber.startsWith("4"))
            cardType = "Visa";
        if (cardNumber.startsWith("5"))
            cardType = "Mastercard";
        if (cardNumber.startsWith("6"))
            cardType = "Discover";
    }//end typeMatcher

    // Returns true if card number is Valid.
    public boolean isValid(String cardNumber){
        if (cardNumber.length() > 12 && cardNumber.length() < 17)
            if((sumOfDoubleEvenPlace(cardNumber)+sumOfOddPlace(cardNumber))/10==0)
                test = true;
        return test;
    }//end isValid

    // Get result from Step 2.
    public int sumOfDoubleEvenPlace(String cardNumber){
        int sum = 0;
        int num = 0;
        for (int i = cardNumber.length()-2; i>-1; i-=2){
            num = Integer.parseInt(cardNumber.substring(i,i+1));
            if (num<10)
                sum+=(num*2);
            else
                sum+=getDigit(num);
        }//end for
        System.out.println(sum);
        return sum;
    }//end SumOfDoubleEvenPlace

    // Return this number if it is a single digit
    // Otherwise return the sum of the two digits.
    public int getDigit (int num){
        int no1 = num/10;
        int no2 = num%10;
        return no1+no2;
    }//end getDigit

    // Returns sum of odd place digits in number.
    public int sumOfOddPlace(String cardNumber){
        int sum = 0;
        for (int i = cardNumber.length()-1; i>-1; i-=2)
            sum += Integer.parseInt(cardNumber.substring(i,i));
        System.out.println(sum);
        return sum;
    }//end sumOfOddPlace

    public void writeToFile(){
        try{
            FileWriter fw = new FileWriter("G:\\Output.txt",true);
            BufferedWriter bw = new BufferedWriter(fw);
            if (isValid(cardNumber)){
                bw.write(cardNumber+" is a valid "+cardType+" card.");
                bw.newLine();
            }//end if
            else{
                bw.write(cardNumber+" is an invalid "+cardType+" card.");
                bw.newLine();
            }//end else
            bw.close();
            System.out.println("Success");
        }//end try

        catch(IOException ioe){
            ioe.printStackTrace();
        }//end catch

    }//end outputToFile

}//end Card

目标是检查信用卡号是否有效(学校项目)

5 个答案:

答案 0 :(得分:2)

只需substring / charAt您想要的数字并解析它......

int d = Integer.parseInt(number.substring(start, start+1))

答案 1 :(得分:0)

这样做:

int x = number.charAt(i) - '0'

Integer.parseInt在这里太过分了。

答案 2 :(得分:0)

方法 sumOfOddPlace 中的子字符串代码存在缺陷:

  sum += Integer.parseInt(cardNumber.substring(i,i));

应该是

  sum += Integer.parseInt(cardNumber.substring(i,i+1));

这可能是您的格式例外的原因。

答案 3 :(得分:-1)

有一个名为“charAt()”的方法可以做你想做的事。

 char c = someString.charAt(someString.length - 2);

然后你需要将字符转换为整数。

选择@Andrew White的答案,因为这使整数转换更容易。你可以用一个char来做,但它有点icky(对我不好,因为我是一个C级程序员)。

答案 4 :(得分:-1)

如果你想要第二个持续时间:

if (number.length() >= 2) {
    int myNumber = number.charAt(number.length() - 2) - '0';
}

或者你的循环:

for (int i = number.length()-2; i>-1; i-=2)
    int x = number.charAt(i) - '0';