Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 634
  • Last Modified:

Problem filling a array in java

Hi there all
i have created an array in java and i ma trying to fill it and i and getting a error
here is my code and you please tell me what i might be doing wrong

import java.util.Scanner;

/**
 * 
 */

/**
 * @author Adam.Wolarczuk
 *
 */
public class DriverTest {


	public static void main(String[] args) {
		//Keyboard scanner to read the input from the keyboard
		Scanner keyboard = new Scanner (System.in);
		
	
		
		Double Answer; //This is to store the keyboard input 
		int count = 1;
		
	
		do {
		System.out.print ("Question" + " " + count + ":" +  " "  );
		Answer = keyboard.nextDouble();
		count ++;
		} while (count < 21);
		
	}


	public class DriverExam{
		int[]CorrentAnswers = new int[19];
		CorrectAnswers [0] = b;
		CorrectAnswers [1] = d;
		CorrectAnswers [2] = a;
		CorrectAnswers [3] = a;
		CorrectAnswers [4] = c;
		CorrectAnswers [5] = a;
		CorrectAnswers [6] = b;
		CorrectAnswers [7] = a;
		CorrectAnswers [8] = c;
		CorrectAnswers [9] = d;
		CorrectAnswers [10] = b;
		CorrectAnswers [11] = c;
		CorrectAnswers [12] = d;
		CorrectAnswers [13] = a;
		CorrectAnswers [14] = d;
		CorrectAnswers [15] = c;
		CorrectAnswers [16] = c;
		CorrectAnswers [17] = b;
		CorrectAnswers [18] = d;
		CorrectAnswers [19] = a;
		
		
		
		
		
	}
	
		// TODO Auto-generated method stub
}
	}

Open in new window

0
awolarczuk
Asked:
awolarczuk
  • 13
  • 11
1 Solution
 
awolarczukAuthor Commented:
CEHJ
Mate that tells me nothin, am i even filling the array correctly ???
0
 
CEHJCommented:
and i and getting a error

So, is the idea that we're meant to guess what error you're getting??
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
awolarczukAuthor Commented:
No mate i am guessing the idea is that you look at my code and see if it looks right and if not i can give you more infomation.  If that is ok of course
0
 
awolarczukAuthor Commented:
Mate this is current code at the moment i am not sure if i am doing it right
i am trying to wite all the answer in to the array StudentAnswers and then read them out
when i read them out i get

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 19
      at DriverExam.main(DriverExam.java:29)
import java.util.Scanner;

/**
 * 
 */

/**
 * @author Adam.Wolarczuk
 *
 */
public class DriverExam {


	public static void main(String[] args) {
		int CorrentAnswers[]={1,2,3};
		int[]StudentAnswers = new int[19];
		//Keyboard scanner to read the input from the keyboard
		Scanner keyboard = new Scanner (System.in);
		
	
		
		Double Answer; //This is to store the keyboard input 
		int count = 1;
		int arrayLocation = 0;
		
	
		do {
		System.out.print ("Question" + " " + count + ":" +  " "  );
		StudentAnswers[arrayLocation] = keyboard.nextInt();
		count ++;
		arrayLocation++;
		
		} while (count < 21);
		for(int y:StudentAnswers)
		System.out.println(y);
		
	}



	
		
		
		
		
		
		
	
		// TODO Auto-generated method stub
}
	

Open in new window

0
 
CEHJCommented:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 19
      at DriverExam.main(DriverExam.java:29)

Open in new window


That's more like it
0
 
awolarczukAuthor Commented:
ok np so what i can do here
0
 
CEHJCommented:
'count' needs to be less than the length of array 'StudentAnswers' (java variables should begin lower case btw)
0
 
awolarczukAuthor Commented:
ok got that i just worked that out, the issues i am having now is that i need to ote char in the array Correct Answers and i need to read Char in frm the keyboard

It doesnt seems that keyboard.nextchar is correct and i get a cannot be resolved to a variable when i try to store char in the array
0
 
CEHJCommented:
char nextChar = keyboard.nextLine().charAt(0);

Open in new window

0
 
awolarczukAuthor Commented:
cool thanks

how aobut this
char [] CorrentAnswers={a,2,3};

I need to store 20 letters in the array numbers are ok but letter it doesnt like
0
 
CEHJCommented:
char [] correctAnswers={ 'a', '2', '3' };

Open in new window

0
 
awolarczukAuthor Commented:
ok one last thing i have got most of it working thanks for your help just in the results fo the missed questions i dont want the end 0 0 0 0 0

is there a way i can not see them
here is my code
import java.util.Scanner;

/**
 * 
 */

/**
 * @author Adam.Wolarczuk
 *
 */
public class DriverExam {


	public static void main(String[] args) {
	
		//Keyboard scanner to read the input from the keyboard
		Scanner keyboard = new Scanner (System.in);
		
	
		
	
		int totalCorrect = 0;
		int totalIncorrect = 0;		
		int count = 1;
		int arrayLocation = 0;
		int missed = 0;
		
		char [] CorrentAnswers = { 'b' , 'd' , 'a' , 'a' , 'c' , 'a' , 'b' , 'a' , 'c' , 'd', 'b' , 'c' , 'd' , 'a', 'd' , 'c' , 'c' , 'b' , 'd' , 'a'};
		char [] StudentAnswers = new char [20];
		int [] questionsMissed = new int [20];
	
		do {
		System.out.print ("Question" + " " + count + ":" +  " "  );
		StudentAnswers[arrayLocation] = keyboard.next().charAt(0);
				if (StudentAnswers[arrayLocation] == CorrentAnswers[arrayLocation])
				{
					totalCorrect ++;
				}
				else
				{
					questionsMissed[missed] = count;
					missed ++;
					totalIncorrect ++;
				}
					
						
		count ++;
		arrayLocation++;
			
		
		} while (count < 21);
		
		
		System.out.println("Correct answers" + " " + totalCorrect);
		System.out.println("Incorect answers" + " " + totalIncorrect);
		
		if (totalCorrect >= 10)
		{
			System.out.println("You passed the exam");
			System.out.println("You missed the following questions:");
			for(int y:questionsMissed)
				System.out.print(y + " ");
		}
		else
		{
			
			System.out.println("You Failed the exam");
			System.out.println("You missed the following questions:");
			for(int y:questionsMissed)
			System.out.print(y + " ");
		}
			
		
		/**for(char y:StudentAnswers)
		System.out.print(y);*/
		
	}



	
		
		
		
		
		
		
	
		// TODO Auto-generated method stub
}
	

Open in new window

0
 
awolarczukAuthor Commented:
Come please can someone help me out here
0
 
CEHJCommented:
Once again, and for the last time, don't expect anyone to help if you can't be bothered to state what the problem is
0
 
awolarczukAuthor Commented:
CEHJ
please read my post it is clear in there what the problem is, before going off you top you might want to have a read, ID: 38357157 is very clear well i thought it as if it is not i am very sorry and i will try better next time
0
 
CEHJCommented:
I apologise (your previous posting behaviour biased me) as this time you stated the problem, but i missed it.

Try
            for (int y : questionsMissed)
                System.out.print(y > 0? y + " " : "");

Open in new window

0
 
awolarczukAuthor Commented:
Thats understandable
I have changed you code and now it is now showing anything
0
 
CEHJCommented:
Please post new code
0
 
awolarczukAuthor Commented:
import java.util.Scanner;

/**
 *
 */

/**
 * @author Adam.Wolarczuk
 *
 */
public class DriverExam {


      public static void main(String[] args) {
      
            //Keyboard scanner to read the input from the keyboard
            Scanner keyboard = new Scanner (System.in);
            
      
            
      
            int totalCorrect = 0;
            int totalIncorrect = 0;            
            int count = 1;
            int arrayLocation = 0;
            int missed = 0;
            
            char [] CorrentAnswers = { 'b' , 'd' , 'a' , 'a' , 'c' , 'a' , 'b' , 'a' , 'c' , 'd', 'b' , 'c' , 'd' , 'a', 'd' , 'c' , 'c' , 'b' , 'd' , 'a'};
            char [] StudentAnswers = new char [20];
            int [] questionsMissed = new int [20];
      
            do {
            System.out.print ("Question" + " " + count + ":" +  " "  );
            StudentAnswers[arrayLocation] = keyboard.next().charAt(0);
                        if (StudentAnswers[arrayLocation] == CorrentAnswers[arrayLocation])
                        {
                              totalCorrect ++;
                        }
                        else
                        {
                              questionsMissed[missed] = count;
                              missed ++;
                              totalIncorrect ++;
                        }
                              
                                    
            count ++;
            arrayLocation++;
                  
            
            } while (count < 21);
            
            
            System.out.println("Correct answers" + " " + totalCorrect);
            System.out.println("Incorect answers" + " " + totalIncorrect);
            
            if (totalCorrect >= 10)
            {
                  System.out.println("You passed the exam");
                  System.out.println("You missed the following questions:");
                  for(int y:questionsMissed)
                        System.out.print(y > 0? y + " " : "");
            }
            else
            {
                  
                  System.out.println("You Failed the exam");
                  System.out.println("You missed the following questions:");
                  for(int y:questionsMissed)
                        System.out.print(y > 0? y + " " : "");
            }
                  
            
            /**for(char y:StudentAnswers)
            System.out.print(y);*/
            
      }



      
            
            
            
            
            
            
      
            // TODO Auto-generated method stub
}
0
 
CEHJCommented:
Question 1: a
Question 2: a
Question 3: a
Question 4: a
Question 5: a
Question 6: a
Question 7: a
Question 8: a
Question 9: a
Question 10: a
Question 11: a
Question 12: a
Question 13: a
Question 14: a
Question 15: a
Question 16: a
Question 17: a
Question 18: a
Question 19: a
Question 20: a
Correct answers 6
Incorect answers 14
You Failed the exam
You missed the following questions:
1 2 5 7 9 10 11 12 13 15 16 17 18 19

Open in new window

The problem is what exactly?
0
 
awolarczukAuthor Commented:
if you only miss a few and nto all of them there are ending zeros

dont worry mate i had to hand it in
0
 
awolarczukAuthor Commented:
Answers were good but i thought was a little rude had a go at me for something that i did correctly before reading the notes , apart from that seems to knwo him stuff
0
 
CEHJCommented:
OK :)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 13
  • 11
Tackle projects and never again get stuck behind a technical roadblock.
Join Now