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

Thread: Scanner not working

  1. #1
    Junior Member
    Join Date
    Oct 2020
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Scanner not working

    Hi! It seems like my Scanner for my string is not working in here. I don't have a error but when I run my program, it just skips the scanner and ends the program. Any idea?

    Thanks!

    import java.util.Scanner;
    public class Food {

    public static void main(String[] args) {

    Scanner Keyboard = new Scanner(System.in);

    double pricefruit = 26.99, pricecheese = 22.99, pricedairy = 13.99, pricemeat = 56.99, priceseafood = 38.99;

    System.out.println("Please enter the quantities for each item in the list.");

    System.out.print("Fruits : ");
    int fruits = Keyboard.nextInt();
    System.out.print("Cheese : ");
    int cheese = Keyboard.nextInt();
    System.out.print("Dairy : ");
    int dairy = Keyboard.nextInt();
    System.out.print("Meat : ");
    int meat = Keyboard.nextInt();
    System.out.print("Seafood : ");
    int seafood = Keyboard.nextInt();

    double totalfruits = fruits*pricefruit, totalcheese = cheese*pricecheese, totaldairy = dairy*pricedairy, totalmeat = meat*pricemeat, totalseafood = seafood*priceseafood;

    double totalprice = totalfruits+totalcheese+totaldairy+totalmeat+total seafood;

    System.out.print("Do you have a membership? (Y/N) ");
    RIGHT HERE--->String member = Keyboard.nextLine();

    if(member.equals("Y"))
    {
    if(totalprice < 250)
    System.out.println("The total price is " + Math.round((0.9*(totalprice-totalseafood)+seafood)*100.0)/100.0 + ". You will receive " + Math.round(2*totalprice) + " points." );
    else if(totalprice >= 250 && totalprice <= 500)
    System.out.println("The total price is " + 0.85*(totalprice-totalseafood)+seafood + ". You will receive " + Math.round(2*totalprice) + " points." );
    else if(totalprice > 500)
    System.out.println("The total price is " + 0.8*(totalprice-totalseafood)+seafood + ". You will receive " + Math.round(3*totalprice) + " points." );
    }
    if(member.equals("N"))
    {
    if(totalprice < 250)
    System.out.println("The total price is " + 0.9*(totalprice-totalseafood)+seafood + "." );
    else if(totalprice >= 250 && totalprice <= 500)
    System.out.println("The total price is " + 0.85*(totalprice-totalseafood)+seafood + "." );
    else if(totalprice > 500)
    System.out.println("The total price is " + 0.8*(totalprice-totalseafood)+seafood + "." );
    }
    System.out.println();
    System.out.print("Thanks for shopping! See you next time!");

    (Keyboard).close();
    }

    }
    Last edited by Arno; October 14th, 2020 at 10:36 AM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    24,199
    Thanks
    53
    Thanked 2,626 Times in 2,576 Posts

    Default Re: Scanner not working

    Your issue is probably about using nextInt and nextLine without handling the lineend character properly.
    Read this: https://www.geeksforgeeks.org/why-is...ext-functions/


    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 7
    Last Post: April 25th, 2013, 01:12 PM
  2. Scanner not working?
    By ryno893 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: July 13th, 2012, 08:07 AM
  3. Scanner not working properly.
    By kookevk in forum What's Wrong With My Code?
    Replies: 9
    Last Post: January 25th, 2012, 10:14 PM
  4. Replies: 4
    Last Post: January 27th, 2009, 12:03 AM