Object orientatted programming and classes

Associate
Joined
19 Jul 2006
Posts
1,847
Really strugling to get my head round OOP.

Im trying to learn Java as well.
Im wanting to do a what i think is a small app. to create usernames from a csv file

If I have 2 files, one a csv file containing firstname , surname and the other is just a wordlist.txt file.

And my output is to a new csv file with firstinitial.surname, firstname,surname,password (from a word list)

I have this so far.

Code:
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
      try
      {
         Readfile read = new Readfile();
         read.run();
      }
      // Report any IOExceptions which occur
      catch (IOException IOEx)
      {
         System.out.println("Problems reading file " + IOEx);
      }
   }
}

and then

Code:
import java.io.*;
import java.util.*;
/**
 **
 * 
 */

public class Readfile {

     private FileReader fr;
     private BufferedReader br;
     private PrintWriter pw;

     public void run() throws IOException
    {
        String nextLine;
        Scanner sc = new Scanner(new File("export.csv"));
        while (sc.hasNext())
        {
            nextLine = sc.nextLine();
            System.out.println(nextLine);
        }

        sc.close();
    }

So i can read in from one file and output to the output bar, i take it i will need to make new line an array so i can split the line up with stringtokenizer??

Im old school so not got my head around none linear programming as such.

what kinda classes or methods should i have,

IF you can help thank you so much in advance
 
Last edited:
Thanks drak3.

I have a good book that explains it in cat and dog speak that i understand but the putting it into practice is another thing.

ok I got this so far
Code:
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package gmailcsvapp;



/**
 *
 * @author 
 */

import java.io.*;
import java.io.FileNotFoundException;
public class Main {

    /**
     * @param args the command line arguments
     */
    
    public static void main(String args[]) throws FileNotFoundException, EmptyFileException
    {
        String importfile = "export.csv";

      try
        {
            Readfile reader1 = new Readfile(importfile);
            while (reader1.iter.hasNext())
           
            {
               System.out.println(reader1.getNextLine()+reader1.password());
            }
        }
        catch (EmptyFileException message){
            System.out.println(importfile + " threw a Empty File Exception; the details were as follows - " + message);
        }
       catch (FileNotFoundException message){
            System.out.println(importfile + "Throw a File Not Found Error");
       }
    }
}

and then this
Code:
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package gmailcsvapp;



import java.io.*;
import java.util.*;

public class Readfile
{
   public File file;
   

   public ArrayList<String> userCollection = new ArrayList<String>();
   public Iterator <String>iter;
   public static final String NONE_LEFT = "";

   

   public Readfile(String filename) throws FileNotFoundException, EmptyFileException
    {
        file = new File(filename);
        Scanner sc = new Scanner(file);

        if(sc.hasNext())
        {
           while(sc.hasNext())
           {
               //assuming that each line only has one word (would add additional logic to split lines here if necessary)
               userCollection.add(sc.nextLine().toLowerCase());
           }

        }
        else
        {
           throw new EmptyFileException(filename +  " contains no words");
        }
        iter = userCollection.iterator();
    }




    public String getNextLine()
    {
        if(iter.hasNext())
        {
            return iter.next();
        }
        else
        {
            return NONE_LEFT;
        }
    }


    public ArrayList<String> passwordCollection = new ArrayList<String>();
    public Iterator <String>iterpass;
    public File file1;

       public String password() throws FileNotFoundException, EmptyFileException
   {
       file1 = new File("passwordlist.txt");
       Scanner Pass = new Scanner(file1);

        if(Pass.hasNext())
        {
           while(Pass.hasNext())
           {
               //assuming that each line only has one word (would add additional logic to split lines here if necessary)
               passwordCollection.add(Pass.nextLine().toLowerCase());
           }

        }
        else
        {
           throw new EmptyFileException("passwordlist.txt contains no words");
        }
        iterpass = passwordCollection.iterator();
        long number = (Math.round(Math.random() * 100));
        Random generator = new Random();
        int index = generator.nextInt ( passwordCollection.size() );
        if( index >-1 ) return ( ","+passwordCollection.get(index)+number);



       return (","+iterpass.next()+number);
   }

}

Ignorring the password bit which works, this now prints out, firstname,surname,and a randompassword.

anypointers on getting the fitstinitial.surname at the begining
 
Thank you so much for that, I slowly understanding it,

Got it to do everything it needs to do :)

How difficult would it be to now use some kind of file selector to select a file to open and a file name to export it to rather than it been hard coded
 
Last edited:
Back
Top Bottom