Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: Creating a new variable from two others

  1. #1
    Member
    Join Date
    Dec 2013
    Posts
    51
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default Creating a new variable from two others

    Hello, very new... I've just been playing around with a program that's meant to ask the user for their name and age...

    Here's the code :

    import java.util.Scanner;
     
    public class Noerng {
     
    	public static void main(String[] args) {
     
    		Scanner name1 = new Scanner(System.in);
     
    		System.out.println("Enter your first name :");
     
    		String firstName = name1.nextLine();
     
    		Scanner name2 = new Scanner(System.in);
     
    		System.out.println("Enter your second name :");
     
    		String secondName = name2.nextLine();
     
    		System.out.println("hello there " + firstName + " " + secondName);
     
    		String fullName = firstName + " " + secondName;
     
    		Scanner age = new Scanner(System.in);
     
    		System.out.println("How old are you?");
     
    		int userAge = age.nextInt();
     
    		System.out.println("You're " + fullName + ", and you are " + userAge
    				+ " years old.");
     
    		if (userAge < 21) {
    			System.out
    					.println("I don't think you're old enough to drink in America");
    		}
     
    		else {
    			System.out.println("I think you're old enough to drink in America");
    		}
     
    		name1.close();
    		name2.close();
    		age.close();
     
    	}
     
    }

    Would be great to have feedback to anything that's a bit off / not the done thing...

    Is there a way to close all of the scanners at once?

    cheers

    --- Update ---

    not sure why eclipse is formatting line 33 to a different one...


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Creating a new variable from two others

    Is there a way to close all of the scanners at once?
    You only need one.

  3. #3
    Member
    Join Date
    Dec 2013
    Posts
    51
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default Re: Creating a new variable from two others

    Ah right - so all the lines that I've written that create new scanners are wrong... And I should just write

    Scanner age = (System.in)

    ?

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Creating a new variable from two others

    Hmmm, not exactly. Create one Scanner instance with a name that makes sense to you. Something like:

    Scanner input = new Scanner( System.in );

  5. #5
    Member
    Join Date
    Dec 2013
    Posts
    51
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default Re: Creating a new variable from two others

    But looking at the code in my first post - I've created a new scanner for each one, name1, name2, age.....

    Is there a better way that I should have done this?

    cheers

  6. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Creating a new variable from two others

    I thought we covered that already. You only need ONE that you can then use for each item input by the user:

    Scanner input = new Scanner( System.in );
    String name = input.nextLine();
    int age = input.nextInt();
    input.nextLine(); // why did I do this? Try it with/without and see why.
    String address = input.nextLine();

    etc.

  7. The Following User Says Thank You to GregBrannon For This Useful Post:

    Scren (December 29th, 2013)

  8. #7
    Member
    Join Date
    Dec 2013
    Posts
    51
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default Re: Creating a new variable from two others

    Ah makes sense and I instantly feel a bit silly, cheers greg.

  9. #8
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Creating a new variable from two others

    No worries, glad to help.

  10. #9
    Junior Member
    Join Date
    Dec 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Creating a new variable from two others

    hii...
    Instead of using multiple scanners use only one scanner to read all the input values of different formats...
    for example..
    //create Scanner
    Scanner s = new Scanner(System.in);
    //so you can close the scanner once by usnig close() method in Scanner class
    Scanner.close();

  11. #10
    Member
    Join Date
    Dec 2013
    Posts
    51
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default Re: Creating a new variable from two others

    Quote Originally Posted by KotiChowdary View Post
    hii...
    Instead of using multiple scanners use only one scanner to read all the input values of different formats...
    for example..
    //create Scanner
    Scanner s = new Scanner(System.in);
    //so you can close the scanner once by usnig close() method in Scanner class
    Scanner.close();

    Cheers Koti - would it not be

    s.close();

    in that instance?

Similar Threads

  1. Replies: 18
    Last Post: March 30th, 2013, 09:11 AM
  2. What is the difference between instant variable and instance variable?
    By avistein in forum Java Theory & Questions
    Replies: 7
    Last Post: January 6th, 2013, 11:42 PM
  3. Replies: 0
    Last Post: October 30th, 2012, 10:06 AM
  4. For-loop: initialisation of variable, can't set variable to value zero?
    By Bitbot in forum Loops & Control Statements
    Replies: 4
    Last Post: July 15th, 2012, 02:32 PM
  5. Replies: 5
    Last Post: November 16th, 2011, 11:22 AM