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: While Loops - Error

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question While Loops - Error

    Hi,
    I have been asked to write a program for one of my 1st year modules. im new to programming. Given below is the question. I have answered it partly and there are some errors and its incomplete too. it would be really great if someone could help me to complete it.

    To pass this exercise you must demonstrate that you are able to
    Write a program using WHILE loop constructs.
    Write a program that is well indented.
    Write a program that contains helpful comments.
    Write a program that uses variable names that give an indication of their use.
    Earliest Dates Write a program that repeatedly asks for the user to type in the name of a current film and then asks for its release date. It should stop when the special code QUIT is entered, and name the film that was released first. If several films were released in the earliest year the program should return the name of the last film entered.

    Name a film? INCEPTION
    What year was INCEPTION released? 2010

    Name a film? TANGLED
    What year was TANGLED made? 2011

    Name a film? DIRTY DANCING
    What year was DIRTY DANCING made? 1987

    Name a film? PHILADELPHIA
    What year was PHILADELPHIA made? 1993

    Name a film? QUIT

    The earliest film was Dirty Dancing released in 1987
    This is the code that Iv written so far. I have no idea how to do beyond this.

    mport javax.swing.*; // import the swing library for I/O

    class films
    {
    public static void main (String[] param)
    {

    movies();
    System.exit(0);

    } // END main


    public static void movies()
    {
    String again = "";
    String film;
    int currentyear=2011;
    String year = "";
    String textinput1;
    int oldest year=9999;

    String textinput3;

    while (!(again.equals("QUIT")))
    {

    film = JOptionPane.showInputDialog("Name a film");

    textinput1 = JOptionPane.showInputDialog
    ("What year was" +film+ "released");
    year = Integer.parseInt(textinput1);


    again = JOptionPane.showInputDialog("Name a film");
    }

    System.out.println("The earliest film was");

    if (current year < year){ // i cant seem to sort out the rest of the if statement

    return;


    } // END

    } // END class

    Thanks in advance
    ZombieGurL


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

    Default Re: While Loops - Error

    We don't read minds. Do you think that posting the exact error message might be helpful?
    Improving the world one idiot at a time!

  3. #3
    Junior Member
    Join Date
    Nov 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: While Loops - Error

    the program is incomplete i just need your help to complete it

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

    Default Re: While Loops - Error

    Well that is not going to happen. Try Rent-a-coder.
    Improving the world one idiot at a time!

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

    Default Re: While Loops - Error

    On the other hand if you actually want help instead of committing academic dishonesty then you should ask a sepcific question about a specific problem you are having and I would be more than happy to provide a specific answer.
    Improving the world one idiot at a time!

  6. #6
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: While Loops - Error

    You shouldn't have two variables with the same name.

    The variable year inside the while loop, if it even allows it to be valid at all, which I'm not sure about, as it has the same name as the String variable year, goes out of scope once the loop ends anyway and so you're comparing Strings and integers.

    Actually, on a second inspection, you've assigned a String variable an int value. That'll cause an error.

    Usually you should initialize variables like film and textinput1 before going into the loop.

    Are you trying to show a dialog box?

    JOptionPane.showMessageDialog(null, "The earliest film was");

    Are you trying to show some error message to report if the movie year isn't possible? (Also try also having a block to check to see if it's like before 1900 as they didn't have anything except maybe, and a very small maybe, slides or something, back then.)

    JOptionPane.showMessageDialog(null, "Year " + year + " hasn't even happened yet!!!!!", "Invalid year", JOptionPane.ERROR_MESSAGE);

    Are you trying to figure out a plain message box?

    JOptionPane.showMessageDialog(null, "The earliest film was");

    You are kind of vague on the error but I'm thinking it's related to the two variables with the same name.

Similar Threads

  1. For Loops
    By Shyamz1 in forum Loops & Control Statements
    Replies: 3
    Last Post: September 27th, 2011, 11:54 AM
  2. For loops
    By JCTexas in forum Loops & Control Statements
    Replies: 4
    Last Post: September 21st, 2011, 05:43 PM
  3. help with loops... :(
    By Macgrubber in forum Loops & Control Statements
    Replies: 2
    Last Post: November 2nd, 2010, 12:38 PM
  4. need help with loops plz
    By Kilowog in forum Loops & Control Statements
    Replies: 4
    Last Post: September 28th, 2009, 08:11 AM
  5. Replies: 11
    Last Post: April 29th, 2009, 03:12 AM

Tags for this Thread