1

I have made an ArrayList.txt which contains a list of students with their grades, a separate class student containing getName(), getGrade(), getMarks() etc and on my main class, I have a main method which will pull from this method

Hence my method goes as this:

public static String findName(ArrayList<Student> o, String name) {

    for(int i = 0; i < o.size(); i++)
    {
        if(o.get(i).getName().startsWith(name.toLowerCase()))
            System.out.print(o.get(i));
    }
    return name;
}

and my main method has this findName(students, "al");

my array.txt looks something like this

afafwafa
B
80
alicegg
C
70
lfhif
D
50
Allllleeee
A
94

however my findName() method is unable to pull all names that starts with "al" even though all my other methods are working. Also I need to take note that the method should be case insensitive and i need to use .startsWith(). Any suggestions?

To add on, with the given ArrayList of type student with a string file name, i need to create a method that will write the members of the arraylist to a text file with a given name with 1 element to a line using student class toString method format. i need to handle possible exceptions with try/catch. I also need to use either filereader, fileoutputsteam, printwriter, printstream in the code

public static void nameToFile(ArrayList<Student> a, String file) {      

    int i = inone.read();
    FileReader inone = new FileReader(a.get(i).toString());
    FileOutputStream fos = new FileOutputStream(file);
    PrintWriter outone = new PrintWriter(fos);

    outone.close();
 }      

my main method has this

nameToFile(students2,"modified.txt");

1
  • 1
    Put a toLowerCase() after getName() too? Commented Feb 28, 2017 at 19:02

1 Answer 1

2

Also i need to take note that the method should be case insensitive and i need to use .startsWith().

If you want your startsWith() method to ignore the case, just convert both to lower or upper case before comparison:

if(o.get(i).getName().toLowerCase().startsWith(name.toLowerCase()))

If your findName() is supposed to just return 1 name, you can return it in the if-condition. If you want to return a list of names, add it to another arraylist, then return the arraylist.

Sign up to request clarification or add additional context in comments.

3 Comments

adding a toLowerCase works! wow i did not realise it was that simple. thanks! as for writing member of ArrayList to a text file, any suggestions?
An alternative that I never feel gets enough credit is o.get(i).getName().regionMatches(true, 0, name, 0, name.length()).
@FinFin For writing you've got all the methods of PrintWriter at your disposal. Note that System.out is a PrintWriter as well so you may already be familiar with the interface. You appear to be opening the output file correctly, all that's left is to print[ln] to your heart's content for each item you want to write, then close the file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.