Search code examples
javaincompatibility

How to return only digits from a scanned string? "incompatible types" error. java


I tried to compile my code, but I got an "incompatible error" with my last code (else if). I was trying to say in that code, "if the input was a mixture of digits and letters return only digits", but it's telling me that there is something wrong with what I have done, and I couldn't figure it out.

import java.util.*;
public class Pr8{
  public static void main(String[] args){
    Scanner scan = new Scanner (System.in);

    //Prompt the user for how many numbers are going to be entered

    System.out.print("* Please write how many numbers are going to be entered: ");
      if (!scan.hasNextInt())
        System.out.println("- Sorry your entery was not correct. The compiler except's digits only.");

      else {
        int a = scan.nextInt(); //a is the scanned number of the user request
        int[] n = new int[a];   //is an array to declare a variable as much as the user entered
        int num = 1;            //to show the sorting number of the string when printing

        //prompt the user to enter a mixture of digits and letters

        for (int i = 0; i < a; i++){
          System.out.print("* Please enter a string #" + num++ + ": ");
            if (scan.hasNextInt()){          //check if the input has only integers
              n[i] = scan.nextInt();
              System.out.println("- " + n[i] + " = " + n[i]);
            }//if
            else if (!scan.hasNextInt()){   //if the input was a mixture of digits and letters, return only the digits
              n[i] = scan.nextLine();
              System.out.println("- there is letters");
            }//else  
        }//for
      }//else if

  }//main
}//Pr8

Solution

  • User Eran ist right. Your program is a bit unclean anyway. I suggest you get rid of the comments and use variable names which tell the reader what they are supposed to contain. What you want is probably this:

    import java.util.Scanner;
    
    public class Pr8 {
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            System.out.print("How many numbers do you wish to enter?  ");
            while (!scanner.hasNextInt()) {
                System.err.print("Again, how many numbers?  ");
                scanner.next();
            }
            int numberCount = scanner.nextInt();
            int[] numbers = new int[numberCount];
            for (int i = 0; i < numberCount; i++) {
                String numberString;
                System.out.print("Please enter string #" + (i + 1) + " containing a number:  ");
                scanner.nextLine();
                while ((numberString = scanner.findInLine("[+-]?[0-9]+")) == null) {
                    System.err.print("Again, string #" + (i + 1) + " should contain a number:  ");
                    scanner.nextLine();
                }
                numbers[i] = Integer.parseInt(numberString);
                System.out.println("Number #" + (i + 1) + " found = " + numbers[i]);
            }
        }
    }
    

    Sample console log:

    How many numbers do you wish to enter?  weewdfsa
    Again, how many numbers?  324klj
    Again, how many numbers?  5
    Please enter string #1 containing a number:  23
    Number #1 found = 23
    Please enter string #2 containing a number:  sdfsdf
    Again, string #2 should contain a number:  werq
    Again, string #2 should contain a number:  sdf345df
    Number #2 found = 345
    Please enter string #3 containing a number:  -34ewr
    Number #3 found = -34
    Please enter string #4 containing a number:  wqweq+555
    Number #4 found = 555
    Please enter string #5 containing a number:  xxx-11yyy
    Number #5 found = -11