Results 1 to 2 of 2

Thread: Java Scanner help

  1. #1
    Join Date
    Oct 2013
    Location
    Finland
    Posts
    52
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Question Java Scanner help

    So i was reading the posts on my latest thread (is Eclipse nessesery?) and i downloaded eclipse and have been working on Java for about 2 months now but i ran into a problem, so i want player to input a integer but the Scanner isn't working. Here's the code:
    Code:
    import java.util.Scanner;
    
    public class Character_Creation {
    	public static void main(String[] args) {
    		Scanner name = new Scanner(System.in);
    		Scanner a = new Scanner(System.in);
    	     int age = a.hasNextInt();
    		
    		if (age < 18) {
    			System.out.println("Too young!");
    		}else{
    			System.out.println("Welcome " + name.nextLine());
    		}	
    	}
    
    }

  2. #2
    Join Date
    Mar 2010
    Location
    Florida
    Posts
    512
    Thanks
    9
    Thanked 61 Times in 59 Posts

    Default

    Code:
    import java.util.Scanner;
    
    
    public class Character_Creation {
    
    	public static void main(String[] args) {
    		Scanner scan = new Scanner(System.in);
    		
    		String name;
    		
    		System.out.print("Enter your name: ");
    		name = scan.next();
    		
    		System.out.print("Test this age: ");
    		int age;
    		try{
    			age = scan.nextInt();
    			if (age < 18) {
    				System.out.println("Too young!");
    			}else{
    				System.out.println("Welcome "+name.toString());
    			}
    		}catch(Exception e){
    			System.out.println("User failed to enter a number.");
    		}
    		
    	}
    
    }
    -DW [Deadweight]
    Resolving your thread: First Post: => EDIT => Lower right: => GO ADVANCED => Top Advance Editor drop down: => PREFIX:Resolved

Similar Threads

  1. inline java popup within another java code...
    By bm13084 in forum JavaScript
    Replies: 0
    Last Post: 02-17-2009, 05:05 AM
  2. code to use with java scanner
    By §on21 in forum Looking for such a script or service
    Replies: 1
    Last Post: 11-05-2008, 07:25 PM
  3. Replies: 13
    Last Post: 09-25-2008, 06:21 PM
  4. process scanner
    By boogyman in forum Other
    Replies: 4
    Last Post: 08-10-2006, 03:51 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •