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: Scanner

  1. #1
    Member
    Join Date
    Sep 2018
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Scanner

    hello,
    I need to create a Scanner in my code, and I've googled it and it seems like I can open the Scanner utility just fine. But I found out later that you need to close the Scanner, and I can't seem to figure that out. I think the basic setup is --

    import java.util.Scanner;

    body of code

    Scanner keyboard = new Scanner(System.in);
    int productnumber = keyboard.nextInt();

    then more code

    scanner.close();

    The thing is, scanner.close(); never works in Eclipse. It always complains with the error " scanner cannot be resolved" like it thinks scanner is a variable.
    I think because I'm not closing the scanner the code doesn't run correctly.
    Anybody have any ideas?

  2. #2
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    284
    My Mood
    Cool
    Thanks
    0
    Thanked 38 Times in 36 Posts

    Default Re: Scanner

    Try keyboard.close()

    Regards,
    Jim

  3. #3
    Member
    Join Date
    Sep 2018
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Scanner

    thank you so much for the reply Jim, but
    keyboard.close()
    or
    keyboard.close();
    both give the "keyboard cannot be resolved" error.

  4. #4
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    284
    My Mood
    Cool
    Thanks
    0
    Thanked 38 Times in 36 Posts

    Default Re: Scanner

    It works for me. Did you show your exact code? BTW, closing the scanner when the Readable source is System.in is not a good idea because that will close the System.in stream and you will not longer be able to take input, even if you create a new Scanner object. I never close a Scanner in that situation.

    Regards,
    Jim

  5. #5
    Member
    Join Date
    Sep 2018
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Scanner

    okay, actually, I decided to start over, but the Scanner is still my problem. I will include the code, but the bigger problem for me right now is the the integer that I think should be holding me keyboard input isn't keeping the value entered.
    So, this is what I have so far
    this is my main
    [code]
    public class Sales{

    public static void main(String[] args) {

    SalesTest productNumberObject = new SalesTest();
    productNumberObject.askProductNumber();

    SalesTest resultsObject = new SalesTest();
    resultsObject.showResults();

    }
    }
    [\code]

    and this is my constructor

    [code]
    import java.util.Scanner;

    public class SalesTest {

    int getProdNumb;

    public void askProductNumber() {

    Scanner input = new Scanner(System.in);
    System.out.print("Please enter the product number (1 - 5) (0 to stop): ");
    int getProdNumb = input.nextInt();
    }

    public void showResults() {

    System.out.print(getProdNumb);
    }

    }
    [\code]

  6. #6
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    284
    My Mood
    Cool
    Thanks
    0
    Thanked 38 Times in 36 Posts

    Default Re: Scanner

    First, the closing code tag needs a forward, not a backward, slash.

    The problem is that you are creating two SalesTest objects when only one is needed.

    The variable getProdNumb is stored in the first instance of SalesTest but you are trying to access it form the second instance where it has not been initialized.

    Regards,
    Jim

  7. #7
    Member
    Join Date
    Sep 2018
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Scanner

    Thank you!! that did it!
    only one more thing if you don't mind.
    I need my System.out.println statements to be formatted to only have 2 decimal places.
    right now they look like this
    System.out.println("Product 1: " + dollarvalueprod1);
    I keep seeing to use
    System.out.printf("%.2f", val);
    but it doesn't work if I do something like
    System.out.println("Product 1: %.2f" + dollarvalueprod1);
    or
    System.out.println("Product 1: + "%.2f" dollarvalueprod1);

  8. #8
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    284
    My Mood
    Cool
    Thanks
    0
    Thanked 38 Times in 36 Posts

    Default Re: Scanner

    System.out.println() does not permit formatting. If you want a return followed by a newline put a %n after the f (e.g. "%.2f%n"). Note. There are many formatting codes to use with printf. You should have a link to Java 10 API handy. It contains the documentation for all the Java API classes. If you are using a different version of Java you can adjust the URL accordingly.

    Regards,
    Jim

  9. #9
    Member
    Join Date
    Sep 2018
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Scanner

    Thanks again, I wasn't sure about the printf thing, but after some googling I came up with this.
    System.out.print("Product 1: ");
    System.out.printf("%.2f",dollarsmadeprod1);
    System.out.println("");
    System.out.print("Product 2: ");
    System.out.printf("%.2f",dollarsmadeprod2);
    System.out.println("");

    Not sure if it's the most elegant code, but it got the job done! Thank you so much for all your help!!

  10. #10
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    284
    My Mood
    Cool
    Thanks
    0
    Thanked 38 Times in 36 Posts

    Default Re: Scanner

    Like I said before, if you put %n in the format statement, you don't need the extra println() calls.

    Regards,
    Jim

Similar Threads

  1. [SOLVED] Scanner Issues - I think
    By meni in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 11th, 2014, 08:55 AM
  2. Why do I have to create a new Scanner?
    By Currahe in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 18th, 2012, 04:31 PM
  3. [SOLVED] Scanner problem
    By sentimentGX4 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: September 30th, 2012, 06:09 PM
  4. Scanner help
    By sp11k3t3ht3rd in forum Java Theory & Questions
    Replies: 3
    Last Post: June 22nd, 2011, 11:55 AM
  5. Help With Scanner
    By jtphenom in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: October 12th, 2009, 08:49 PM