-1

I am trying to figure out the best way to read in 3 integers from 0 to 9 with no spaces. Eventually I will have to check the input to make sure there are no duplicates or letters and that 3 numbers have been entered. This is what I've tried so far. Would it be better to read in the input as a string then put the numbers in an integer array? Any tips would be greatly appreciated.

 Scanner guess = new Scanner(System.in);
 int[] array = new int[4];

  System.out.println("Enter 3 numbers from 0 to 9");

 while (guess.hasNextInt()) {
        array[i] = guess.nextInt();
        i++;
        if (i == 3) {
           break;
        }
    }

5 Answers5

1

What you have is alright. I wouldn't use strings, I would just use a for loop:

Scanner input = new Scanner(System.in);
int[] arnNums = new int[3];

System.out.println("Enter 3 numbers from 0 to 9");

for(int i = 0; i < 3; i++)
{
    arnNums[i] = input.nextInt();
}
  • I still have to press enter after each number to put into the array. Say the user enter 123 I need arnNums[0] = 1, arnNums[1] = 2, and arnNums[2] = 3. I'm not sure how to get them to separate. – user3264060 Feb 08 '14 at 04:11
  • To make this a bit better, you should use `i < arnNums.length` instead of `i < 3`. This way, you could only potentially update the number of iterations in one place. – Makoto Feb 08 '14 at 04:11
  • @user3264060: If you enter them as, for example, "23 23 1", it will still work. – Makoto Feb 08 '14 at 04:12
  • I need it to work with no spaces unfortunately. – user3264060 Feb 08 '14 at 04:15
  • Instead of `input.nextInt()`, you could read in a string, then do `arnNums[i] = Integer.parseInt(string.substring(i, i+1))` – user3286112 Feb 08 '14 at 04:26
  • It worked! Thank you Now I was thinking a try catch block around the for loop to catch any characters, but how would I limit the number of integers that could be entered? Let them enter the numbers and if the array goes outofbounds the try catch would catch the error? – user3264060 Feb 08 '14 at 04:37
  • You could check the length of the string and see if it's greater than the number of integers you want entered, then show an error or something. – user3286112 Feb 08 '14 at 04:44
1

nextInt() will always give you only the integer part of the input.

Nikola Despotoski
  • 49,966
  • 15
  • 119
  • 148
1

nextInt() for Scanners assumes that the int values will be space-separated, but you've stated that they will be entered without spaces (such as "314"). So, nextInt() won't do what you want.

You might consider reading the input as a String, and then grabbing each digit using charAt() from the String class (and note that your array only needs to be of length 3...not 4 as you had it):

Scanner guess = new Scanner(System.in);
int[] array = new int[3];

System.out.println("Enter 3 numbers from 0 to 9");
String input = guess.next();

for (int i = 0; i < 3; i++) {
  array[i] = input.charAt(i) - '0';
}
normKrumpe
  • 63
  • 5
0

Unless I'm misinterprinting the quesiton, sounds like you want to read [0-9] multiple times.

Safest/best way I can think of would be to scan it as an int (so it parses out extrenous text), convert to string, parse, then convert back the seperate integers. e.g.

Scanner guess = new Scanner(System.in);
int[] array = new int[3];
System.out.println("Enter 3 numbers from 0 to 9");
if (guess.hasNextInt()) {
  String g = guss.nextInt().toString().;
  int max = g.length() > 2 ? 3 : g.length();
  for(int i = 0; i < max; i++) {
    array[i] = Character.getNumericValue(g.charAt(i));
  }
} else {
  //bad guess
}

If you expect that user will instead enter it as something like 1 2 3, you can put it in a while loop until you get at least 3 items in your array.

Reinstate Monica Please
  • 11,123
  • 3
  • 27
  • 48
0

Eventually I will have to check the input to make sure there are no duplicates or letters and that 3 numbers have been entered.

Try,

Scanner guess = new Scanner(System.in);
int[] array = new int[4];
System.out.println("Enter 3 numbers from 0 to 9 , separated by comma (,)");
String input = guess.next();
Set <String> set = new HashSet<String>();
set.addAll(Arrays.asList(input.trim().split(","))); // Move in to a hashset (it does not accept duplicate values)
if(set.size()<3){
    System.out.println("Duplicate Data FounD");
}else{
    System.out.println("Data entered in a corect manner");
    System.out.println("The final array ::");
    int i= 0;
    for(String str :set){
        array[i] = Integer.parseInt(str.trim());
        System.out.println(array[i]);
        i++;
    }
}

Output: case 1:

Enter 3 numbers from 0 to 9 , separated by comma (,)
12,23,12
Duplicate Data FounD

Output: case 2:

Enter 3 numbers from 0 to 9 , separated by comma (,)
23,24,25
Data entered in a corect manner
The final array ::
23
24
25
Rakesh KR
  • 6,357
  • 5
  • 40
  • 55