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 6 of 6

Thread: What gives?

  1. #1
    Junior Member
    Join Date
    May 2012
    Posts
    12
    My Mood
    Confused
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Lightbulb What gives?

    Hello, I am trying to get to grips with Java and specifically at this moment with text input, I wrote this with some help from Dr Google:

    package stringvars;
     
    import java.util.Scanner;
     
    public class StringVariables {
     
      public static void main(String[] args) {
     
      	  Scanner user_input = new Scanner(System.in);
     
      	  String first_name;
      	  System.out.print("Enter your first name: ");
      	  first_name = user_input.next();
     
      	  String family_name;
      	  System.out.print("Enter family name: ");
      	  family_name = user_input.next();
     
      	  String full_name;
      	  full_name = first_name + " " + family_name;
     
      	  System.out.println("You are " + full_name);
      }
    }

    It did not work…. So I tinkered with it and low and behold, when I removed the top line (package stringvars it worked ☺

    So – my question is why?

    Thanks in advance for any help – Ian ☺

  2. #2
    Junior Member
    Join Date
    May 2013
    Location
    Charleston SC
    Posts
    21
    Thanks
    0
    Thanked 8 Times in 7 Posts

    Default Re: What gives?

    It works with package stringvars; if that is the true name of your package. My only thought is the name of your package was incorrect otherwise it should have run just fine

  3. #3
    Junior Member
    Join Date
    May 2012
    Posts
    12
    My Mood
    Confused
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: What gives?

    Okay - so... I did some digging around and it seams that the Package command is something i would use if I wanted to create a package of classes to use in my program, is this correct? If so - is this something that is becoming obsolete with newer versions of Java? Obviously my version (7.3) didn't need it...

    Ian

  4. #4
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: What gives?

    A package is just a folder in the hierarchy used for organization.
    It is not necessary to organize related classes in different packages, you can dump everything into one big pile if you choose to.
    As things get more complicated you will be glad to use them.

  5. The Following User Says Thank You to jps For This Useful Post:

    iansmiler (May 24th, 2013)

  6. #5
    Junior Member
    Join Date
    May 2012
    Posts
    12
    My Mood
    Confused
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: What gives?

    So.... It is a folder that I create and put whatever classes I require into - hence why the program didn't work originally because I had not created that folder, makes sense now

    A couple more questions then:

    1. Does the import command pull from the packages folder
    2. As I already stated when I removed the top line of code (package stringvars) the program worked... so in this instance, where did the command import pull from?

    Thanks - Ian

  7. #6
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: What gives?

    The import command "pulls from the package" named in the statement.
    Take for example the Math class. Officially named java.lang.Math
    This tells you the class named Math is in the package named lang which happens to reside in a higher package of the hierarchy named java. The compiler must know where to find the java package to get the ball rolling, this is set up differently based on the development environment.

    Drop that class into a folder with the package name and it will be fine, or even create your own new package name, or when you have none at all, it is considered to be in the default package, which is why it works without the statement.

  8. The Following User Says Thank You to jps For This Useful Post:

    iansmiler (May 24th, 2013)