beginner java program, output problems

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 408 Views - Last Post: 04 February 2012 - 05:49 PM Rate Topic: -----

Topic Sponsor:

#16 jruss1212  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 202
  • Joined: 04-February 12

Re: beginner java program, output problems

Posted 04 February 2012 - 05:03 PM

ok so I have completed my program and everything seems to be working fine. The only question I had now was if the if statement could somehow be simplified. Here it is:

for(int n = 0; n < line.length(); ++n)
	    {
	    	char c = line.charAt(n) ;
	    	String hexValue = Integer.toHexString(c);
	    	
	    	if(c >= 65 && c <= 90 && c != 32)
	    	{	
	    		newLine = newLine + c;
	    	}
	    	else if(c >= 97 && c <= 122 && c != 32)
	    	{
	    		newLine = newLine + c ;
	    	}
	    	else if(c >= 48 && c <= 57 && c != 32)
	    	{
	    		newLine = newLine + c ;
	    	}
	    	else if(c == 95 || c == 45 || c == 46 || c == 42)
	    	{
	    		newLine = newLine + c ;
	    	}
	    	else if(32 == c)
	    	{
	    		newLine = newLine + '+';
	    	}
	    	else
	    	{
	    		newLine = newLine + '%' + hexValue ;
	    	}
	    	
	    	
	    }


Is it possible to combine the top four sections of the if statement? As you can see they all have the same statement(newLine = newLine + c).
Was This Post Helpful? 0
  • +
  • -

#17 GregBrannon  Icon User is offline

  • Ready for water skiing!
  • member icon

Reputation: 1067
  • View blog
  • Posts: 2,701
  • Joined: 10-September 10

Re: beginner java program, output problems

Posted 04 February 2012 - 05:19 PM

Look for conditions that repeat and determine if they have to. For example, c != 32 is included in several of the if statements. If you checked for c == 32 first, you may be able to eliminate those later checks.

You can simplify your newLine = newLine + c statements to:

newLine += c;

Define the String hexValue only if it's actually going to be used.

I would add comments to each of the if statements to describe which cases they cover.

Except for the c == 32 if, you would want to order the if statements from most to least likely. That way the program won't have to check all of the least likely conditions before getting to the most likely.

But I'm wandering into code optimization, and that's probably a waste of time. The compiler already does an excellent job, better than most can do.

Good job!

Edit: And I just saw your "is it possible to combine . . . ?" question. Yes, it is, and you keyed on the right reason for doing so, but it would make the code much harder to read. I'd leave it the way it is for readability.

This post has been edited by GregBrannon: 04 February 2012 - 05:21 PM

Was This Post Helpful? 0
  • +
  • -

#18 jruss1212  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 202
  • Joined: 04-February 12

Re: beginner java program, output problems

Posted 04 February 2012 - 05:49 PM

Thank you! You've been a huge help, I can't thank you enough.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2