Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with using same String name over and over, I assume :)

Status
Not open for further replies.

JProg

Programmer
Apr 4, 2002
88
JP
Hey everyone,

I have written some code to find all of the e characters that occur within the command line arguments to my program. If there are no e's then there are no problems with the code execution, additionally this code compiles fine and runs fine... "sort of". The thing is that any command line argument that has an e in it causes the program to simply hang :)

I have never waited long enough to see if this program will do what it is supposed to do, however on a Pentium 4 I can't see why it should take longer than a couple of seconds.

The code is as follows

class ExerciseTenPointThree
{
public static void main(String[] args)
{
if(args.length > 0)
{
EasyGoing findEs = new EasyGoing(args);
System.out.println("Your strings contained " + findEs.findEs() + " e's :)");
}
else
{
System.out.println("Please supply one or more command line arguments.");
}
}
}

class EasyGoing
{
private String[] allStrings;
private int masterCountOfEs;

EasyGoing(String[] progArguments)
{
this.allStrings = progArguments;
}

public int findEs()
{
for(int i=0; i<allStrings.length; i++) //Performed for each program argument string!
{
String currentString = allStrings;

while(currentString.indexOf("e") != -1)
{
int location = currentString.indexOf("e");

if(location != -1)
{
masterCountOfEs++; //An "e" has been found :)
currentString = currentString.substring(location);
}
}

}
return masterCountOfEs;
}


}

I figure that there is something wrong with reusing the same String name over and over... so I probably need to modify the use of the currentString variable. However I am not 100% sure of what is going on, so any suggestions / input will be really appreciated.

Thanks heaps

Regards

Davo
 
Hi JProg.

Your code was not working because in the line where you call substring, you were substring'ing from the current location of the found 'e' to the end of the string - so your loop was infinite because if you found an e - the first character of the new string was that 'e' if you see what I mean.

Below is your original code fixed (notice the '+1').

Code:
class ExerciseTenPointThree
{
    public static void main(String[] args)
    {
        if(args.length > 0)
        {
            EasyGoing findEs = new EasyGoing(args);
            System.out.println("Your strings contained " + findEs.findEs() + " e's :)");
        }
        else
        {
            System.out.println("Please supply one or more command line arguments.");
        }
    }
}

class EasyGoing
{
    private String[] allStrings;
    private int masterCountOfEs;

    EasyGoing(String[] progArguments)
    {
        this.allStrings = progArguments;
    }

    public int findEs()
    {
        for(int i=0; i<allStrings.length; i++)        //Performed for each program argument string!
        {
            String currentString = allStrings[i];

            while(currentString.indexOf("e") != -1)
            {
                int location = currentString.indexOf("e");

                if(location != -1)
                {

                    masterCountOfEs++;    //An "e" has been found :)
                    currentString = currentString.substring(location+1);
                }
            }

        }
        return masterCountOfEs;
    }


}

However, I would say the below code, a slight variation on your original code is slightly more easy to understand :

Code:
class ExerciseTenPointThree
{
    public static void main(String[] args)
    {
        if(args.length > 0)
        {
            EasyGoing findEs = new EasyGoing(args);
            System.out.println("Your strings contained " + findEs.findEs() + " e's :)");
        }
        else
        {
            System.out.println("Please supply one or more command line arguments.");
        }
    }
}

class EasyGoing
{
    private String[] allStrings;
    private int masterCountOfEs;

    EasyGoing(String[] progArguments)
    {
        this.allStrings = progArguments;
    }

    public int findEs()
    {
        for(int i=0; i<allStrings.length; i++)        //Performed for each program argument string!
        {
            String currentString = allStrings[i];

			char[] chars = currentString.toCharArray();
			for (int j = 0; j < chars.length; j++) {
				if (chars[j] == 'e')
					masterCountOfEs++;
			}

        }
        return masterCountOfEs;
    }


}

--------------------------------------------------
Free Database Connection Pooling Software
 
sedj,

Legend, thanks for finding the quirck in my code. I couldn't work out what was going on! I have looked at your code and it looks a lot more succinct than mine, however the exercise that I am doing (from a text book) demands that I use only methods from the string class. None the less I will keep a copy of your code, because if I ever encounter this kind of requirement in projects that I am undertaking I will shorely use your way (simpler, easier, etc :). Thanks again for your assistance.

Regards

Davo
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top