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

Thread: How do i user a Scanner that was imported in another Class? (student, beginner)

  1. #1
    Junior Member
    Join Date
    Dec 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How do i user a Scanner that was imported in another Class? (student, beginner)

    Hi, first year student here, first post!

    I've attached the diagram of the program in question, question is: in the CharacterHolder class I am required to get input, but the Scanner is the PokemonBattler class.
    (specifically, input needs to happen in the UserPicksCharacterFromHand() method)

    We are required to use the Scanner imported in the PokemonBatter Class, but i cant find any specific examples in my notes on how to do this.

    the program is complete and currently working with an instance of Scanner being imported from inside the CharacterHolder class.

    I hope this is enough information.



    Pokemon_UML.jpg
    Attached Images Attached Images


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,661
    Thanks
    45
    Thanked 1,960 Times in 1,934 Posts

    Default Re: How do i user a Scanner that was imported in another Class? (student, beginner)

    Please post some code that shows the problem.

    If you have defined a variable or method in one class at the class level and want to access it in another class, you need a reference to the class that contains the desired item and then can use that reference to access them.
    refToClassWithItem.theItem;
    refToClassWithItem.theMethod();
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Dec 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How do i user a Scanner that was imported in another Class? (student, beginner)

    Yes! thank you!

    all i had to do was change:

    int number = in.nextInt();

    to:
    int number = PokemonBattler.in.nextInt();

    Sorry for not posting code in the first place, but thanks for the reply!

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,661
    Thanks
    45
    Thanked 1,960 Times in 1,934 Posts

    Default Re: How do i user a Scanner that was imported in another Class? (student, beginner)

    Awkward and not recommended syntax. Pass a reference to in to the class that wants to use it instead of having a static variable.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Dec 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How do i user a Scanner that was imported in another Class? (student, beginner)

    Thank you for the reply again, I'm not sure if I understand what you mean though? Could you give me an example of what you are describing? I can post more code, but I'm not sure what would help.

    Currently what is happening is:

    The class with the main method contains -
    import java.util.Scanner;
    public class PokemonBattler
    {
    public static Scanner in = new Scanner(System.in);
    }

    and the class calling it is doing something like -
    System.out.print("\n\t\tYou have the following pokemon:\n ");
            for (int i = 0; i < characters.length; i++)
            {
                System.out.print("\n" + (i + 1) + ". " + characters[i].toString());
            }
     
            do
            {
                System.out.print("\n\n\tEnter your choice: ");
                boolean isInt = PokemonBattler.in.hasNextInt();

Similar Threads

  1. Using the Scanner class
    By Brocco767 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 26th, 2012, 06:47 PM
  2. Beginner Java Student Having Some Trouble
    By csprung in forum Loops & Control Statements
    Replies: 1
    Last Post: April 25th, 2012, 08:14 AM
  3. Reading user input from the console with the Scanner class
    By JavaPF in forum Java SE API Tutorials
    Replies: 3
    Last Post: September 7th, 2011, 03:09 AM
  4. Imported Class of Components are Hidden
    By polaris395 in forum AWT / Java Swing
    Replies: 3
    Last Post: July 25th, 2011, 09:35 AM
  5. Reading user input from the console with the Scanner class
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 0
    Last Post: November 11th, 2008, 01:47 PM