-1

Ok so im trying to make it so if the person does not want to enter their age the program will print out a different answer. However, when i do this it gives me an error for the string. I used // to make it so the int answer wasnt being played and it worked then. How exactly would I make it so they both work for the same question? I searched for an answer but I couldnt seem to find it so if there is a link for this please link me. Thanks!

   System.out.println("So how old are you?");

   TimeUnit.SECONDS.sleep(2);

   System.out.println("If you dont want to answer you dont have to.   ");

   Scanner scan4 = new Scanner (System.in);
   String user_imput_string1 = scan.nextLine();

   if (user_imput_string1.equals("I dont know")) {
       System.out.println("Ah thats cool. You look great regardless of your age anyway");
   } else {
       System.out.println("Ah thats cool. You look great regardless of your age anyway");
   }
Parth Solanki
  • 2,673
  • 1
  • 16
  • 39
  • @KevinEsche A bit confused with your comment. Are you saying I cant have both a string and an int has seperate variables for one question? –  Oct 14 '16 at 05:37
  • Oh sorry, missread it as you where using a `String` in the second part. [Check this question, it should be related](http://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-nextint-or-other-nextfoo) – SomeJavaGuy Oct 14 '16 at 05:41
  • @NateCraft Hey Nate, it seems like you deleted the second part of your code which had `user_imput_int`. – kbunarjo Oct 14 '16 at 17:30

3 Answers3

1

You would need to convert the String into an int in order to compare the value to 30. However, looking at your code, you seem to have two different variables already, user_imput_string1 and user_imput_int, the latter of which is still a String.

Here is the sample code you could use in order to correctly convert from a String to an int:

int result = Integer.parseInt(user_imput_int);
if (result > 30){
// do whatever
}

Also, as a side note, you are spelling input wrong.

kbunarjo
  • 1,008
  • 1
  • 8
  • 24
  • Thanks a ton man. Also im laughing a bit too much about my inability to spell input –  Oct 14 '16 at 20:22
0

You can do this by catching Exception

public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    System.out.print("So how old are you? ");
    String age = scanner.next(); //Read string by default
    try{
       int actualAge = Integer.parseInt(age);
       //do your stuff with age
    }catch(Exception e){ //Raises NumberFormatException if it's not a number
       //e.printStackTrace();
       System.out.println("Ah thats cool. You look great regardless of your age anyway");
    }
  }
Jyothi Babu Araja
  • 8,434
  • 3
  • 28
  • 37
0

The code is below,I hope it can help you.

        System.out.println("So how old are you?");
        TimeUnit.SECONDS.sleep(2);
        System.out.println("If you dont want to answer you dont have to.   ");
        Scanner scan = new Scanner(System.in);
        String user_imput_int = scan.next();
        if ("I dont know".equals(user_imput_int)) {
            System.out.println("Ah thats cool. You look great regardless of your age anyway");
        } else {
            try {
                int age = Integer.parseInt(user_imput_int);
                if(age > 30)
                {
                    System.out.println("Oh wow you look so good");
                }
                else {
                    System.out.println("Oh thats ok. You look great regardless");
                }
            } catch (Exception e) {
                System.out.println("your input is either 'I dont know' or int number");
            }
        }
Pan
  • 97
  • 3
  • Thanks man. Havent quite gotten to the integer.parseInt in my lessons yet so its good to know =) –  Oct 14 '16 at 20:24