Hi guys,

I've got the program running, but not as how the instructor requested. The professor wants us to ask the user for a text file and classify the data accordingly.

Example:

E = Enter
L = Leave

So if I have a text file with the following info

E User1
E User2
L User3
E User4

it should add the E's to a list, and remove the L's from the list.

The program I have written does this but in a different manner. My program provides the user with options, asking if user is going to enter/leave and adds/removes respectively. My program, adds/removes users one by one.

So what I'm asking is, how can I do this from the text file? I'm guessing I would need to use something like isLetter to identify the first letter of each line and categorize accordingly?

Any tip or example gearing me to the right direction would greatly be appreciated.

Thanks in advance :)

Edited 4 Years Ago by tformed

how can I do this from the text file

Read a line from the text file, extract the first letter of the line and use an if statement to test it and do what it requests to be done.

For the file input I currently have

`public static void main( String [ ] args ) 
    {
        String fileName = "test.txt";

        try
        {
            File f = new File( fileName );
            Scanner scanbyLine = new Scanner( f );

            char E = 'E';
            char L = 'L';

            while (scanbyLine.hasNextLine ())
            {

            //if line starts with e then do accordingly
            //else if line starts with l then do accordingly


            }
             catch( IOException e )
        {
            System.err.println( "Error handling file " + fileName + ":" + e );


        `

How would I extract the first character per line?

Read the line into a String and use one of the String class's methods to extract the first character.

i think you have to do something like following.

String str=scanbyLine.nextLine();
if(str.indexOf('E')==0)
{
//then do whatever for E
}
elseif(str.indexOf('L')==0)
{
//then do whatever for L
}
else
{
//no string starting with e orl
}

thank you.

This article has been dead for over six months. Start a new discussion instead.