[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

Question
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

7.8

while loop in Java doesn't work when line is split using split method

Asked by cmlane08 in Java Server Pages (JSP), Java Programming Language, New to Java Programming

Tags: java, jsp, programming

I have a nested while loop that works perfectly when I'm only reading one column per line.  I am using the BufferedReader to read a line from a text file and then I'm looping through each token in a StringTokener object.

It works great when there is only one column in the file.  For instance the text file has the values:

red
green
blue

In this scenario the code below will read each line and then assign the value of each row to  the variable "color".    I can then pass this value to another method and the loop continues until the end.   It then returns to the calling method and outputs the counter variable values as intended.

The problem is that now my text file has two columns so it's as follows:

red, pink
green, yellow
blue, purple

So now I need to pass two variables to the paintColors() method in the nested while loop.  

Instead of:

color = st.nextToken();

I need to split the token as follows:

String[] columnValues = line.split(",");

color = columnValues[0];
color2 = columnValues[1];

because now I have two values that must be assigned to variables and passed to the other method.    When I do this however, I encounter an indefinite loop and the while loop won't read the next line.   It also doesn't finish so that my totalItems and changeCounter counter won't output.  I don't think it's important to delve into details on what the paintColors() method does.   Then counter works fine when there's only 1 column in the colorList file so I'm certain that the method is returning properly.  

I simply want to do what I did previously with only 1 column but now I have two columns.  I will post the code that works first and then the code that doesn't work when the second value is added.

Please note that the "colorList" file first contained the following:

red
green
blue

Then I added a second column as follows and it stopped working:

red, pink
green, yellow
blue, purple

Thanks!!
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
private static void countColors() {
 
try{
        BufferedReader bufRdr = new BufferedReader(new FileReader("C:\\colorList..txt"));
 
        String line = null;
 
        int totalItems=0;
        int changeCount=0;
 
        while((line = bufRdr.readline()) != null)
        {
               StringTokenizer st = new StringTokenizer(line, ",");
               
               String color = null;
                    
                      while (st.hasMoreTokens())
                      {
                            color = st.nextToken();
 
                            totalItems++;
                                
                            try {
                                     int returnCount = paintColors(color);
 
                                     changeCount = changeCount + returnCount;
                                 
                                 } catch (Exception e) {
 
                                       System.out.println("ERR: " + e.getMessage());
                                  }
                   }
 
            }
            
              System.out.println("\nProcess Completed !!");
          
              System.out.println("\nTotal Color Count: " + totalItems);
 
              System.out.println("Total Content Updated Count: " + changeCount);
     
     } catch (Exception e) {
 
              System.out.println("ERR: " + e.getMessage());
 
              }
}
Attachments:
 
first colorList file with only 1 column
 
[+][-]09/04/09 12:47 PM, ID: 25262772Accepted Solution

View this solution now by starting your 30-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

About this solution

Zones: Java Server Pages (JSP), Java Programming Language, New to Java Programming
Tags: java, jsp, programming
Sign Up Now!
Solution Provided By: CEHJ
Participating Experts: 2
Solution Grade: A
 
[+][-]09/04/09 07:42 AM, ID: 25260162Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]09/04/09 07:44 AM, ID: 25260177Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]09/04/09 07:52 AM, ID: 25260269Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]09/04/09 07:53 AM, ID: 25260278Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]09/04/09 09:44 AM, ID: 25261289Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]09/04/09 10:11 AM, ID: 25261535Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]09/04/09 10:16 AM, ID: 25261580Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]09/04/09 10:22 AM, ID: 25261637Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]09/04/09 12:27 PM, ID: 25262616Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]09/08/09 10:57 AM, ID: 25284429Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]09/08/09 11:21 AM, ID: 25284594Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]09/08/09 11:25 AM, ID: 25284625Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]09/08/09 11:29 AM, ID: 25284648Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]09/08/09 11:33 AM, ID: 25284699Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]09/08/09 11:38 AM, ID: 25284759Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]09/08/09 11:48 AM, ID: 25284858Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]09/08/09 11:49 AM, ID: 25284874Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]09/08/09 11:56 AM, ID: 25284963Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]09/08/09 12:05 PM, ID: 25285061Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]09/08/09 12:40 PM, ID: 25285422Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]09/11/09 08:44 AM, ID: 25310683Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]09/11/09 09:05 AM, ID: 25310870Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]09/11/09 09:06 AM, ID: 25310877Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]09/11/09 09:30 AM, ID: 25311134Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]09/11/09 09:41 AM, ID: 25311224Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20091111-EE-VQP-92 - Hierarchy / EE_QW_3_20080625