0

I try to make a android application, the goal is that player will play against te computer. First player will fill in what kind of a digit(possibilities are 3 or 4 digit).

Let's say player choose 3 digit. Than computer generate a digit that every nummer may only use once in the digit. Like 021, 236, 750 ... Than player will create also 3 digit. Than player will try to guess computer nummer an computer will try to find player nummer. i have tried something like that;

public static void main(String[] args) {
    ArrayList<String> fiNum = new ArrayList<String>();
    ArrayList<String> cijfer = new ArrayList<String>();
    cijfer.add("0");
    cijfer.add("1");
    cijfer.add("2");
    cijfer.add("3");
    cijfer.add("4");
    cijfer.add("5");
    cijfer.add("6");
    cijfer.add("7");
    cijfer.add("8");
    cijfer.add("9");

    System.out.println("Please enter digit length ");
    Scanner nummerOfChoise = new Scanner(System.in);
    String cooiseresult = nummerOfChoise.nextLine();
    int cc = Integer.parseInt(cooiseresult);

    for (int i = 0; i < cc; i++) {
        int getal = (int) (Math.random() * cijfer.size());
        fiNum.add(cijfer.get(getal));
        cijfer.remove(getal);
    }
    System.out.print(fiNum);
}

If i want to print the nummer it prints something like

Please enter digit length
3
[3, 9, 1]

my question is what is the best approach to convert this array to integer or string and how can i do that.

Thank you.

Erwin Bolwidt
  • 28,093
  • 15
  • 46
  • 70
Muratcan
  • 235
  • 2
  • 5
  • 18

4 Answers4

2

Using your existing code, the easiest is probably:

String drieCijfers = fiNum.get(0) + fiNum.get(1) + fiNum.get(2);

But it may be better to not build your number as a List of Strings, but rather as an int or long to begin with:

public static void main(String[] args) {
    ArrayList<Integer> cijfer = new ArrayList<Integer>();
    for (int i = 0; i <= 9; i++)
        cijfer.add(i);

    System.out.println("Please enter digit length ");
    Scanner nummerOfChoise = new Scanner(System.in);
    String cooiseresult = nummerOfChoise.nextLine();
    int cc = Integer.parseInt(cooiseresult);

    long fiNum = 0;
    for (int i = 0; i < cc; i++) {
        int getal = (int) (Math.random() * cijfer.size());
        fiNum = fiNum * 10 + cijfer.get(getal);
        cijfer.remove(getal);
    }
    System.out.print(fiNum);
}
Erwin Bolwidt
  • 28,093
  • 15
  • 46
  • 70
1

You can do

public static void printNumber(int length) {
    List<Integer> digits = new ArrayList<Integer>();
    for(int i = 0; i < 10; i++) digits.add(i);
    Collections.shuffle(digits);
    for(int i = 0; i < length; i++)
        System.out.print(digits.get(i));
    System.out.println();
}

The shuffle works like shuffling a deck of cards, you can only draw a card/digit once.

Peter Lawrey
  • 498,481
  • 72
  • 700
  • 1,075
0

How about using this thread solution How do I generate random integers within a specific range in Java? to generate a number between a min and a max.

Then you compare the generated value with the value selected by the computer/player. You know the min, and the max should be the max number that you can have with X digits (10^X -1)

Community
  • 1
  • 1
Maxime
  • 560
  • 3
  • 15
0

Here is a similar question that should lead you in the direction you need.

How can I read input from the console using the Scanner class in Java?

Community
  • 1
  • 1
sudoJustin
  • 60
  • 8