-7
package kappa;
import java.util.Scanner;

public class Kappa {


    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        System.out.println("Who are you ?");
        String Name;
        Name = scanner.next();

        System.out.println("So you are " + Name);

       String any;
       System.out.println("Press any to continue");
       any = scanner.next();

      int age;
       System.out.println("Enter you age : " );
        age = Integer.parseInt();

       if(age > 18){
       System.out.println(Name + " is older than 18");

       }
       if (age < 18){
       System.out.println(Name + " is younger than 18");
       }
       }


    }

// i dont how to convert string to int and i know this is an easy problem but im just new to java language so any help will be really helpful.

Madhawa Priyashantha
  • 9,633
  • 7
  • 33
  • 60

3 Answers3

1

You can use int i = Integer.parseInt(stringVariable); But after looking at your code I found some issues and suggestions:

  1. age = Integer.parseInt(); // Not valid.

if you want to achieve that then you need to use int age = scanner.nextInt();

  1. You should ideally use variable name starting with small case and then go for camelcase. Example : String name, String firstName, etc.

I hope this helps.

Suyash
  • 525
  • 1
  • 6
  • 10
1

Change age = Integer.parseInt(); to age = scanner.nextInt();

Debosmit Ray
  • 5,228
  • 2
  • 27
  • 43
0

You may go through this link

Other than that you can scanner.nextInt(); but before taking next int you have use scanner.next() to emty the scanner .As scanner has not been read completely yet.Then again use scanner.nextInt() to get new value.

Also need try catch for scanner.nextInt() as this may throw error in case of int not found.

Community
  • 1
  • 1