简体   繁体   中英

Program about how many times the number has been repeated

For example i want to find how many times 1 repeated in number 123900148 It must be write 2 times but i get wrong values for everytime

Scanner input = new Scanner(System.in);
@author Başar Ballıöz

int counter = 0;
int repeat;
int tmp;


System.out.print("Enter A Number: ");
tmp = input.nextInt();
String number = Integer.toString(tmp);


System.out.print("Enter A Number You Want To Find: ");
repeat = input.nextInt();


for (int i = 0; i < number.length() - 1 ; i++) {    

    if (number.substring(i , i+1).equals(repeat))

    counter++;
}

System.out.println(repeat + " number " + counter + " repeated.");

i would like to see my output like:

number : 134211
number i want to find how many times repeated: 1
your number has repeated 3 times

You are comparing a String (returned by number.substring(i , i+1 ) to an Integer , so of course it will always return false .

Either compare two int s or two String s. Since you are essentially comparing two digits, comparing int s would be more efficient.

for (int i = 0; i < number.length(); i++) {    
    if (Character.getNumericValue(number.charAt(i)) == repeat) {
        counter++;
    }
}
  Scanner input = new Scanner(System.in);

    int counter = 0;
    int repeat;
    int tmp;

    System.out.print("Enter A Number: ");
    tmp = input.nextInt();
    String number = Integer.toString(tmp);

    System.out.print("Enter A Number You Want To Find: ");
    repeat = input.nextInt();

    while (tmp > 0) {

        if (tmp % 10 == repeat) {
            counter++;               
        }
        tmp = tmp/10;
    }

    System.out.println(number + " number " + counter + " repeated.");

You're comparing a String against an Integer via equals hence you're not getting the expected result. instead convert the integer to a string prior to comparison:

 if (number.substring(i , i+1).equals(String.valueOf(repeat)))

Further, you could cache the result of String.valueOf(repeat) into a variable before the for loop to prevent a string object construction in each iteration of the loop.

Try this. I added some helpful output so you can see how it's indexed.

import java.util.Scanner;
import java.util.Map;
import java.util.HashMap;

public class CountChars {

    public static void main(String [] args) {

        Scanner input = new Scanner(System.in);

        System.out.print("Enter A String: ");
        Map<String, Integer> map = indexString(input.nextLine());

        while (true) {
            System.out.print("Enter A Character You Want To Count (ENTER to exit): ");
            String repeat = input.nextLine();
            if (repeat == null || repeat.isEmpty()) {
                break;
            }
            System.out.println(String.format("'%s' was repeated %d time(s).", repeat, (map.containsKey(repeat)) ? map.get(repeat):Integer.valueOf(0)));
        }
    }

    private static Map<String, Integer> indexString(String s) {
        Map<String, Integer> map = new HashMap<>();
        System.out.println(String.format("'%s' has %d characters.  Indexing now.", s, s.length()));
        for (int i = 0; i < s.length() ; i++) {
            String c = String.valueOf(s.charAt(i));
            if (!map.containsKey(c)) {
                map.put(c, 0);
                System.out.println(String.format("Indexing %s", c));
            }
            System.out.print(String.format("Incrementing '%s' from %d ", c, map.get(c)));
            map.put(c, map.get(c) + 1);
            System.out.println(String.format("to %d.", map.get(c)));
        }
        return map;
    }

}

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