I got this code sample from the internet, http://richardbowles.tripod.com/java/menu.htm was able to change the deprecated code to BufferedReader to read in the file, but don't know what's wrong with the errors I'm getting. This is sample code I'm trying to get to work so I can step through with a debugger and see how it works for my own combination multiple choice true false quiz. The author mentions downloading the .txt file, but I can't find it on the site. I used the format shown to make mine. ...maybe that's the problem? Or maybe

We only do command window, no gui for our assignments, and don't read in or write to files, but I want to check this out and shoot for some extra credit on mine.

If you can explain whats going on with these errors I would greatly appreciate it. I've googled for a while, but I'm getting nowhere, and I don't see anything wrong around the lines mentioned.

As always, any help would be greatly appreciated.

//The errors are 
         at java.lang.Integer.parseInt(Unknown Source)
	at java.lang.Integer.parseInt(Unknown Source)
	at bioquiz.load(bioquiz.java:111)
	at bioquiz.init(bioquiz.java:84)
	at sun.applet.AppletPanel.run(Unknown Source)
	at java.lang.Thread.run(Unknown Source)

//the code is


// 
import java.awt.*;
import java.applet.*;
import java.io.*;
import java.net.*;
import java.util.*;

public class bioquiz extends Applet
{ // This variable holds the user's score
   private int score = 0;
   private int mode = 0;   // 0 = answering questions, 1 = viewing results
   private int current = 0;   // Current question being viewed
   private final int numQuestions = 22;
   
   private String[] questions;
   private String[] possible1, possible2, possible3, possible4;
   private String[] revise;
   private int[] choice, correct;

   static final Font textFont=new Font("Helvetica", Font.PLAIN, 14);
   
   private Button nextButton, prevButton, finished;
   private Button opt1, opt2, opt3, opt4;
   private TextField t1, t2, t3, t4;
   private TextArea quest;
   private Panel top;
   
   public void init()
   { setBackground(Color.cyan);
     setFont(textFont);
     prevButton = new Button("Previous Question");
     finished = new Button("Finished");
     nextButton = new Button("Next Question");
     
     top = new Panel();
     top.add(prevButton);
     top.add(finished);
     top.add(nextButton);
     add("North", top);

     opt1 = new Button("1");
     opt2 = new Button("2");
     opt3 = new Button("3");
     opt4 = new Button("4");

     quest = new TextArea(3,35);
     quest.setEditable(false);
     quest.setFont(textFont);
     // quest.setWrap(true);
     
     t1 = new TextField(31);
     t1.setEditable(false);
     t1.setFont(textFont);
     t2 = new TextField(31);
     t2.setFont(textFont);
     t2.setEditable(false);
     t3 = new TextField(31);
     t3.setEditable(false);
     t3.setFont(textFont);
     t4 = new TextField(31);
     t4.setEditable(false);
     t4.setFont(textFont);

     add(quest);
     add(opt1);
     add(t1);
     add(opt2);
     add(t2);
     add(opt3);
     add(t3);
     add(opt4);
     add(t4);
     
    questions = new String[numQuestions];
    possible1 = new String[numQuestions];
    possible2 = new String[numQuestions];
    possible3 = new String[numQuestions];
    possible4 = new String[numQuestions];
    revise = new String[numQuestions];
    correct = new int[numQuestions];
    choice = new int[numQuestions];
    
    load();
   }
   
   // Load text into the text area from a file
   public void load ()
    { URL whereAmI = getCodeBase();
      String temp_s = whereAmI.toString();
      String temp2 = temp_s + "biotext.txt";
      String fileName;
      // If this program is running from a hard disc (as opposed to the web)
      // then the file name will start with "file:/" which the DataInputStream
      // can't seem to cope with, so remove it.
      if (temp2.startsWith("file:/") == true)
       fileName = temp2.substring(6,temp2.length());
     else
       fileName = temp2;     // Otherwise just copy string straight

      try
      { 
        BufferedReader f = new BufferedReader(new FileReader(fileName));
        String line;
        for (int i = 0; i < numQuestions; i++)
        { questions[i] = f.readLine();
           possible1[i] = f.readLine();
           possible2[i] = f.readLine();
           possible3[i] = f.readLine();
           possible4[i] = f.readLine();
           correct[i] = Integer.parseInt(f.readLine());
           revise[i] = f.readLine();
        }
        f.close();
      }
      catch (IOException e)
      {  System.err.println("Error in file " + fileName + ": " + e.toString());
         System.exit(1);
       }
     }

     public void paint (Graphics g)
     { quest.setText(questions[current]);
       wordWrap(40);
       t1.setText(possible1[current]);
       t2.setText(possible2[current]);
       t3.setText(possible3[current]);
       t4.setText(possible4[current]);
       if (choice[current] != 0)
         g.drawString("You chose: "+choice[current],10,245);
       if (mode == 1)
         { if (choice[current] == 0)
             g.drawString("You gave no answer.",10,245);
           g.drawString("The correct answer was "+correct[current],150,245);
           if (choice[current] == correct[current])
             g.drawString("You got this one correct.",10,270);
           else
             g.drawString("You should revise " + revise[current] + ".",5,270);
           g.drawString("Your score is " + score + " out of " + numQuestions,10,300);
         }
     }
     
     public boolean action (Event e, Object arg)
     { if (e.target == prevButton && current > 0)
         { current--;
           repaint();
         }
       if (e.target == finished)
         { mode = 1;  // Indicates no more answers can be added
           // Now determine the score
           score = 0;
           for (int i = 0; i < numQuestions; i++)
             if (choice[i] == correct[i])
               score++;
           repaint();
         }
       if (e.target == nextButton && current < numQuestions-1)
         { current++;
           repaint();
         }
       if (e.target == opt1 && mode == 0)
         { choice[current] = 1;
           repaint();
         }
       if (e.target == opt2 && mode == 0)
         { choice[current] = 2;
           repaint();
         }
       if (e.target == opt3 && mode == 0)
         { choice[current] = 3;
            repaint();
          }
        if (e.target == opt4 && mode == 0)
          { choice[current] = 4;
            repaint();
          }
       return true;
     }

     // Method that word wraps the text in the text area 'quest'.
     // The width of the field is passed as a parameter
     public void wordWrap(int width)
     { String s = quest.getText();   // Copy text into 's' ready to be decimated
       StringTokenizer temp = new StringTokenizer(s," ");    // Sets up tokenized version of text;
       String s2 = "";                        // Adapted version of string , with \n characters, built up here
       int colCount = 0;                   // Count columns since start of string or since last \n character
       int current_s2_length = 0;   // Length of s2 up to last \n character
       while (temp.hasMoreTokens())
         { String nextWd = temp.nextToken();   // Get next word from tokenized text
           if (colCount + 1 + nextWd.length() < width)
             { s2 = s2 + " " + nextWd;                     // If not run out of room, add word on end
               colCount = s2.length() - current_s2_length;;
             }
           else
             { s2 = s2 + "\n" + nextWd;                   // Otherwise move to next line
               current_s2_length = s2.length() - nextWd.length();
               colCount = nextWd.length();            // New column count is number of chars on next line
             }
         }
       quest.setText(s2.trim());                           // Copy the text back into the text area
     }
}
// my text file is named biotext.txt and saved in my bin project file in Ecliplse.
Who wrote Beethoven's Fifth Symphony?
Mozart
Shostakovitch
Beethoven
Schubert
3
Classical music

Who wrote Beethoven's sixth Symphony?
Brahms
Taichofsky
Beethoven
Bach
3
Classical music

the line numbers on this would be error for 111 -> 122 here, and error 84 -> 95 here

How many questions do you have in the file you are reading? Are there 22 or more?

I only had two questions in my file, and I was using a whitespace line between questions while the one at the link Skyelher provided doesn't have any.

Thanks Skyelher! I looked around the site and tried to click on what looked like the link, but it didn't work at the time (probably my browser).

I overwrote my file with this one and ...just like magic ... it works. I put a white space between two questions and it gives me errors the same as my original post.

Thanks again

Glad you solved it. My question was pointing to what I was suspicious. It is not that your file is missing, but it is that you are attempting to parse something that the file/buffer reader cannot return because it has already finished reading the file. The number of loop goes above the file reader can return; as a result 'Unknown Source' is thrown inside the Integer.parseInt(). If the file is missing, it would throw a different Exception.

Thanks for answering. Since I hadn't done any I/O from a file I just wasn't sure what was going on, I figured it was something in my code rather than the file.

It wasn't a link. And it's still not a link.

Well, glad I helped.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.