Results 1 to 4 of 4
Thread: Help with while loop! ARGHH
- 02-22-2012, 10:55 PM #1
Member
- Join Date
- Feb 2012
- Posts
- 3
- Rep Power
- 0
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!
-
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.
- 02-23-2012, 06:58 AM #3
Member
- Join Date
- Feb 2012
- Posts
- 19
- Rep Power
- 0
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();
}
}
- 02-23-2012, 07:46 AM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,560
- Rep Power
- 11
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
-
Converting a for loop to a do-while loop
By awesom in forum New To JavaReplies: 1Last Post: 11-23-2011, 03:02 PM -
Problem with while loop, assigning a variable with a different value every loop? Help
By JavaProg in forum New To JavaReplies: 2Last Post: 11-07-2011, 02:25 AM -
Is it Possible? Array elements Initialized in Loop, can it be viewed outside loop?
By JPH in forum New To JavaReplies: 1Last Post: 10-01-2011, 02:12 AM -
JTextField loop 2x for-loop WEIRD!
By Streetproject in forum AWT / SwingReplies: 2Last Post: 02-16-2011, 05:46 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks