Sajith
Sajith

Reputation: 113

Passing tokens to a function in java

I have a text file named test5 which have 4 words. I need to pass these four words to both functions contVal and contVal2. When i execute the below code the first and third word goes to function contVal and second and forth word goes to contVal2. Is there any way i can pass all four words to both functions?

                String a = "D:/test5.txt" ;
                String abc = readFile(a);
                StringTokenizer st = new StringTokenizer(abc);
                float cv1 = 1;
                float cv4 =1;
                while (st.hasMoreTokens()) 
                {
                    float cv0 = contVal(a1,st.nextToken());
                    cv1 = cv0*cv1;
                    float cv3 = contVal2(a1,st.nextToken());
                    cv4=cv3*cv4;

                }

Upvotes: 1

Views: 795

Answers (1)

Eran
Eran

Reputation: 393811

Each call to st.nextToken() consumes another token. Therefore each method gets just half of the tokens.

Store each token in a variable before passing it to the methods :

            while (st.hasMoreTokens()) 
            {
                String token = st.nextToken();
                float cv0 = contVal(a1,token);
                cv1 = cv0*cv1;
                float cv3 = contVal2(a1,token);
                cv4=cv3*cv4;
            }

Upvotes: 2

Related Questions