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: Why won't my program compile? (While loop with inputs)

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

    Question Why won't my program compile? (While loop with inputs)

    import java.util.Scanner;

    public class Factorial {

    public static void main(String[] args) {

    Scanner input = new Scanner(System.in);
    System.out.println("Enter Number ===>> ");

    int factorial;
    while(input>0)
    {
    factorial *= input;
    input--;
    }

    System.out.println(factorial);
    }
    }



    The errors I get are:

    --------------------Configuration: Factorial - JDK version 1.7.0_25 <Default> - <Default>--------------------
    C:\Users\Luke\Documents\AP comp. Sci\Factorial.java:11: error: bad operand types for binary operator '>'
    while(input>0)
    ^
    first type: Scanner
    second type: int
    C:\Users\Luke\Documents\AP comp. Sci\Factorial.java:13: error: bad operand types for binary operator '*'
    factorial *= input;
    ^
    first type: int
    second type: Scanner
    C:\Users\Luke\Documents\AP comp. Sci\Factorial.java:14: error: bad operand type Scanner for unary operator '--'
    input--;


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Why won't my program compile? (While loop with inputs)

    Scanner is an object, not a number. How can it be greater than 0?
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  3. #3
    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: Why won't my program compile? (While loop with inputs)

    Please read the Announcement topic at the top of each/any sub-Forum to learn how to post your code in code tags.

  4. #4
    Member GoodbyeWorld's Avatar
    Join Date
    Jul 2012
    Location
    Hidden command post deep within the bowels of a hidden bunker somewhere under a nondescrip building
    Posts
    161
    My Mood
    Stressed
    Thanks
    14
    Thanked 25 Times in 25 Posts

    Default Re: Why won't my program compile? (While loop with inputs)

    Use the Scanner nextInt() method to get your int variable.

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

    Default Re: Why won't my program compile? (While loop with inputs)

    you have not initialized the factorial and input.
    take the reference of scanner with different name then assign that reference to input with nextLine() method.
    then it will be compiled.

  6. #6
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Why won't my program compile? (While loop with inputs)

    Rubbish!

    Why do they need to do all that instead of following the good advice by GoodbyeWorld?
    Improving the world one idiot at a time!

Similar Threads

  1. Java won't compile
    By headhenchmanhollywood in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 31st, 2013, 09:25 PM
  2. Replies: 8
    Last Post: February 12th, 2013, 05:45 AM
  3. program won't compile, can't get data from google analytics
    By nervous in forum What's Wrong With My Code?
    Replies: 3
    Last Post: June 20th, 2012, 11:57 PM
  4. Code won't compile
    By JavaChallenged in forum What's Wrong With My Code?
    Replies: 6
    Last Post: February 12th, 2012, 08:18 PM
  5. *why won't this compile?*
    By dcshoecousa in forum What's Wrong With My Code?
    Replies: 6
    Last Post: November 2nd, 2010, 07:18 PM

Tags for this Thread