for(int i = 0; i < program.size(); i++)
if(program.get(i).contains(remp))

with the coding above, i can search and display all string in my arraylist that contains "hit2080".
for example
HIT2080___Introduction to Programming___ALL___Core___NULL___NULL
HIT1307___Internet Technologies___ALL___Core___NULL___HIT2080

but i just want to search and display the first token that contains string "hit2080". Is there any way to get the first token?

Recommended Answers

All 8 Replies

You can break out of the loop as soon as you have found a match, ie:

for (int i ...) {
  if (prog....) {
     display token
     break;
}

ok, is working if the order is
HIT2080___Introduction to Programming___ALL___Core___NULL___NULL
HIT1307___Internet Technologies___ALL___Core___NULL___HIT2080

but what if the order is
HIT1307___Internet Technologies___ALL___Core___NULL___HIT2080
HIT2080___Introduction to Programming___ALL___Core___NULL___NULL

I don't understand that question.
You asked for the first matching token, and if you present the data in a different order, a different one will be the first. So what's the problem? Maybe there's another part of the specification that you haven't shared with us?

actually i have a textbox receive user input for unit code(ie:hit2080). after the user press the search button, the program will go search every string in text file that contains "hit2080". now my problem is, if my text file order for the string is:

HIT1307___Internet Technologies___ALL___Core___NULL___HIT2080
HIT2080___Introduction to Programming___ALL___Core___NULL___NULL

it will print out the all string having hit2080 no matter the hit2080 at first token or last token. but i just want to print the string with hit2080 in first token only, is there any way to do it?

exactly the way JamesCherrill explained. but maybe you misunderstood him.
can you post the code you use? maybe we'll find a logical error in there.

try{
                    sc = new Scanner(new File("C:\\Users\\Alex Ting\\workspace\\gui\\src\\Files\\units.txt"));
                    while(sc.hasNextLine())
                    {

                        String s1 = sc.nextLine();
                        program.add(s1);
                    }



                    for(int i = 0; i < program.size(); i++)
                    {
                        if(program.get(i).contains(remp))
                        {

                            u=program.get(i);
                            break;


                        }
                    }
if(program.get(i).contains(remp))

the contains method looks for remp anywhere in the line.

if(program.get(i).startsWith(remp))

the startsWith method checks if the line starts with remp. Is that what you wanted?
(NB the parameter for startsWith is a REGEX, but if it's just letters and numbers then the result is the same).

yes, that is the thing i want. thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.