Results 1 to 4 of 4
  1. #1
    William is offline Member
    Join Date
    Feb 2012
    Posts
    3
    Rep Power
    0

    Default Help with while loop! ARGHH

    Hi, been trying this question all day. It is as follows:

    Write a program that repeatedly asks the user to type in the name of a current film and then asks for its release date. It should then stop when the code word "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. The program should run as follows(input is in bold):

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

    Name a film? Tangled
    What year was Tangled released? 2011

    Name a film? Dirty Dancing
    What year was Dirty Dancing released? 1987

    Name a film? Philadelphia
    What year was Philadelphia released? 1993

    Name a film? Quit

    The earliest film was Dirty Dancing released in 1987


    Where can I store the "earliest film" in a variable to use at the end of the program, as it will keep changing each time the while loop is executed. You can't use Arrays either. Any help/code/logic much appreciated!

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Help with while loop! ARGHH

    Simply declare a String and int before the while loop, and declare another set inside the while loop to accept input. If the input int is less than the current min, swap em, simple as that.

  3. #3
    jairoh_ is offline Member
    Join Date
    Feb 2012
    Posts
    23
    Rep Power
    0

    Default Re: Help with while loop! ARGHH

    u can try this.
    import java.util.*;
    public class Movie {
    private static Scanner in = new Scanner(System.in);
    private String filmName="";
    private int filmRelease;
    private int n =0, i=0;
    private String []films= new String [10000];
    private int []dates = new int [10000];
    private int earliestDate;
    private boolean tf;
    private int a;

    public Movie(){
    while(true){
    askAFilm();
    askDateReleased();
    }
    }
    public void askAFilm(){
    System.out.println("Name a Film: ");
    setFilmName(in.nextLine());
    if(getFilmName().equalsIgnoreCase("quit")){
    FirstReleased();
    }
    films[n] = getFilmName();

    }
    private void askDateReleased(){
    try{
    System.out.println("What year was "+getFilmName()+" released? ");
    setFilmRelease(Integer.parseInt(in.nextLine()));
    dates[n] = getFilmRelease();
    }catch(NumberFormatException nfe){
    System.out.println("Invalid date. "+nfe.getMessage());
    askDateReleased();
    }
    n++;
    }
    private void setArrayFilms(String [] array){
    this.films = array;
    }
    private String [] getArrayFilms(){
    return films;
    }
    private void setArrayDates(int [] date){
    this.dates = date;
    }
    private int [] getArrayDates(){
    return dates;
    }
    public void setFilmName(String name){
    this.filmName = name;
    }
    public String getFilmName(){
    return filmName;
    }
    public void setFilmRelease(int date){
    this.filmRelease = date;
    }
    public int getFilmRelease(){
    return filmRelease;
    }
    public void FirstReleased(){
    earliestDate = dates[0];
    for(int x=1; x<dates.length; x++){
    if(dates[x]!=0){
    if(earliestDate>dates[x]){
    earliestDate = dates[x];
    a=x;
    }
    }
    }
    System.out.println("The ealiest film was "+films[a]+" released in "+dates[a]);
    System.exit(0);


    }


    public static void main(String args[]){
    Movie m = new Movie();
    }

    }

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: Help with while loop! ARGHH

    Hi jairoh_. Posting code solutions is frowned on here. And with good reason, I think: we don't want to take the fun and learning out of it for the original poster.

    That said, suggestions, ideas, thoughts etc are always welcome.

    General points that I try to follow are:

    * use the code tags and other formatting. See bbcode faq
    * compile and test code that presents itself as complete (ie is not obviously pseudocode). Or, at least, indicate where code is untried
    * read - and think about! - the previous answers. Fubarable has suggested an approach that does *not* need an array, which ought to lead one to stop and think before suggesting another that involves a couple of arrays (especially when it does not meet the OP's stated objective because of the limit on the size of the array.)

Similar Threads

  1. Converting a for loop to a do-while loop
    By awesom in forum New To Java
    Replies: 1
    Last Post: 11-23-2011, 04:02 PM
  2. Replies: 2
    Last Post: 11-07-2011, 03:25 AM
  3. Replies: 1
    Last Post: 10-01-2011, 03:12 AM
  4. JTextField loop 2x for-loop WEIRD!
    By Streetproject in forum AWT / Swing
    Replies: 2
    Last Post: 02-16-2011, 06:46 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •