简体   繁体   中英

Accessing single letters in String / digits in numbers - Java

I have numeric input (11 digits), and I need to perform some operations on each digit (example: multiply 1st by 5, 2nd by 3, etc.). How can I do so in Java? Is there a simple way to access single letter / digit? Is there another way to do it?

If you don't want to convert the number to a string, then there is a simple trick.

digit = number % 10 

will give you the right most digit.

number = number / 10 

Will remove the right most digit from the number.

So you can run in a loop until the number reaches 0.

while(0 < number)
{
    int digit = number % 10;
    number = number / 10;

    // do here an operation on the digits
}

You can use a for loop to help you count. For example

for(int index = 0; 0 < number; ++index, number /= 10)
{
    int digit = number % 10;
    // do an operation on the number according to the 'index' variable
}

Here is a similar StackOverFlow question on a similar question

You can use .charAt() to get a character from a string. Then using Character.getNumericValue() you can convert the character to an integer.

Like this:

String string = "1434347633";

int digit1 = Character.getNumericValue(string.charAt(1));

Convert that number input to String data type so that you can interpret it as a String.

int numbers = 1122334455; // integer won't be able to save this much data, 
// just for example ok, 
String numberString = numbers.toString();
foreach (char number in numberString) {
  // do work on each of the single character in the string.
}

You should work them out, depending on the some condition.

If you want to access the digits by index without converting to a string, you can use these two functions length and digitAt :

public class DigitAt {

    public static int length(long v) {
        return (int) Math.ceil(Math.log10(v));
    }

    public static int digitAt(long v, int digit) {
        return (int) ((v / (long) Math.pow(10, length(v) - digit - 1)) % 10);
    }

    public static void main(String[] args) {
        System.out.println("Digits: " + length(1234567));
        System.out.println(digitAt(1234567, 0));
        System.out.println(digitAt(1234567, 1));
        System.out.println(digitAt(1234567, 6));
    }
}

Well there are many ways you can do it like :

        int a = 12345;
        int b;
        while(a>0)
        {
            b = a%10;
            System.out.print(b + " ");
            a = a/10;
        }

Here it gives you the digits in reverse order like you will get b=5 then b=4....

You can just manipulate them

Other way

    int d = 12345;
    String str = String.valueOf(d);
    for(int i=0;i<str.length();i++)
    {
        char c = str.charAt(i);
        System.out.print(Character.getNumericValue(c) * 10 + " ");

    }

Or

        char c[] = str.toCharArray();
        for(Character ch : c)
        {
            System.out.print(Character.getNumericValue(ch) * 2 + " ");
        }
public String stringAfterOperations(String digits) {

    ArrayList<Integer> z = new ArrayList<Integer>();

    for(Character c: digits.toCharArray()) {
        z.add(Character.getNumericValue(c));
    }

    //TODO Set your own operations inside this "for"
    for(int i=0; i<z.size(); i++){
        if(i == 1){
            z.set(i, z.get(i)*4);
        }
        else if(i == 7){
            z.set(i, z.get(i)/3);
        }
        else {
            z.set(i, z.get(i)+2);
        }
    }

    String newString = "";
    for(Integer i: z){
        newString += i;
    }

    return newString;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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