0
  1. Hi, I attached my solution about yours problems, I comment diferents parts about code. There are two functions where a signal if is a numérica data or category of age. &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&

    import java.util.Scanner;
    
    public class main {
    
    public static void main(String[] args) {
        System.out.print("Type your age: ");
    
        Scanner sc = new Scanner(System.in);
    //Save var String
        String ageStr = sc.nextLine();
    
        String notify="Not an age.";
        //Evaluate the age if we typify a number.
            if (isNumeric(ageStr)==true) {
                int age = Integer.parseInt(ageStr);
                notify = myAge(age);
            }
    
        //Notification about your Age
            System.out.println(notify);
    }
    //Method about Age
    public static String myAge(int age) {
    
        if (age >= 18) {
            return "Over 18 or equals 18";
        }
        else if (age == 0) {
            return "0 age?";
        }
        else if (age <= 18) {
            return "Under 18";
        }
        return null;
    }
    //Method is numeric?
    public static boolean isNumeric(final String str) {
    
        if (str == null || str.length() == 0) {
            return false;
        }
    
        for (char c : str.toCharArray()) {
            if (!Character.isDigit(c)) {
                return false;
            }
        }
    
        return true;
    
    }
    

    }

I hope that can I help you

Regards


Chmoro
  • 3
  • 1
Bruno
  • 1
  • 3
  • "It doesn't work" is not a meaningful problem statement. Please [edit] your question to include the details of *how* your program doesn't work. – azurefrog Sep 24 '19 at 20:13
  • 2
    Your code snippet doesn't show any attempt to use `String age2 = Integer.toString(age)`, which makes it difficult for us to tell what you're doing wrong. – Dawood ibn Kareem Sep 24 '19 at 20:15
  • Use camel case for methods in Java. – arcadeblast77 Sep 24 '19 at 20:21
  • Your problem is not that you can't convert an int to String, your problem is that you don't properly check the input prior reading it as int. I'll link you a question which tells you how to do that. – Tom Sep 24 '19 at 20:22
  • If i just return "Not an age" this happens: Type your age: dsadsa Exception in thread "main" java.util.InputMismatchException at java.base/java.util.Scanner.throwFor(Scanner.java:939) at java.base/java.util.Scanner.next(Scanner.java:1594) at java.base/java.util.Scanner.nextInt(Scanner.java:2258) at java.base/java.util.Scanner.nextInt(Scanner.java:2212) at Prova.Age.AgeFunction(Age.java:14) at Prova.Test.main(Test.java:8) Process finished with exit code 1 – Bruno Sep 24 '19 at 20:23
  • `nextInt` throws exceptions. You can handle them with `try`/`catch`. The docs tell you which exceptions `nextInt` throws and why. https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#nextInt() – arcadeblast77 Sep 24 '19 at 20:26

1 Answers1

0

As you said, you'll currently get an InputMismatchException. You could simply catch this and return your desired value.

Doc for Scanner.nextInt() linked by arcadeblast77

public class Age {

    public String AgeFunction() {

        Scanner sc = new Scanner(System.in);

// User type your age
        try {
            System.out.print("Type your age: ");
            int age = sc.nextInt();

            if (age >= 18) {
                return "Over 18 or equals 18";
            }

            else if (age == 0) {
                return "0 age?";
            }

            else if (age <= 18) {
                return "Under 18";
            }

    // If what was typped be different int, return string


        } catch (InputMismatchException exception) {
            return "Not an age.";
        }

    }

}
DerMolly
  • 345
  • 1
  • 10