— Scott McNealy, Sun Microsystems Chairman, President and ceo



Download 40,57 Mb.
Pdf ko'rish
bet422/555
Sana26.05.2022
Hajmi40,57 Mb.
#609235
1   ...   418   419   420   421   422   423   424   425   ...   555
Bog'liq
Head First Java (Kathy Sierra, Bert Bates) (z-lib.org)

serialization 
and
file I/O
you are here
4
457
public 
class NextCardListener
implements ActionListener {
public void actionPerformed(ActionEvent ev) {
if (isShowAnswer) {
// show the answer because they’ve seen the question
display.setText(currentCard.getAnswer());
nextButton.setText(“Next Card”);
isShowAnswer = false;
} else {
// show the next question
if (currentCardIndex < cardList.size()) {
showNextCard();
} else {
// there are no more cards!
display.setText(“That was last card”);
nextButton.setEnabled(false);
}
}


public 
class OpenMenuListener
implements ActionListener {
public void actionPerformed(ActionEvent ev) {
JFileChooser fileOpen = new JFileChooser();
fileOpen.showOpenDialog(frame);
loadFile(fileOpen.getSelectedFile());
}
}
private void 
loadFile(File file)
{
cardList = new ArrayList();
try {
BufferedReader reader = new BufferedReader(new FileReader(file));
String line = null;
while ((line = reader.readLine()) != null) {
makeCard(line);
}
reader.close();
} catch(Exception ex) {
System.out.println(“couldn’t read the card file”);
ex.printStackTrace();
}
// now time to start by showing the first card
showNextCard();
}
private void 
makeCard(String lineToParse)
{
String[] result = lineToParse.split(“/”);
QuizCard card = new QuizCard(result[0], result[1]);
cardList.add(card);
System.out.println(“made a card”);
}
private void 
showNextCard()
{
currentCard = cardList.get(currentCardIndex);
currentCardIndex++;
display.setText(currentCard.getQuestion());
nextButton.setText(“Show Answer”);
isShowAnswer = true;
}
} // close class
Check the isShowAnswer boolean flag to 
see if they’re currently viewing a question 
or an answer, and do the appropriate 
thing depending on the answer.
Bring up the file dialog box and let them 
navigate to and choose the file to open.
Make a BufferedReader chained 
to a new FileReader, giving the 
FileReader the File object the user 
chose from the open file dialog.
Read a line at a time, passing the 
line to the makeCard() method 
that parses it and turns it into a 
real QuizCard and adds it to the 
ArrayList.
Each line of text corresponds to a single 
flashcard, but we have to parse out the 
question and answer as separate pieces. We 
use the String split() method to break the 
line into two tokens (one for the question 
and one for the answer). We’ll look at the 
split() method on the next page.



Download 40,57 Mb.

Do'stlaringiz bilan baham:
1   ...   418   419   420   421   422   423   424   425   ...   555




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish