Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

JAVA Programming Help... I have one small error! :(

Options
  • 09-05-2013 2:36pm
    #1
    Registered Users Posts: 12


    Hey.... I have a program here that I need help with. The problem is this.... I know I need a .txt file to read/write data for the program, but I don't know what to put as the names for the .txt files.

    If I post the program here will someone please look at it and help me out with it please? I'm only a beginner you see :(

    Any help would be gratefully appreciated! :)
    Thanks,
    Kieran.


Comments

  • Registered Users Posts: 12 KieranOs


    **** This is a login system for college, students and admin's can login, each getting different menu options. Admins can add students and modules, this is where my error is. The code is right but the .txt files aren't set up and i need help with this. Please help me someone :( ****

    Here is the code:

    import java.util.Scanner;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.io.FileWriter;
    public class Project {
    /**
    * @param args
    * @throws IOException
    */
    public static void main(String[] args) throws IOException {
    // TODO Auto-generated method stub
    Scanner kb = new Scanner(System.in);
    int choice=0;

    while(choice!=7)
    {
    String[] moduleCodeList = read("moduleCodeList.txt");
    String[] moduleNameList = read("moduleNameList.txt");
    String[] idList=read("idList.txt");
    String[] passwordList=read("passwordList.txt");
    String[] nameList=read("nameList.txt");
    int noOfUsers=getNumberOfList("idList.txt");
    int noOfModules=getNumberOfList("moduleCodeList.txt");
    String[] userTypeList=read("userTypeList.txt");
    int index = -1;
    printTitle("");

    index=login(idList,passwordList,noOfUsers);

    while (choice!=6)
    {
    moduleCodeList = read("moduleCodeList.txt");
    moduleNameList = read("moduleNameList.txt");
    idList=read("idList.txt");
    passwordList=read("passwordList.txt");
    nameList=read("nameList.txt");
    noOfUsers=getNumberOfList("idList.txt");
    noOfModules=getNumberOfList("moduleCodeList.txt");
    userTypeList=read("userTypeList.txt");

    printTitle(nameList[index]);
    if (userTypeList[index].compareTo("1")==0)
    {
    choice=adminMenu(moduleCodeList,moduleNameList,idList,passwordList,nameList,userTypeList,index,noOfUsers,noOfModules);
    }
    else
    {
    choice=studentMenu(nameList,idList,moduleCodeList,moduleNameList,noOfModules,index);
    }
    System.out.println("\n\n\tPress ENTER to continue");
    kb.nextLine();
    System.out.print("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
    }
    System.out.println("\tTo Login enter: 1");
    System.out.println("\tTo Exit enter enter: 2");
    System.out.print("\n\n\n\n\n\n");
    choice = getIntInRange(1,2);
    System.out.print("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
    if(choice == 2)
    {
    choice=7;
    }
    }


    }

    public static int login(String[] idList,String[] passwordList,int noOfStudents)
    {
    int index=0;
    String password;
    String id;
    do
    {
    id=getString("User ID : ");
    index=findStringInList(idList,id,noOfStudents);
    if(index!=-1)
    { password=getString("Password : ");
    if (password.compareTo(passwordList[index])==1)
    {
    index=-1;
    }
    }
    if(index==-1)
    {
    System.out.println("Wrong password/id try again");
    }
    }
    while(index==-1);
    return index;
    }
    public static int adminMenu(String[] moduleCodeList,String[] moduleNameList,String[] idList,String[] passwordList,String[] nameList,String[] userTypeList,int index,int noOfUsers, int noOfModules) throws IOException
    { int choice=0;
    System.out.println("Please choose one of the following options:");
    System.out.println("(1) List Students");
    System.out.println("(2) List Modules");
    System.out.println("(3) Add Student");
    System.out.println("(4) Add Module");
    System.out.println("(5) View list of students registered");
    System.out.println("(6) Log out");
    choice = getIntInRange(1,6);

    if(choice==1)
    {
    System.out.println("
    ");
    System.out.println("\t ID Names");
    displayList(idList,nameList,noOfUsers);
    }
    else if(choice==2)
    {
    displayList(moduleCodeList,moduleNameList,noOfModules);
    }
    else if(choice==3)
    {
    addStudent(idList,passwordList,nameList,userTypeList,noOfUsers);
    }
    else if(choice==4)
    {
    addModule(moduleCodeList,moduleNameList, noOfModules);
    }
    else if(choice==5)
    {
    listOfStudentsRegistered(idList, passwordList,nameList, userTypeList, noOfUsers);
    }
    return choice;

    }
    public static int studentMenu(String[] names, String[] idList, String[] moduleCodeList,String[] moduleNameList,int noOfModules, int index) throws IOException
    {
    int choice=0;
    while(choice !=3)
    {
    System.out.println("\t(1) List of modules");
    System.out.println("\t(2) Register for a module");
    choice = getIntInRange(1,3);
    if (choice == 1)
    {
    displayList(moduleNameList,noOfModules);
    }
    else if (choice == 2)
    {

    register(idList[index], moduleCodeList,moduleNameList, noOfModules);
    }
    else System.out.println("\tBye Bye");

    }
    choice=5;
    return choice;
    }
    public static int findStringInList (String[] list,String x,int numberOfExistingItems)
    {
    int index=-1;

    for(int i=0;i<numberOfExistingItems;i++)
    {
    if (x.compareToIgnoreCase(list)==0)
    {
    index=i;
    break;
    }
    }

    return index;

    }
    public static int getIntInRange(int min, int max)
    {
    int choice=0;
    boolean valid = false;

    Scanner kb=new Scanner(System.in);
    System.out.print("Please etner your choice between " + min + " and "+max);
    while(valid==false)
    {
    while(!kb.hasNextInt())
    {
    System.out.print("Error! please make sure you entered an integer\nPlease etner your choice between " + min + " and "+max);
    kb.nextLine();
    }
    choice=kb.nextInt();
    if (choice >= min && choice <= max)
    {
    valid=true;
    }
    else
    {
    valid = false;
    System.out.print("Your choice must be between " + min + " and "+max+"!");
    }
    }


    return choice;
    }
    public static String getString(String text)
    {
    String x="";
    Scanner kb=new Scanner(System.in);
    System.out.print(""+text);
    x=kb.next();
    return x;

    }
    public static void addStudent(String[] idList,String[] passwordList,String[] nameList,String[] userTypeList,int noOfUsers) throws IOException
    {
    if(noOfUsers>=10)
    {
    System.out.println("Sorry but there are maximum amount of users has been registered allready");
    }
    else
    {
    int valid=0;
    String id="";
    while (valid!=-1)
    {
    id=getString("Please enter ID: ");
    valid=findStringInList(idList,id,noOfUsers);
    if(valid!=-1)
    {
    System.out.println("This ID is already in use! Please choose different ID!");
    }
    }
    String name=getString("Please enter name of student: ");
    String password=getString("please enter password for this accont");
    boolean validateType=false;
    String type="";
    while (validateType==false)
    {
    type=getString("please enter chose type: (1)Admin (2)Student ");
    if(type.compareTo("1")==0 || type.compareTo("2")==0)
    {
    validateType=true;
    }
    else {validateType=false;}
    }
    write(id,"idList.txt");
    write(name,"nameList.txt");
    write(password,"passwordList.txt");
    write(type,"userTypeList.txt");
    }
    }
    public static void addModule(String[] moduleCodeList,String[] moduleNameList, int noOfModules) throws IOException
    {
    if(noOfModules>=10)
    {
    System.out.println("Sorry but there are maximum amount of modules has been registered allready");
    }
    else
    {
    int valid=0;
    String id="";
    while (valid!=-1)
    {
    id=getString("Please enter CODE for the module : ");
    valid=findStringInList(moduleCodeList,id,noOfModules);
    if(valid!=-1)
    {
    System.out.println("This CODE is already in use! Please choose different ID!");
    }
    }
    String name=getString("Please enter name of module: ");
    write(id,"moduleCodeList.txt");
    write(name,"moduleNameList.txt");
    }

    }
    public static int getInt(String text)
    {
    int x=0;
    Scanner kb=new Scanner(System.in);
    System.out.print(""+text);

    while(!kb.hasNextInt())
    {
    System.out.print("Error! please make sure you entered an integer\n"+text);
    }
    x=kb.nextInt();
    return x;

    }
    public static void displayList(String[] list, int noInList)
    {
    for(int i=0; i<noInList;i++)
    {
    System.out.println((i+1)+". "+list);
    }
    }
    public static void displayList(String[] list,String[] list2, int noInList)
    {
    for(int i=0; i<noInList;i++)
    {
    System.out.println("\t"+(i+1)+". "+list + " " + list2);
    }
    }
    public static String[] read(String fileName) throws IOException {
    // TODO Auto-generated method stub
    File authors=new File(fileName);
    int j=0;
    String[] name=new String[10];


    Scanner scanFile = new Scanner(authors);

    while (scanFile.hasNextLine())
    {
    name[j]=scanFile.nextLine();
    j++;
    }

    scanFile.close();

    return name;
    }
    public static int getNumberOfList(String fileName) throws IOException {
    // TODO Auto-generated method stub
    File authors=new File(fileName);
    int j=0;
    String[] name=new String[10];


    Scanner scanFile = new Scanner(authors);
    while (scanFile.hasNextLine())
    {


    name[j]=scanFile.nextLine();
    j++;
    }
    scanFile.close();
    return j;
    }
    public static void write(String text,String fileName) throws IOException {
    // TODO Auto-generated method stub
    PrintWriter writeIn = new PrintWriter(new FileWriter(fileName, true));
    writeIn.println(text);
    writeIn.close();

    }
    public static void listOfStudentsRegistered(String[] idList,String[] passwordList,String[] nameList,String[] userTypeList,int noOfUsers) throws IOException {
    // TODO Auto-generated method stub
    for(int i=0;i<noOfUsers;i++)
    {
    System.out.println(idList+" "+nameList+" : ");
    String fileName=idList + ".txt";
    File students=new File(fileName);
    int j=0;
    String[] modules={"","","",""};
    Scanner scanFile = new Scanner(students);

    while (scanFile.hasNextLine())
    {
    modules[j] = scanFile.nextLine();
    j++;
    }
    scanFile.close();
    for(int n=0;n<j;n++)
    {
    System.out.println(""+modules[j]);
    }
    }


    }
    public static void register(String studentID, String[] moduleCodeList,String[] moduleNameList, int noOfModules) throws IOException {
    // TODO Auto-generated method stub
    String fileName = studentID + ".txt";
    displayList(moduleCodeList, moduleNameList,noOfModules);
    String text="";
    String module = getString("Please enter the code of a module that you want to register for : ");
    int index = findStringInList(moduleCodeList,module,noOfModules);
    if (index==-1)
    {
    System.out.println("Sorry but your registration was unsuccesful! Please make sure you are entering a correct module code!");
    }
    else
    {
    System.out.println("Congrads you have registered for " + moduleNameList[index] + " module.");
    text=moduleCodeList[index] + " " + moduleNameList[index];
    write(text,fileName);
    }

    }
    public static void printTitle(String name)
    {
    System.out.println("

    ");
    System.out.println("Welcome " + name + " CIT MODULE REGISTRATION PROGRAM");
    System.out.println("
    ");
    }
    }


  • Registered Users Posts: 37,485 ✭✭✭✭Khannie


    Ah here youngfella. This is clearly your homework. I feel a bit sorry for you though.....

    The filename comes from this line:
    String fileName = studentID + ".txt";

    when this line is called:

    write(text,fileName);

    it should create the file with this command in the write method:
    PrintWriter writeIn = new PrintWriter(new FileWriter(fileName, true));

    If you look at the docs for FileWriter: http://docs.oracle.com/javase/7/docs/api/java/io/FileWriter.html
    it should create and append to the file. If not, something is wrong and it will throw an IOException. This all relies on studentID existing and the directory being writeable (otherwise it will just create a file called ".txt".

    Are you getting some kind of error?


  • Moderators, Technology & Internet Moderators Posts: 11,015 Mod ✭✭✭✭yoyo


    This isn't the place to ask for advice on your project work. if you have specific queries you can ask them over in the Development forum. The javadocs Khannie linked should help

    Nick


This discussion has been closed.
Advertisement