Results 1 to 2 of 2
  1. #1
    Terminus_Est is offline Member
    Join Date
    Feb 2012
    Posts
    5
    Rep Power
    0

    Default Need help passing Scanner objects into char arrays

    Ok so I am doing this lab assignment in which I have to use arrays to process input from a file and display results. The code for the main program was already provided by my instructor and this is it right here:

    Java Code:
    import java.util.Scanner;
    import java.io.*;
    
    import DriverExam.DriverExam;
    
    public class Chapter7ALabDemo
    {
        public static void main (String [] args) throws IOException
        {
             File file;
             Scanner readKey;
             Scanner readAnswers;
             String str;
             int numQuestions;
             DriverExam student;
             int [] missedQuestions;
    
             Scanner keyboard = new Scanner(System.in);
    
             System.out.println("Enter the name of the file that is the test key ");
             str = keyboard.nextLine();
             file = new File(str);
             readKey = new Scanner(file);
    
             System.out.println("Enter the name of the file with the student answers.");
             str = keyboard.nextLine();
             file = new File(str);
             readAnswers = new Scanner(file);
    
             System.out.println("How many test questions are there?");
             numQuestions = keyboard.nextInt();
    
             student = new DriverExam(readKey, readAnswers, numQuestions);
             missedQuestions = student.questionsMissed();
    
             System.out.println(student);
             if (student.passed())
                  System.out.println("The student passed.");
             else
                  System.out.println("The student did not pass.");
    
             System.out.println("The student got " + student.totalCorrect() + " answers correct.");
             System.out.println("The student got " + student.totalIncorrect() + " answers incorrect.");
             System.out.println("The following questions were missed by the student: ");
             student.printMissed(missedQuestions);
         }
    }
    Alright so I am going to have to write a class with two fields that both reference Char arrays but just how do I pass the data from the Scanner objects into char arrays? So far this is what I have. The "int questions" receives the total size of both arrays.

    Java Code:
    //Mark Herrera
    
    package DriverExam;
    
    import java.util.Scanner;
    
    public class DriverExam {
    	
    	private char[] answerKey;
    	private char[] studentAnswers;
    	
    	public DriverExam(Scanner rKey, Scanner rAnswers, int questions)
    	{
    		
    		
    	}
    
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,576
    Rep Power
    23

    Default Re: Need help passing Scanner objects into char arrays

    how do I pass the data from the Scanner objects into char arrays
    If the Scanner methods you are using read Strings, the contents of the Strings can be extracted to char arrays using String class methods.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. passing objects w/RMI
    By nicoeschpiko in forum Advanced Java
    Replies: 5
    Last Post: 02-06-2011, 08:59 AM
  2. scanner objects with arrays again..
    By nevermiind in forum New To Java
    Replies: 18
    Last Post: 05-24-2010, 11:45 AM
  3. Comparing two Char arrays
    By viperlasson in forum New To Java
    Replies: 3
    Last Post: 01-30-2010, 08:05 AM
  4. Passing Objects
    By Java_Developer in forum New To Java
    Replies: 6
    Last Post: 10-29-2009, 01:32 PM
  5. MVC passing objects
    By simo_mon in forum New To Java
    Replies: 1
    Last Post: 07-17-2009, 06:57 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •