Results 1 to 4 of 4
  1. #1
    diwakardelhi is offline Member
    Join Date
    Nov 2009
    Posts
    7
    Rep Power
    0

    Default challenging error

    Java Code:
    import javax.swing.JFrame;
    import javax.swing.JOptionPane;
    import javax.swing.JButton;
    import javax.swing.Icon;
    import javax.swing.ImageIcon;
    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    public class class2 extends JFrame{
    	JButton movie1;
    	JButton movie2;
    	JButton movie3;
    	JButton movie4;
    	JButton result;
    	public String nom;
    	static int chk=0;
    	
    	public class2(){
    		super("Movie Name Guess Game...");
    		setLayout(new FlowLayout());
    		
    		Icon a =  new ImageIcon(getClass().getResource("a.png"));
    		Icon b =  new ImageIcon(getClass().getResource("b.png"));
    		Icon c =  new ImageIcon(getClass().getResource("c.png"));
    		Icon d =  new ImageIcon(getClass().getResource("d.png"));
    		Icon e =  new ImageIcon(getClass().getResource("e.png"));
    		Icon f =  new ImageIcon(getClass().getResource("f.png"));
    		Icon g =  new ImageIcon(getClass().getResource("g.png"));
    		Icon h =  new ImageIcon(getClass().getResource("h.png"));
    		
    		movie1 = new JButton(a);
    		movie2 = new JButton(b);
    		movie3 = new JButton(c);
    		movie4 = new JButton(d);
    		result = new JButton("RESULT");
    		movie1.setRolloverIcon(e);
    		movie2.setRolloverIcon(f);
    		movie3.setRolloverIcon(g);
    		movie4.setRolloverIcon(h);
    		add(movie1);
    		add(movie2);
    		add(movie3);
    		add(movie4);
    		add(result);
    		thehandler handler = new thehandler();
    		movie1.addActionListener(handler);
    		movie2.addActionListener(handler);
    		movie3.addActionListener(handler);
    		movie4.addActionListener(handler);
    		result.addActionListener(handler);
    	}
    	
    	public class thehandler implements ActionListener{
    		public void actionPerformed(ActionEvent event){
    		[COLOR="Teal"]if(event.getSource()==movie1){
    nom = JOptionPane.showInputDialog("Guess the name of this awesome movie about a struggle of a man.");
    if(nom == "Pursuit of Happyness" || nom == "pursuit of happyness" || nom == "PURSUIT OF HAPPYNESS")
    increasechk();
    }[/COLOR]
    			else if(event.getSource()==movie2){
    				nom = JOptionPane.showInputDialog("Guess the name of this amazing story of a thief.");
    				if(nom == "Oye Lucky Lucky Oye" || nom== "oye lucky luck oye" || nom == "OYE LUCKY LUCKY OYE" || nom =="OLLO" || nom == "ollo")
    					increasechk();
    				}
    			[COLOR="Red"]else if(event.getSource()==movie3){
    				nom = JOptionPane.showInputDialog("Guess the name of this all time superhit Shahrukh Khan.");
    				if(nom == "DDLJ" || nom == "ddlj" || nom == "Dilwale Dulhania Le Jayenge")
    					increasechk();
    				}[/COLOR]
    			[COLOR="Magenta"]else if(event.getSource()==movie4){
    				nom = JOptionPane.showInputDialog("Guess the name of this all time favourite comedy movie.");
    				if(nom == "Andaz Apna Apna" || nom == "ANDAZ APNA APNA" || nom == "Andaaz Apna Apna" || nom == "andaaz apna apna" || nom == "andaz apna apna" )
    					increasechk();
    				}[/COLOR]
    			[COLOR="DimGray"]else if(event.getSource()==result)
    				checkresult();[/COLOR]
    		}
    	}
    	public static void increasechk(){
    		chk++;
    	}
    	
    	public static void checkresult(){
    		if(chk == 4)
    			JOptionPane.showMessageDialog(null,"U Rock man. All Correct. U hava a good taste of movies.");
    		if(chk == 3)
    			JOptionPane.showMessageDialog(null,"Good man. 3 correct answers. Good Going.");
    		if(chk == 2)
    			JOptionPane.showMessageDialog(null,"Average Performance. U will die as an average person.");
    		if(chk == 1)
    			JOptionPane.showMessageDialog(null,"Only one correct. Thats bad. Watch all of these man.");
    		if(chk == 0)
    			JOptionPane.showMessageDialog(null,"Thats really poor. U dont deserve to live. Start watching some good movies.");
    	}
    }
    Moderator: code tags added
    Last edited by Fubarable; 11-15-2009 at 10:30 PM.

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

    Default

    You almost never compare Strings using == as this compares whether one String variable holds the same object is another, and you don't want to know this. Instead you want to know if the String object held by a String variable holds the same string characters as another, and this is done using the equals or equalsIgnoreCase methods.

    For instance, don't do this:
    Java Code:
    if (nom == "Pursuit of Happyness" || nom == "pursuit of happyness"
                || nom == "PURSUIT OF HAPPYNESS")
    but instead do:
    Java Code:
    if (nom.equalsIgnoreCase("pursuit of happyness"))
    Finally, please see my signature about using code tags when posting code. I've taken the liberty of adding them to your posts above.

  3. #3
    diwakardelhi is offline Member
    Join Date
    Nov 2009
    Posts
    7
    Rep Power
    0

    Default

    thanx bro..actually i m new to this site. just read this info nd i have transferred it to the appropriate thread also..
    thanx for the reply...

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

    Default

    I've frozen your other thread and sent you a private message. We prefer not to have discussions fragmented amongst more than one thread as it's not fair to the volunteers who help here.

Similar Threads

  1. non preemptive scheduling.....challenging!!
    By snehil2009 in forum New To Java
    Replies: 5
    Last Post: 11-10-2009, 10:23 AM
  2. Replies: 1
    Last Post: 10-26-2009, 05:38 PM
  3. Replies: 3
    Last Post: 01-23-2009, 09:53 AM
  4. challenging problems
    By jayant3001 in forum JCreator
    Replies: 24
    Last Post: 10-30-2008, 06:41 AM
  5. Replies: 24
    Last Post: 07-29-2008, 05:04 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
  •