Jump to content
Sign in to follow this  
gongpex

proble with arrayList

Recommended Posts

Hello everyone,

 

long time no see,

 

 

I think this will more easily if you ever read Head First Java book,

 

When I tried code on that book on page 450 (chapter 14) about QuizCard builder.

 

this the code :

import  java.util.*;import  java.awt.event.*;import  javax.swing.*;import  java.awt.*;import  java.io.*;  public class QuizCardBuilder {     private JTextArea question;     private JTextArea answer;     private ArrayList<QuizCard> cardList;     private JFrame frame;     public static void main(String[] args)  {        QuizCardBuilder builder = new QuizCardBuilder();        builder.go();     }    public void go() {      //build GUI         frame = new JFrame("Quiz Card Builder");      JPanel mainPanel = new JPanel();      Font bigFont = new Font("sanserif", Font.BOLD, 24);      question = new JTextArea(6,20);      question.setLineWrap(true);      question.setWrapStyleWord(true);      question.setFont(bigFont);      JScrollPane qScroller = new JScrollPane(question);      qScroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);      qScroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);            answer = new JTextArea(6,20);      answer.setLineWrap(true);      answer.setWrapStyleWord(true);      answer.setFont(bigFont);        JScrollPane aScroller = new JScrollPane(answer);      aScroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);      aScroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);      JButton nextButton = new JButton("Next Card");         cardList = new ArrayList<QuizCard>();      JLabel qLabel = new JLabel("Question :");      JLabel aLabel = new JLabel("Answer : ");      mainPanel.add(qLabel);      mainPanel.add(qScroller);      mainPanel.add(aLabel);      mainPanel.add(aScroller);      mainPanel.add(nextButton);        nextButton.addActionListener(new NextCardListener());      JMenuBar menuBar = new JMenuBar();      JMenu fileMenu = new JMenu("File");      JMenuItem newMenuItem = new JMenuItem("New");      JMenuItem saveMenuItem = new JMenuItem("Save");      newMenuItem.addActionListener(new NewMenuListener());            saveMenuItem.addActionListener(new SaveMenuListener());      fileMenu.add(newMenuItem);      fileMenu.add(saveMenuItem);      menuBar.add(fileMenu);      frame.setJMenuBar(menuBar);      frame.getContentPane().add(BorderLayout.CENTER, mainPanel);      frame.setSize(500,600);      frame.setVisible(true);    }       public class NextCardListener implements ActionListener {      public void actionPerformed(ActionEvent ev) {        QuizCard card = new QuizCard(question.getText(), answer.getText());        cardList.add(card);        clearCard();     }   }    public class SaveMenuListener implements ActionListener {       public void actionPerformed(ActionEvent ev) {        QuizCard card = new QuizCard(question.getText(), answer.getText());        cardList.add(card);                 JFleChooser fileSave = new JFileChooser();        fileSave.showSaveDialog(frame);        saveFile(fileSave.getSelectedFile());     }   }   public class NewMenuListener implements ActionListener {       public void actionPerformed(ActionEvent ev) {        cardList.clear();        clearCard();      }   }  private void clearCard() {     question.setText("");     answer.setText("");     question.requestFocus();  }  private void saveFile(File file) {    try {         BufferedWriter writer = new BufferedWriter(new FileWriter(file));        for(QuizCard card:cardList) {            writer.write(card.getQuestion() + "/");            writer.write(card.getAnswer() + "n");        }     writer.close();     } catch(IOException ex) {       System.out.println("couldn't write the cardList out");       ex.printStackTrace();   }  }}

the issue is start from : private ArrayList<QuizCard> cardList; .

 

when I see it using eclipse, it shown : "QuizCard cannot be resolved to a type" and can't compile.

 

Q : what's mistake on this code?

 

please someone help me

 

thanks

 

Share this post


Link to post
Share on other sites

I believe you need to import the QuizCard class.

Share this post


Link to post
Share on other sites

Pretty straight forward.

 

You are missing the class QuizCard:

 

You are creating an instance in here:

 

QuizCard card = new QuizCard(question.getText(), answer.getText());

 

Of course you need to have that class in the project.

public class QuizCard{	String question,answer;	public QuizCard(){}	public QuizCard(String q,String a){		question=q;		answer=a;	}	public String getQuestion(){return question;}	public String getAnswer(){return answer;} }

Result:

 

dzeZgAd.png

Edited by panodil

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...