Solved

Java: problems with for loop? any thoughts... thx

Posted on 2012-03-27
7
265 Views
Last Modified: 2012-03-27
Hello Experts,

Having problems with the loop below; it may not be a loop... I am not sure...

any thoughts will help ... thx..

based on a home code; need to find all materials belonging to that house... can't get it to work... thx again

public class SearchHomeMaterials {

	public static void main(String[] args) {		
		//log source
		String source = " home s78i has featureNumber E7i10 built with material Wood " +
				"E7i10 has a kitchen, two baths, three bedrooms and bar built with material 4397 " +
				" home 67cf6 has featureNumber E2410 built with material890217 " +
				"home s78i contains a tree, chairs, and front door with material Plastic " +
				" home 65ac has featureNumber D7i10 using material4397  material Plywood " +
				"home s78i posseses a patio, closet doors, and cabinets have material Glass " +
				" home 67cf6 has featureNumber D5844 built with material 4446" +
				"D5844 has a gold window with material5643" +
				" home 33zt has featureNumber E4ty8 built with materialm111" +
				"E4ty8 has 7 doors and 3 shelves built with material Wood";	
		
		//pseudo: 1: search materials belonging to this home
		String home = "s78i";		
		String material = "material";
		
		//pseudo: 2: add materials found for house s78i to vector
		Vector<String> homeMaterials = new Vector<String>(); 	
		String logs[] = source.split("[\\s]"); 
		boolean addMaterial = false;

		for (String log : logs) {
			if (addMaterial) {
				// add material found for that house: Wood, Plastic, Glass to vector
				if(log.equals(material)){
					homeMaterials.add(log);
				}
				addMaterial = false;
			}

			// if home searched found, add material true
			if (log.equals(home)) {
				addMaterial = true;
			}
		}		
		System.out.println(homeMaterials);       
        
	}//END OF MAIN
	
}//END OF CLASS

Open in new window

0
Comment
Question by:epifanio67
  • 5
  • 2
7 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 37774153
Now it prints materials:

import java.util.Vector;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class SearchHomeMaterials {

	public static void main(String[] args) {
		//log source
		String source = " home s78i has featureNumber E7i10 built with material Wood " +
				"E7i10 has a kitchen, two baths, three bedrooms and bar built with material 4397 " +
				" home 67cf6 has featureNumber E2410 built with material890217 " +
				"home s78i contains a tree, chairs, and front door with material Plastic " +
				" home 65ac has featureNumber D7i10 using material4397  material Plywood " +
				"home s78i posseses a patio, closet doors, and cabinets have material Glass " +
				" home 67cf6 has featureNumber D5844 built with material 4446" +
				"D5844 has a gold window with material5643" +
				" home 33zt has featureNumber E4ty8 built with material m111" +
				"E4ty8 has 7 doors and 3 shelves built with material Wood";

		//pseudo: 1: search materials belonging to this home
		String home = "s78i";
		String material = "material";

		//pseudo: 2: add materials found for house s78i to vector
		Vector<String> homeMaterials = new Vector<String>();

        Pattern p = Pattern.compile("material[\\s]+(\\w+)\\s");
        Matcher m = p.matcher(source);

        while(m.find()){


            System.out.println(m.group(1));
            homeMaterials.add(m.group(1));
        }


		String logs[] = source.split("material[\\s]+");
		boolean addMaterial = false;

		for (String log : logs) {
			if (addMaterial) {

                System.out.println(log);
				// add material found for that house: Wood, Plastic, Glass to vector
				if(log.equals(material)){
					homeMaterials.add(log);
				}
				addMaterial = false;
			}

			// if home searched found, add material true
			if (log.equals(home)) {
				addMaterial = true;
			}
		}
		System.out.println(homeMaterials);

	}//END OF MAIN

}//END OF CLASS

Open in new window



Output:

Wood
4397
Plastic
Plywood
Glass
4446D5844
m111E4ty8
[Wood, 4397, Plastic, Plywood, Glass, 4446D5844, m111E4ty8]

Open in new window

0
 
LVL 47

Expert Comment

by:for_yan
ID: 37774170
That would be better as some of them don't have space after material

import java.util.Vector;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class SearchHomeMaterials {

	public static void main(String[] args) {
		//log source
		String source = " home s78i has featureNumber E7i10 built with material Wood " +
				"E7i10 has a kitchen, two baths, three bedrooms and bar built with material 4397 " +
				" home 67cf6 has featureNumber E2410 built with material890217 " +
				"home s78i contains a tree, chairs, and front door with material Plastic " +
				" home 65ac has featureNumber D7i10 using material4397  material Plywood " +
				"home s78i posseses a patio, closet doors, and cabinets have material Glass " +
				" home 67cf6 has featureNumber D5844 built with material 4446" +
				"D5844 has a gold window with material5643" +
				" home 33zt has featureNumber E4ty8 built with materialm111" +
				"E4ty8 has 7 doors and 3 shelves built with material Wood";

		//pseudo: 1: search materials belonging to this home
		String home = "s78i";
		String material = "material";

		//pseudo: 2: add materials found for house s78i to vector
		Vector<String> homeMaterials = new Vector<String>();

        Pattern p = Pattern.compile("material[\\s]*(\\w+)\\s");
        Matcher m = p.matcher(source);

        while(m.find()){


            System.out.println(m.group(1));
            homeMaterials.add(m.group(1));
        }


		String logs[] = source.split("material[\\s]+");
		boolean addMaterial = false;

		for (String log : logs) {
			if (addMaterial) {

                System.out.println(log);
				// add material found for that house: Wood, Plastic, Glass to vector
				if(log.equals(material)){
					homeMaterials.add(log);
				}
				addMaterial = false;
			}

			// if home searched found, add material true
			if (log.equals(home)) {
				addMaterial = true;
			}
		}
		System.out.println(homeMaterials);

	}//END OF MAIN

}//END OF CLASS

Open in new window


Output:

Wood
4397
890217
Plastic
4397
Plywood
Glass
4446D5844
5643
m111E4ty8
[Wood, 4397, 890217, Plastic, 4397, Plywood, Glass, 4446D5844, 5643, m111E4ty8]

Open in new window

0
 

Author Comment

by:epifanio67
ID: 37774183
Thank you for_yan...
however, the solution above is printing all the materials...
I need to print all the material that belong to home s78i.... excluding the other ones... see what I am?

thx so much for your help...

regards
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 47

Expert Comment

by:for_yan
ID: 37774185
split{) method is not very suitable for this case - you rather want t use Pattern and find pattern wehre the word material if ollowed by the next word and you want toretrieve the next word - ans dthat what it is doing now
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37774208
this will do only for s78i:

import java.util.Vector;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class SearchHomeMaterials {

	public static void main(String[] args) {
		//log source
		String source = " home s78i has featureNumber E7i10 built with material Wood " +
				"E7i10 has a kitchen, two baths, three bedrooms and bar built with material 4397 " +
				" home 67cf6 has featureNumber E2410 built with material890217 " +
				"home s78i contains a tree, chairs, and front door with material Plastic " +
				" home 65ac has featureNumber D7i10 using material4397  material Plywood " +
				"home s78i posseses a patio, closet doors, and cabinets have material Glass " +
				" home 67cf6 has featureNumber D5844 built with material 4446" +
				"D5844 has a gold window with material5643" +
				" home 33zt has featureNumber E4ty8 built with materialm111" +
				"E4ty8 has 7 doors and 3 shelves built with material Wood";

		//pseudo: 1: search materials belonging to this home
		String home = "s78i";
		String material = "material";

		//pseudo: 2: add materials found for house s78i to vector
		Vector<String> homeMaterials = new Vector<String>();

        String []  homes = source.split("home");

        for(String homeS: homes) {
            if(homeS.indexOf("s78i") == -1)continue;
        Pattern p = Pattern.compile("material[\\s]*(\\w+)\\s");
        Matcher m = p.matcher(homeS);

        while(m.find()){


            System.out.println(m.group(1));
            homeMaterials.add(m.group(1));
        }

        }


		String logs[] = source.split("material[\\s]+");
		boolean addMaterial = false;

		for (String log : logs) {
			if (addMaterial) {

                System.out.println(log);
				// add material found for that house: Wood, Plastic, Glass to vector
				if(log.equals(material)){
					homeMaterials.add(log);
				}
				addMaterial = false;
			}

			// if home searched found, add material true
			if (log.equals(home)) {
				addMaterial = true;
			}
		}
		System.out.println(homeMaterials);

	}//END OF MAIN

}//END OF CLASS

Open in new window


Output:
Wood
4397
Plastic
Glass
[Wood, 4397, Plastic, Glass]

Open in new window

0
 
LVL 47

Accepted Solution

by:
for_yan earned 500 total points
ID: 37774218
Thsi is the same thing as above but using your variable home, rather than repeating "s78i"

import java.util.Vector;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class SearchHomeMaterials {

	public static void main(String[] args) {
		//log source
		String source = " home s78i has featureNumber E7i10 built with material Wood " +
				"E7i10 has a kitchen, two baths, three bedrooms and bar built with material 4397 " +
				" home 67cf6 has featureNumber E2410 built with material890217 " +
				"home s78i contains a tree, chairs, and front door with material Plastic " +
				" home 65ac has featureNumber D7i10 using material4397  material Plywood " +
				"home s78i posseses a patio, closet doors, and cabinets have material Glass " +
				" home 67cf6 has featureNumber D5844 built with material 4446" +
				"D5844 has a gold window with material5643" +
				" home 33zt has featureNumber E4ty8 built with materialm111" +
				"E4ty8 has 7 doors and 3 shelves built with material Wood";

		//pseudo: 1: search materials belonging to this home
		String home = "s78i";
		String material = "material";

		//pseudo: 2: add materials found for house s78i to vector
		Vector<String> homeMaterials = new Vector<String>();

        String []  homes = source.split("home");

        for(String homeS: homes) {
            if(homeS.indexOf(home) == -1)continue;
        Pattern p = Pattern.compile("material[\\s]*(\\w+)\\s");
        Matcher m = p.matcher(homeS);

        while(m.find()){


            System.out.println(m.group(1));
            homeMaterials.add(m.group(1));
        }

        }


		String logs[] = source.split("material[\\s]+");
		boolean addMaterial = false;

		for (String log : logs) {
			if (addMaterial) {

                System.out.println(log);
				// add material found for that house: Wood, Plastic, Glass to vector
				if(log.equals(material)){
					homeMaterials.add(log);
				}
				addMaterial = false;
			}

			// if home searched found, add material true
			if (log.equals(home)) {
				addMaterial = true;
			}
		}
		System.out.println(homeMaterials);

	}//END OF MAIN

}//END OF CLASS

Open in new window


Wood
4397
Plastic
Glass
[Wood, 4397, Plastic, Glass]

Open in new window

0
 

Author Closing Comment

by:epifanio67
ID: 37774237
thank you for_yan...
I see what you did... thank you so much....

boy, I have long way to go with Java... thx for your patience....
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
groupSumClump challenge 9 102
JUnit 4 @Before and @BeforeClass differences 3 48
replace a word with other 1 40
Arrays.asList  VS  ArrayList 4 45
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This video teaches viewers about errors in exception handling.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now