Total members 11894 |It is currently Thu Nov 21, 2024 2:04 pm Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





How to compare two arrays of characters in java
hey, im working on this project for homework and it consists of comparing the elements of one array to another. I had it running but then i did something and it made it give an exception error.

also before i messed that up i was stuck on how to compare the 2 arrays.

Code:
//holds methods used for drivers test
import java.util.Scanner; //scanner class import

 class DriverTestMethods //class name
 {
 
    private String
[] answers = { "B", "D", "A", "A", "C", "A", "B", "A",
                                "C", "D", "B", "C", "D", "A", "D", "C",
                                 "C", "B", "D", "A" }; //the real answers.
                                 
    private String
[] inputted;  //inputted answers
                                 
    private String
[] questions;   //displays questions.
    private int correct = 0; //holds amount of correct answers.
    private int incorrect = 0; //holds amount of incorrect answers.
    private int qcounter = 1; //counts questions til 20.
    private int index = 0;
    
     Scanner keyboard 
= new Scanner(System.in);// holds input
    
    void testBegin
()
        {
            
            do
            
{
                System.out.println("Question " + qcounter +" goes here");
                inputted[index] = keyboard.next();
                
                if
(inputted[index] == answers[index])
                {
                    correct++;
                }
               else
               
{
                    incorrect++;    
               
}
                    
              qcounter
+=1;
              index+=1;
            }while (qcounter != 21);
            
            passed
();
         }
                    
    void passed
()
        {
            if(correct >= 15)
            {
                System.out.println("Good job, you passed!");
                System.out.println("Correct: " + correct);
                System.out.println("Incorrect: " + incorrect);
            }            
           else
            if
(incorrect >= 5)
            {    
                System
.out.println("Sorry, You Suck!!");
                System.out.println("Correct: " + correct);
                System.out.println("Incorrect: " + incorrect);
            }    
            
            System
.exit(0);
                
        
}
    }
 





Attachments:
File comment: there are 2 java files compressed,
drivertestfiles.zip [1.25 KiB]
Downloaded 837 times

_________________
Environmental enrichment (neural), how the brain is affected by the stimulation of its information processing provided by its surroundings.


Last edited by 3nriched on Sun Nov 28, 2010 2:05 am, edited 1 time in total.
Author:
Newbie
User avatar Posts: 6
Have thanks: 1 time

Ok! Finally Found The Error! :yahoo:
But I don't know is that you looking for... :swoon:

Here what I did,

I changed
Code:
private String inputted;  //inputted answers
inputted = keyboard.next();
inputted.equals(answers[index]


And Here the whole java file.
Code:
//holds methods used for drivers test
import java.util.Scanner; //scanner class import

class DriverTestMethods //class name
{

   private String[] answers = { "B", "D", "A", "A", "C", "A", "B", "A",
                        "C", "D", "B", "C", "D", "A", "D", "C",
                         "C", "B", "D", "A" }; //the real answers.
                        
   private String inputted;  //inputted answers
                        
   private String[] questions;   //displays questions.
   private int correct = 0; //holds amount of correct answers.
   private int incorrect = 0; //holds amount of incorrect answers.
   private int qcounter = 1; //counts questions til 20.
   private int index = 0;
       
   
    Scanner keyboard = new Scanner(System.in);// holds input
   
   void testBegin()
      {
         
         do
         {
            System.out.println("Question " + qcounter +" goes here");
            inputted = keyboard.next();
            
            if(inputted.equals(answers[index]))
            {
               correct++;
                                       
            }
            else
            {
               incorrect++;   
            }
               
           qcounter+=1;
           index+=1;
         }while (qcounter != 21);
         
         passed();
       }
               
   void passed()
      {
         if(correct >= 15)
         {
            System.out.println("Good job, you passed!");
            System.out.println("Correct: " + correct);
            System.out.println("Incorrect: " + incorrect);
         }         
         else
          if(incorrect >= 5)
         {   
            System.out.println("Sorry, You Suck!!");
            System.out.println("Correct: " + correct);
            System.out.println("Incorrect: " + incorrect);
         }   
         
         System.exit(0);
            
      }
   }


If you have any question ask me! :rolf:

_________________
Coding my life with Java, PHP, JavaScript, and Python


Author:
Newbie
User avatar Posts: 39
Have thanks: 15 time

For this message the author isuru has received thanks - 2: 3nriched, DrRakha

nice dude, i actually made it passed this part earlier today but this helps me with a different problem i have now ..lol
thanks again man!

_________________
Environmental enrichment (neural), how the brain is affected by the stimulation of its information processing provided by its surroundings.


Author:
Newbie
User avatar Posts: 6
Have thanks: 1 time
Post new topic Reply to topic  [ 3 posts ] 

  Related Posts  to : How to compare two arrays in java
 Arrays in java     -  
 Multidimensional arrays in Java     -  
 Comparing Arrays in java     -  
 creating arrays in java     -  
 Passing arrays as function parameter in java     -  
 compare two strings     -  
 Compare two strings in php     -  
 compare strings     -  
 compare two sets     -  
 Compare directories     -  









Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com