Author Topic: Any java programmers? Simple question i have...  (Read 7678 times)

0 Members and 1 Guest are viewing this topic.

Offline tjanuranus

  • Posts: 2234
  • Gender: Male
Re: Any java programmers? Simple question i have...
« Reply #35 on: May 03, 2011, 09:36:00 PM »
ok one more. This is pissing me off. :censored
Code: [Select]
import java.util.Scanner;

class DriversExam
{
private String[] corAns = {"B", "D", "A", "A", "C", "A",
"B", "A", "C", "D", "B", "C",
"D", "A", "D", "C", "C", "B",
"D", "A"};

//STORE THE ANSWERS
private String[] userAns;
int[] missedAnswers = new int[corAns.length];

//process the user answers
public DriversExam(String[] a)
{
userAns = new String[a.length];

for (int i = 0; i < a.length; i++)
{
userAns[i] = a[i];
}
}
public DriversExam()
{

}
//returns boolean value if correct > 15
public boolean passed()
{
if (totalCorrect() >= 15)
return true;
else
return false;
}
//total correct answers
public int totalCorrect()
{
int correctCount = 0;

for (int i = 0; i < corAns.length; i++)
{
if(userAns[i].equalsIgnoreCase(corAns[i]))
{
correctCount++;
}
}
return correctCount;
}
//total incorrect answered
public int totalIncorrect()
{
int wrong = 0;

for (int i = 0; i < corAns.length; i++)
{

if (!userAns[i].equalsIgnoreCase(corAns[i]))
{
missedAnswers[wrong] = i;
wrong++;
}
}
return wrong;
}
//Missed questions
public int[] questionsMissed()
{
return missedAnswers;
}
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);

System.out.println("Drivers Liscense Exam.");
System.out.println("20 multiple choice questions.");

//Inputting String
String[] answers = new String[20];
String answer;

for (int i = 0; i < 20; i++)
{
do
{
System.out.println((i +1) + ": ");
answer = keyboard.nextLine();

}while (!isValid(answer));
answers[i] = answer;
}
//Process
DriversExam drivers = new DriversExam(answers);

//Result
System.out.println("Totcal Correct: " + drivers.totalIncorrect());
System.out.println("Totcal Incorrect: " + drivers.totalIncorrect());

if (drivers.totalIncorrect() > 0)
{
System.out.println("The incorrect answers are: ");
int missedIndex;
for (int i = 0; i < drivers.totalIncorrect(); i++)
{
missedIndex = drivers.questionsMissed()[i] + 1;
System.out.println(" " + missedIndex);
}
}
DriversExam pass = new DriversExam();
if (pass.passed() == true)
{
System.out.println("You passed.");
}
else
System.out.println("You failed.");
}
public static boolean isValid(String a)
{
return "A".equalsIgnoreCase(a) || "B".equalsIgnoreCase(a) ||
"C".equalsIgnoreCase(a) || "D".equalsIgnoreCase(a);
}
}

Offline tjanuranus

  • Posts: 2234
  • Gender: Male
Re: Any java programmers? Simple question i have...
« Reply #36 on: May 03, 2011, 09:37:19 PM »
error messages are after i enter the 20 letters..

Exception in thread "main" java.lang.NullPointerException
   at DriversExam.totalCorrect(DriversExam.java:43)
   at DriversExam.passed(DriversExam.java:31)
   at DriversExam.main(DriversExam.java:109)