9 Replies - 4313 Views - Last Post: 09 March 2011 - 09:08 PM Rate Topic: -----

#1 brdstnstrnglr   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 23-February 11

Java License Exam Problem with Methods and Validation

Posted 09 March 2011 - 10:02 AM

Another Homework Problem, This is supposed to be a license Exam where correct answers are held in an array and student answers are put into another array. The issues I have now are A. Input validation doesn't seem to be working (must accept only a,b,c,d answers). B. Methods I am calling to do array comparison and then calculation don't seem to be working (method 1 does a test and determines pass/fail; method 2 determines number of correct answers; method 3 determines number of wrong answers; and method 4 tries to use an array list to find out the number of the questions missed and print them out). Any help that anyone could provide for any of these issues would be greatly appreciated.

import java.util.*;
import javax.swing.*;
//
public class DriverExam 
{
static boolean arrayEquals = true;	

public static void main(String[] args)
{

//Variables
String[] correctAnswer = {"B", "D", "A", "A", "C", "A", "B", "A", "C", "D", "B", "C", "D", "A", "D",
									"C", "C", "B", "D", "A"};
String[] studentAnswer = new String[20];
int i;
Scanner imput = new Scanner(System.in);

//Gathers Student Answers		
for (i = 0; i < correctAnswer.length; i++)
{
System.out.println("Please enter your Answer for Question " + (i + 1) );
studentAnswer[i] = imput.next();

//Changes Lower case answers to upper case 
if (studentAnswer[i] == "a")
studentAnswer[i] = "A";
else if (studentAnswer[i] == "b")
studentAnswer[i] = "B";
else if (studentAnswer[i] == "c")
studentAnswer[i] = "C";
else if(studentAnswer[i] == "d")
studentAnswer[i] = "D";

//While loop to verify valid answers
while(studentAnswer[i] != "A" || studentAnswer[i] != "a" || studentAnswer[i] != "B" ||
studentAnswer[i] != "b" ||	studentAnswer[i] != "C" || studentAnswer[i] != "c" ||
studentAnswer[i] != "D" || studentAnswer[i] != "d")
{
System.out.println("Invalid Answer ");
System.out.println("Please enter your Answer for Question " + (i + 1) );
studentAnswer[i] = imput.next();
}
}
//Calls Calculation Methods
passed(correctAnswer, studentAnswer);
totalCorrect(correctAnswer, studentAnswer);
totalIncorrect(correctAnswer, studentAnswer);
questionMissed(correctAnswer, studentAnswer);
}

//Calculates pass / fail
public static void passed(String[] correctAnswer, String[] studentAnswer)
{
int i = 0;
int pass = 0;

while(arrayEquals && i < correctAnswer.length)
{
if (correctAnswer[i] != studentAnswer[i])
{
arrayEquals = false;
i++;
}
}
if (arrayEquals)
pass += 1;
else
pass -= 1;
if (pass >= 15)
System.out.println("You Passed ");
else
System.out.println("You did not pass ");
}
	
//Calculates number of correct answers
public static void totalCorrect(String[] correctAnswer, String[] studentAnswer)
{
int i =0;
int numCorrect = 0;
	
while(arrayEquals && i < correctAnswer.length)
{
if (correctAnswer[i] != studentAnswer[i])
{
arrayEquals = false;
i++;
}
if (arrayEquals)
numCorrect += 1;
}
System.out.println("You Answered " + numCorrect + " correctly ");
}
	
// Calculates Number of incorrect answers
public static void totalIncorrect(String[] correctAnswer, String[] studentAnswer)
{
int i =0;
int numWrong = 0;

while(arrayEquals && i < correctAnswer.length)
{
if (correctAnswer[i] != studentAnswer[i])
{	{
arrayEquals = false;
i++;
}
if (!arrayEquals)
numWrong += 1;
}
System.out.println("You Answered " + numWrong + " incorrectly ");
}

//Calculates the number wrong for the purpose of displaying the question numbers missed
public static void questionMissed(String[] correctAnswer, String[] studentAnswer)
{
int i =0;

//Use array list for totaling and displaying numbers missed
ArrayList arrayList = new ArrayList();
		
while(arrayEquals && i < correctAnswer.length)
{
if (correctAnswer[i] != studentAnswer[i])
{
arrayEquals = false;
arrayList.add(new Integer(i + 1)) ;
i++;
}
}
System.out.println("You Missed The Following Questions " + arrayList );
}
}



Is This A Good Question/Topic? 0
  • +

Replies To: Java License Exam Problem with Methods and Validation

#2 lbray785   User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 11
  • Joined: 30-March 10

Re: Java License Exam Problem with Methods and Validation

Posted 09 March 2011 - 12:38 PM

Firstly, I would use the toUpperCase() instead of using if statements.
System.out.println("Please enter your Answer for Question " + (i + 1) );
studentAnswer[i] = imput.next().toUpperCase();

This post has been edited by lbray785: 09 March 2011 - 12:40 PM

Was This Post Helpful? 0
  • +
  • -

#3 brdstnstrnglr   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 23-February 11

Re: Java License Exam Problem with Methods and Validation

Posted 09 March 2011 - 05:08 PM

View Postlbray785, on 09 March 2011 - 12:38 PM, said:

Firstly, I would use the toUpperCase() instead of using if statements.
System.out.println("Please enter your Answer for Question " + (i + 1) );
studentAnswer[i] = imput.next().toUpperCase();

Thanks That seems to have helped, any idea why this validation won't work? I used the same validation for the exact same problem in my C++ class.
import java.util.*;
import javax.swing.*;
//
public class DriverExam 
{
	static int SIZE = 20;
	static boolean arrayEquals = true;
	
	public static void main(String[] args)throws java.io.IOException
	{
//Variables
String[] correctAnswer = {"B", "D", "A", "A", "C", "A", "B", "A", "C", "D", "B",
			  "C", "D", "A", "D", "C", "C", "B", "D", "A"};
String[] studentAnswer = new String[SIZE];
int i;
Scanner imput = new Scanner(System.in);
//Gathers Student Answers		
for (i = 0; i < SIZE; i++)
{
System.out.println("Please enter your Answer for Question " + (i + 1) );
studentAnswer[i] = imput.next().toUpperCase();		
//While loop to verify valid answers
while(studentAnswer[i] != "A" && studentAnswer[i] != "a" && studentAnswer[i] != "B" && studentAnswer[i] != "b" && studentAnswer[i] != "C" && studentAnswer[i] != "c" && studentAnswer[i] != "D" && studentAnswer[i] != "d")
{
System.out.println("Invalid Answer ");
System.out.println("Please enter your Answer for Question " + (i + 1) );
studentAnswer[i] = imput.next().toUpperCase();
}
}


This post has been edited by brdstnstrnglr: 09 March 2011 - 05:10 PM

Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12317
  • View blog
  • Posts: 45,417
  • Joined: 27-December 08

Re: Java License Exam Problem with Methods and Validation

Posted 09 March 2011 - 05:15 PM

Please properly indent your code so others, as well as yourself, can read it. One thing I noticed was that you compare Strings using the == and != operators. Don't do this. Those operators compare the Strings' locations in memory. Use the equals() method instaed to compare Strings. It returns a boolean based on whether or not they have equal values.
Was This Post Helpful? 0
  • +
  • -

#5 brdstnstrnglr   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 23-February 11

Re: Java License Exam Problem with Methods and Validation

Posted 09 March 2011 - 05:19 PM

View Postmacosxnerd101, on 09 March 2011 - 05:15 PM, said:

Please properly indent your code so others, as well as yourself, can read it. One thing I noticed was that you compare Strings using the == and != operators. Don't do this. Those operators compare the Strings' locations in memory. Use the equals() method instaed to compare Strings. It returns a boolean based on whether or not they have equal values.

Sorry for the indention issues. as far as the == and != are you referring to the array comparisons in the methods? I copied the array comparison from an example in my text can you give me an idea as to how the equals()method works?
Thanks

This post has been edited by brdstnstrnglr: 09 March 2011 - 05:20 PM

Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12317
  • View blog
  • Posts: 45,417
  • Joined: 27-December 08

Re: Java License Exam Problem with Methods and Validation

Posted 09 March 2011 - 05:20 PM

Exactly like this:
if(myString.equals("someOtherStringHere")){
   //code
}


Was This Post Helpful? 0
  • +
  • -

#7 brdstnstrnglr   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 23-February 11

Re: Java License Exam Problem with Methods and Validation

Posted 09 March 2011 - 05:23 PM

View Postmacosxnerd101, on 09 March 2011 - 05:20 PM, said:

Exactly like this:
if(myString.equals("someOtherStringHere")){
   //code
}


Thanks very much I'll give that a try
Was This Post Helpful? 0
  • +
  • -

#8 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12317
  • View blog
  • Posts: 45,417
  • Joined: 27-December 08

Re: Java License Exam Problem with Methods and Validation

Posted 09 March 2011 - 05:24 PM

Glad I could help! :)
Was This Post Helpful? 0
  • +
  • -

#9 brdstnstrnglr   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 23-February 11

Re: Java License Exam Problem with Methods and Validation

Posted 09 March 2011 - 06:10 PM

View Postmacosxnerd101, on 09 March 2011 - 05:24 PM, said:

Glad I could help! :)

So one last question (hopefully), using the equals()method, do i need to keep the arrayEquals stuff in the method below?
public static void passed(String[] correctAnswer, String[] studentAnswer)
	{
		int i = 0;
		int pass = 0;
		
		while(arrayEquals && i < SIZE)
		{
			if (correctAnswer[i].equals(studentAnswer[i]))
			{
				arrayEquals = false;
				 i++;
			}
		}
		if (arrayEquals)
			pass += 1;
		else
			pass -= 1;
		if (pass >= 15)
			System.out.println("You Passed ");
		else
			System.out.println("You did not pass ");
	}	


Was This Post Helpful? 0
  • +
  • -

#10 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Java License Exam Problem with Methods and Validation

Posted 09 March 2011 - 09:08 PM

View Postbrdstnstrnglr, on 09 March 2011 - 07:23 PM, said:

View Postmacosxnerd101, on 09 March 2011 - 05:20 PM, said:

Exactly like this:
if(myString.equals("someOtherStringHere")){
   //code
}


Thanks very much I'll give that a try

As you are only comparing one character to effectively change the answer into a char and then use == and !=

studentAnswer[i] = imput.next().toUpperCase();
if(studentAnswer[i].equals("A") || .....)

or

studentAnswer[i] = imput.next().toUpperCase();
char ans = studentAnswer[i].chatAt(0);
if(ans == 'A' || ...)
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1