Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JAVA - How to access variables inside a FOR loop, from outside the for loop

Tags:

java

for-loop

My code is to add RSS feeds to a list - and the code originally was only to pull one feed from the first position in a list, and add this object to another list.

This was the original code:

public static List<Feed> getFeedsFromXml(String xml) {
      Pattern feedPattern = Pattern.compile("<feed>\\s*<name>\\s*([^<]*)</name>\\s*<uri>\\s*([^<]*)</uri>\\s*</feed>");


      Matcher feedMatch = feedPattern.matcher(xml);
      while (feedMatch.find()) {
          String feedName = feedMatch.group(1);
          String feedURI = feedMatch.group(2);
          feeds.add(new Feed(feedName, feedURI));
      }

      return feeds;
}

@POST
@Consumes(MediaType.APPLICATION_XML)
@Produces(MediaType.APPLICATION_XML)
public String addXmlFeed() throws IOException
{
    int i = 0;
    String stringXml = "<feed><name>SMH Top Headlines</name><uri>http://feeds.smh.com.au/rssheadlines/top.xml</uri></feed><feed><name>UTS Library News</name>";
    getFeedsFromXml(stringXml);
    Feed f = (Feed) feeds.get(0);
    feedList.add(f);
    String handler = "You have successfully added: \n"; 
    String xmlStringReply = "" + f + "\n";

    feedList.save(feedFile);
    return handler + xmlStringReply;

}

Everything was going well, and then I decided to implement a for loop for handling the adding of more than one feed to the list, and I tried the following (only the code for the second method in question):

@POST
@Consumes(MediaType.APPLICATION_XML)
@Produces(MediaType.APPLICATION_XML)
public String addXmlFeed() throws IOException
{
    int i = 0;
    String stringXml = "<feed><name>SMH Top Headlines</name><uri>http://feeds.smh.com.au/rssheadlines/top.xml</uri></feed><feed><name>UTS Library News</name>";
    getFeedsFromXml(stringXml);
    for (Feed feed: feeds)
    {
        Feed f = (Feed) feeds.get(i++);
        feedList.add(f);
        String handler = "You have successfully added: \n"; 
        String xmlStringReply = "" + f + "\n";
    }

    feedList.save(feedFile);
    return handler + xmlStringReply;

}

Now I'm sure this is a basic problem, but now in the line:

    return handler + xmlStringReply;

handler and xmlStringReply cannot be resolved to a variable as they are within the FOR LOOP.

Is there any easy way around this?

like image 227
Infinitexistence Avatar asked May 20 '26 21:05

Infinitexistence


2 Answers

The scope of those 2 variables is limited to the for loop. To access them outside the loop, you need to increase their scope by declaring them before the loop:

String handler = ""; 
String xmlStringReply = "";
for (Feed f: feeds) {
    feedList.add(f);
    handler = "You have successfully added: \n"; 
    xmlStringReply = "" + f + "\n";
}

feedList.save(feedFile);
return handler + xmlStringReply;

Also, your current code overwrites the value of your strings at each loop, whereas you probably meant to concatenate the values. In that case, you could use a StringBuilder instead of string concatenation:

StringBuilder xmlStringReply = new StringBuilder("You have successfully added: \n");
for (Feed f: feeds) {
    feedList.add(f);
    xmlStringReply.append(f + "\n");
}

feedList.save(feedFile);
return xmlStringReply.toString();
like image 106
assylias Avatar answered May 23 '26 10:05

assylias


The question you need to answer is "what do I want to return if I add several feeds ?".

Maybe you'd like to return "You have successfully added : feed1 feed2 feed3\n"

In that case, the code is :

            StringBuilder response = new StringBuilder( "You have successfully added: ");
            for (Feed feed: feeds)
                {
                    feedList.add(feed);
                    response.append(f.toString()).append(" ");
                }
            feedList.save(feedFile);
            return response.toString();

By the way, your feedand fvariables are just the same and redondant !

Don't write :

int i = 0;    
for (Feed feed: feeds)
{
    Feed f = (Feed) feeds.get(i++);
    feedList.add(f);
}

but

for (Feed feed: feeds)
{
    feedList.add(feed);
}
like image 33
Orabîg Avatar answered May 23 '26 10:05

Orabîg



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!