Jump to content

Reading text file and Multidimensional arrays in Java

Hi, I understand most people will just brush past this post so I'll be brief. Our assignment was  this:

A registered user should be afforded the opportunity of taking Multiple Choice Practice Tests (MCPTs). Details of registered users are held on file (format of this file is username,password). When your application is launched a person enters their username and password to begin a test. A person is allowed to have three attempts at most to supply a valid username and password. When a valid username and password are supplied the user is presented with a list of Java topics. The user selects a topic on which he/she is then presented with Multiple Choice Questions (MCQs) on same. A topic should only be included in this list if there is at least one associated MCQ. A MCPT should comprise at most ten questions which should be chosen at random from the available list of questions on the topic. A topic may have less that ten questions associated with it. Since the objective of the application is to improve a person’s understanding of topics, one question should be presented at an instance. From the list of four possible answers, if the user selects the correct one he/she is then moved onto the next question otherwise, he/she is presented with details of the correct answer along with an explanation. At the end of a MCPT the user is informed of his/her result for this MCPT and his/her overall performance to date (not only of this interaction but of any previous interactions with the application). Once a MCPT is complete the user is returned to the list of topics to select another or exit. All information concerning topics, questions, answers and explanations should be stored in one or more data files.

 

That's a lot to take in, but in short:

1) user logs on with username and password, which are stored in a .txt file

2) Prompted with a list of topics, which are also stored in a .txt file

3)User enters quiz

 

So far I have attempted the username and password and topic menu but I'm prompted with six errors.


import java.io.*;
import java.util.*;
public class project
{

    //These lines of code intiate three multi-demsional arrays lists.
    //We did this so we could have our usernames and passwords together
    //Our topics and topic numbers together
    //And to put all out questions and answers together
    public static ArrayList<ArrayList<String>> user;
    public static ArrayList<ArrayList<String>> topics;
    public static ArrayList<ArrayList<String>> questions;
    public static void main(String [] args)
    {
        //this willc all a method that will return true if all files exists
        //if one or more files do not exist or are corrupt it will prompt an error message
        //Program will then shut down

        if(readFilesIntoArrayList())
        {
            //if all goes succesfully, we call userLogon method which should promt the user to logon
            //else promopted with error and program ends
            userLogon();
        }
        else
        {
            System.out.println("The files you enetered are corrupt or missing.");
        }
    }

    //This method will read all three files and split them into array lists
    //This is to save us from doing this further on in the code
    //It also makes use of each multidemsional array lists we created
    public static boolean readFilesIntoArrayList(ArrayList<ArrayList<String>> user) throws IOException
    {
        String filename1 = "username.txt";
        String filename2 = "topics.txt";
        String filename3 = "questions.txt";
        String fileElements[];
        File inputFile         = new File(filename1);
        File inputFile1     = new File(filename2);
        File inputFile2         = new File(filename3);

        user     = new ArrayList<ArrayList<String>>();
        user.add(new ArrayList<String>());
        user.add(new ArrayList<String>());

        topics     = new ArrayList<ArrayList<String>>();
        topics.add(new ArrayList<String>());
        topics.add(new ArrayList<String>());

        questions = new ArrayList<ArrayList<String>>();
        questions.add(new ArrayList<String>());
        questions.add(new ArrayList<String>());
        questions.add(new ArrayList<String>());
        questions.add(new ArrayList<String>());
        questions.add(new ArrayList<String>());

        //Will check if all files actually exist
        //If they do, it will begin splitting them into arrays at each ","
        //Will continue to do this if more lines of text exist
        if(inputFile.exists() && inputFile1.exists() && inputFile2.exists())
        {
            Scanner in;
            in = new Scanner(inputFile);
            while(in.hasNext())
            {
                fileElements = (in.nextLine()).split(",");
                user.get(0).add(fileElements[0]);
                user.get(1).add(fileElements[1]);
            }
            in.close();
            in = new Scanner(inputFile1);
            while(in.hasNext())
            {
                fileElements = (in.nextLine()).split(",");
                topics.get(0).add(fileElements[0]);
                topics.get(1).add(fileElements[1]);
            }
            in.close();
            in = new Scanner(inputFile2);
            while(in.hasNext())
            {
                fileElements = (in.nextLine()).split(",");
                questions.get(0).add(fileElements[0]);
                questions.get(1).add(fileElements[1]);
                questions.get(2).add(fileElements[2]);
                questions.get(3).add(fileElements[3]);
                questions.get(4).add(fileElements[4]);
                }
            in.close();
            return true;
        }
        else
            return false;
    }

    //This is the method for user log on
    //Each time this method is ran the user gets three attempts
    //It checks username and password by comparing if the username and password enetered
    //are on the same index in each array
    //user will then be promted to topics section
    //else program end
    public static void userLogon(ArrayList<ArrayList<String>> user)
    {

        //user is asked for username and passwords
        System.out.print("Enter username:");
        Scanner scan = new Scanner(System.in);
        String username = scan.nextLine();
        System.out.print("Enter password:");
        String password = scan.nextLine();
        boolean logonSuccess=false;


        //Having problems in this line of code
        //Cannot get it to give us the length of each
        int pos = username.indexOf.user.get(0);
        int rows = 2;
        int counter = 0;
        int columns = user.get(0).size();
        String [][]usernameAndPassword = new String [rows][columns];
        for (int i=0; i <usernameAndPassword.length; i++){
            usernameAndPassword[0][i] = (user.get(0).add(i));
            usernameAndPassword[1][i] = (user.get(1).add(i));
        }

        while(counter<3)
        {
            for (int k=0; k<usernameAndPassword.length; k++)
            {

                //Matching password and username to the index in which they came
                //ie. if password is in index 1 then username must be also
                if (username == usernameAndPassword[k][0])
                {
                    if(password == usernameAndPassword[0][k])
                    {
                        //Here the boolean value is set to true, meaning topics will be initialised
                        logonSuccess=true;
                    }

                }
                counter++;
        }
        if(logonSuccess)
        {
            //user is prompted to topics section, here he/she will be asked to choose a topic
            topics();
        }
        else
            System.out.println("User failed to logon under three attempts");
    }
    }


    //Topics method will display each each topic and the number it represents
    //the user will eneter a interget value of a topic
    //This value will be returned to a new topic called questions will sort questions into which the user has asked
    public static int topics(ArrayList<ArrayList<String>> topics)
    {
        //temp strings will allow the topic name and number to change constantly
        String temp  = "";
        String temp1 = "";
        System.out.println("Please enter a topic number in which you would like to be quizzed on:");
        //will get the size of the array
        int size = topics.get(0).size();
        //for look to display each message
        for(int i = 0; i < size; i++)
        {
            //Will print out each number and topic on the same line
            //once again problems arrose herehkj
            temp = topics.get(0).get(i);
            temp1 = topics.get(1).get(i);
            System.out.print(temp);
            System.out.print("/t" + temp1 + "/n");
        }
        //here is when the user is asked to pick a interger value that represents the topic of choice
        Scanner scan = new Scanner(System.in);
        int selection = scan.nextInt();
        return selection;
    }
        //In the method questions the user will be promopted with a questions followed by four answers
        //He enters between 1-4, which is his selection
        //It then compares his input to the correct answer and will tally his correct results
        public static int questions(int selection)
        {

        }
}

The errors are:

C:\Users\james\Desktop\project.java:19: error: method readFilesIntoArrayList in class project cannot be applied to given types;
        if(readFilesIntoArrayList())
           ^
  required: ArrayList<ArrayList<String>>
  found: no arguments
  reason: actual and formal argument lists differ in length
C:\Users\james\Desktop\project.java:23: error: method userLogon in class project cannot be applied to given types;
            userLogon();
            ^
  required: ArrayList<ArrayList<String>>
  found: no arguments
  reason: actual and formal argument lists differ in length
C:\Users\james\Desktop\project.java:118: error: cannot find symbol
        int pos = username.indexOf.user.get(0);
                          ^
  symbol:   variable indexOf
  location: variable username of type String
C:\Users\james\Desktop\project.java:124: error: no suitable method found for add(int)
            usernameAndPassword[0] = (user.get(0).add(i));
                                                    ^
    method Collection.add(String) is not applicable
      (argument mismatch; int cannot be converted to String)
    method List.add(String) is not applicable
      (argument mismatch; int cannot be converted to String)
    method AbstractCollection.add(String) is not applicable
      (argument mismatch; int cannot be converted to String)
    method AbstractList.add(String) is not applicable
      (argument mismatch; int cannot be converted to String)
    method ArrayList.add(String) is not applicable
      (argument mismatch; int cannot be converted to String)
C:\Users\james\Desktop\project.java:125: error: no suitable method found for add(int)
            usernameAndPassword[1] = (user.get(1).add(i));
                                                    ^
    method Collection.add(String) is not applicable
      (argument mismatch; int cannot be converted to String)
    method List.add(String) is not applicable
      (argument mismatch; int cannot be converted to String)
    method AbstractCollection.add(String) is not applicable
      (argument mismatch; int cannot be converted to String)
    method AbstractList.add(String) is not applicable
      (argument mismatch; int cannot be converted to String)
    method ArrayList.add(String) is not applicable
      (argument mismatch; int cannot be converted to String)
C:\Users\james\Desktop\project.java:149: error: method topics in class project cannot be applied to given types;
            topics();
            ^
  required: ArrayList<ArrayList<String>>
  found: no arguments
  reason: actual and formal argument lists differ in length
Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output
6 errors

Tool completed with exit code 1
 

Irish in Vancouver, what's new?

 

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×