Results 1 to 10 of 10
  1. #1
    mathidioticz is offline Member
    Join Date
    Jan 2012
    Posts
    48
    Rep Power
    0

    Default ArrayList problem

    I am experiencing problems in my ArrayList and there is a red curly line under add in line 69
    And i don't really understand where i got wrong.


    Java Code:
    package HealthOK.ui;
    
    import javax.swing.JPanel;
    import java.awt.Color;
    import java.awt.Dimension;
    import javax.swing.JLabel;
    import java.awt.Rectangle;
    import javax.swing.ImageIcon;
    import java.awt.Font;
    import javax.swing.JFrame;
    import javax.swing.JOptionPane;
    import javax.swing.SwingConstants;
    import javax.swing.JButton;
    import java.awt.Point;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.Timer;
    import HealthOK.ui.TimerModel;
    import HealthOK.entity.ForumAnswer;
    import HealthOK.entity.GameQuestion;
    import OOPJ.database.DBController;
    import OOPJ.database.DBController;
    import java.sql.ResultSet;
    import java.util.ArrayList;
    
    
    public class GamesQuestionPanel extends JPanel{
    	
    	private static final long serialVersionUID = 1L;
    	private JLabel jLabelQuestion = null;
    	private JLabel jLabelScore = null;
    	private JLabel jLabelHeart1 = null;
    	private JLabel jLabelHeart2 = null;
    	private JLabel jLabelHeart3 = null;
    	private JButton jButtonAnswer1;
    	private JButton jButtonAnswer2;
    	private JButton jButtonAnswer3;
    	private JLabel jLabelDescription1;
    	private JLabel jLabelDescription2;
    	private JLabel jLabelDescription3;
    	private JButton jButtonAdd = null;
    	private JFrame myFrame = null;
    	private JLabel jLabelTimer = null;
    	private Timer t;
    	private TimerModel tm;
    	private int qn_num = 0;
    	private JLabel jLabelScore1 = null;
    	private JLabel jLabelQn;
    	private JButton jButtonEdit = null;
    	private static int g_correctAnswer;
    	private static String ans;
    	int Score = 0;
    	
    	public void Randomise() {
    		ArrayList<GameQuestion> Random = new ArrayList<GameQuestion>();
    		try{
    			DBController db = new DBController();
    			// passing data source name setup
    			db.setUp("CFDatabase");
    			String g_question="General";
    			String dbQuery = "SELECT * FROM GameQuestion WHERE g_question ='" + g_question+ "'";
    			
    			// for retrieve SQL use readRequest method
    			ResultSet rs = db.readRequest(dbQuery);
    
    		if (rs.next()){
    			int id = rs.getInt("g_questionNo");
    			Random.add(id);  //here is line 69, the offending code. 
    			System.out.println(Random.get(id));
    			}
    			
    			}catch (Exception e) {
    				   e.printStackTrace();
    			}
    		;}
    
    	public void DisplayQuestion(){
    		
    	}
    
    	
    	/**
    	 * This is the default constructor
    	 */
    	public GamesQuestionPanel() {
    		super();
    		initialize();
    	}
    
    	public GamesQuestionPanel(JFrame f) {
    		this();
    		myFrame = f;
    	}
    	
    	public GamesQuestionPanel(JFrame f, String s ){
    		this();
    		myFrame = f;
    		jLabelTimer.setText(s);
    	}
    Last edited by mathidioticz; 02-04-2012 at 06:32 AM.

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

  3. #3
    mathidioticz is offline Member
    Join Date
    Jan 2012
    Posts
    48
    Rep Power
    0

    Default Re: ArrayList problem

    I had re-edit my post and add a comment message to that particular line.

    I re-paste the code here again.

    Java Code:
     
    Random.add(id);  //here is line 69, the offending code.
    there is a red curly line under the word "add". But I don't understand why the word "add" is wrong and shows the curly lines.

  4. #4
    mathidioticz is offline Member
    Join Date
    Jan 2012
    Posts
    48
    Rep Power
    0

    Default Re: ArrayList problem

    This is line 69:
    Java Code:
     Random.add(id);
    There is no error message shown but there is a red curly line under the word "add"
    i believe that there is something wrong with code.

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

    Default Re: ArrayList problem

    OK, that's an ArrayList of GameQuestion objects. What are you trying to add to the ArrayList on that line? What type is id?

  6. #6
    mathidioticz is offline Member
    Join Date
    Jan 2012
    Posts
    48
    Rep Power
    0

    Default Re: ArrayList problem

    Actually I am trying to do a randomise code to randomise my game questions so that everytime the user play the game, the questions will not be in the same order. My id is a number which is also an integer. Sorry if i answered your question wrongly, i just started java and do not really understand.

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

    Default Re: ArrayList problem

    The answer I was trying to elicit from you is that id is an int. The point I am trying to make is that you have an ArrayList<String> or an ArrayList of String. This array list can hold String objects and only String objects. On your offending line of code, you try to add an int to an ArrayList that only accepts String, and the compiler is telling you that this is illegal to do. Not only is it illegal, but it just doesn't make sense. If the ArrayList is supposed to hold questions, why would you want it to hold a simple int number?

  8. #8
    mathidioticz is offline Member
    Join Date
    Jan 2012
    Posts
    48
    Rep Power
    0

    Default Re: ArrayList problem

    So you mean that i can use my game question to do the randomise instead of using the id?

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

    Default Re: ArrayList problem

    Quote Originally Posted by mathidioticz View Post
    So you mean that i can use my game question to do the randomise instead of using the id?
    If your goal is to get a random String out of an ArrayList (not put on in the ArrayList), you can either use java.util.Arrays.shuffle(myArrayList) to shuffle all the Strings held by the list or you could use a Random object to get a random index. For e.g., if the Random variable is named myRandom:

    Java Code:
    String randomString = myArrayList.get(myRandom.nextInt(myArrayList.size()));

  10. #10
    mathidioticz is offline Member
    Join Date
    Jan 2012
    Posts
    48
    Rep Power
    0

    Default Re: ArrayList problem

    ThankYou for your help! i understand what are you trying to say already. ThankYou!! :D

Similar Threads

  1. Replies: 12
    Last Post: 07-07-2011, 09:49 PM
  2. Problem- arraylist
    By whizzz in forum New To Java
    Replies: 3
    Last Post: 12-05-2010, 02:13 AM
  3. ArrayList problem.
    By keo in forum New To Java
    Replies: 4
    Last Post: 10-15-2010, 12:22 PM
  4. ArrayList problem
    By brian.baert in forum New To Java
    Replies: 8
    Last Post: 03-06-2010, 02:49 PM
  5. Hey! ArrayList problem here
    By Samgetsmoney in forum New To Java
    Replies: 31
    Last Post: 02-20-2009, 01:39 AM

Posting Permissions

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