beginner java program, output problems

  • (2 Pages)
  • +
  • 1
  • 2

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

#1 jruss1212  Icon User is offline

  • D.I.C Head

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

beginner java program, output problems

Posted 04 February 2012 - 06:31 AM

Hi I am a beginner to programming and currently taking an intro to java class. I need to write a program that prompts the user to enter a line, and then it is supposed look at each char individually to see if the char needs to be changed for the output line. Basically wherever there is a space, a + needs to be in the spot for the output. So if they enter "I love java", the output should be "I+love+java". For some reason it keeps printing the space and then the + sign (ex. I +love +java). Is the problem in my if statement in the for loop?? The code is below:

public static void main(String[] args)
	{
		Scanner keyboardScanner = new Scanner(System.in);
		
		System.out.println("Enter a line of text to be URL encoded"); 
	    String line = keyboardScanner.nextLine();
	    System.out.println("The string read is:	" + line);
	    System.out.println("Length in chars is:	" + line.length());
		
	    String newLine = "";
	    
	    for(int n = 0; n < line.length(); ++n)
	    {
	    	char c = line.charAt(n) ;
	    	String hexValue = Integer.toHexString(c);
	    	
	    	newLine = newLine + c;
	    	
	    	if(' ' == c)
	    	{
	    		newLine = newLine + '+';
	    	}
	    	
	    	
	    }
	    System.out.println(newLine);
}


Is This A Good Question/Topic? 0
  • +

Replies To: beginner java program, output problems

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2197
  • View blog
  • Posts: 5,224
  • Joined: 10-September 10

Re: beginner java program, output problems

Posted 04 February 2012 - 06:39 AM

The problem is line 17 in that you add the existing space character before determining if it should be replaced by a '+'.

This would be a good place for an if/else statement.

This post has been edited by GregBrannon: 04 February 2012 - 06:40 AM

Was This Post Helpful? 0
  • +
  • -

#3 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 - 06:42 AM

<Removed self quote>

This post has been edited by smohd: 04 February 2012 - 07:13 AM

Was This Post Helpful? 0
  • +
  • -

#4 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2197
  • View blog
  • Posts: 5,224
  • Joined: 10-September 10

Re: beginner java program, output problems

Posted 04 February 2012 - 06:44 AM

Try again.
Was This Post Helpful? 0
  • +
  • -

#5 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 - 06:46 AM

So would I need to delete that line or am I supposed to place it somewhere inside or after the if statement? Is the body of the if statement correct in the sense that I'm concat a + to the output? This is really difficult for me because I'm new to this and my teacher is not doing such a great job, hence the reason I'm here.
Was This Post Helpful? 0
  • +
  • -

#6 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2197
  • View blog
  • Posts: 5,224
  • Joined: 10-September 10

Re: beginner java program, output problems

Posted 04 February 2012 - 06:54 AM

I've found that I learn best when I experiment and discover the answer myself. Everyone learns differently, so it may not be true for you. Even so, I suggest you try the options you suggested and see what happens.

I'll add to my if/else hint by saying that line 17 is a perfectly good and necessary statement, and I already suggested that it was out of order by doing something "before" it should. Use those hints to experiment a bit and discover the right answer. You've a good start with this program. It just needs a slight adjustment.

If you don't find the answer fairly quickly, come back. And come back to let us know when you do discover it.
Was This Post Helpful? 1
  • +
  • -

#7 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 - 07:20 AM

If I move line 17 after the if statement it prints a + and then the space. If I put it inside it just prints + signs. I'm not sure how I should put an if/else statement there but according to our directions it looks like we only need it for replacing the space. The program is not done yet either, next it should say else/if letter or number it remains unchanged and then else/if print its hexvalue. I'm not there yet though because I can't get past this point.
Was This Post Helpful? 0
  • +
  • -

#8 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2197
  • View blog
  • Posts: 5,224
  • Joined: 10-September 10

Re: beginner java program, output problems

Posted 04 February 2012 - 07:23 AM

BIGGER hint:

if ( ' ' != c )
{
   // do this
}
else
{
   // do the other thing
}

Either 'this' or 'the other thing' is where your line 17 should be.
Was This Post Helpful? 0
  • +
  • -

#9 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 - 07:48 AM

That did the trick! It makes total sense to me too. Now it tests each character to see if it is a plus sign and needs to be changed. The way I had it, it was printing the space and then adding a plus. That was such a big help, thank you very much sir! =]
Was This Post Helpful? 0
  • +
  • -

#10 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2197
  • View blog
  • Posts: 5,224
  • Joined: 10-September 10

Re: beginner java program, output problems

Posted 04 February 2012 - 07:51 AM

You're welcome. Glad you got it and understand why it works.
Was This Post Helpful? 0
  • +
  • -

#11 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 - 12:45 PM

Is it possible to assign a bunch of characters to one char variable? The program needs to keep all letters(a-z, and A-Z), all numbers, underscores, dashes, dots, and asterisks the same. Everything else (besides the space which is a + sign) is shown as a '%' followed by that characters hexvalue. So I'm wondering how I can create the if/else statement to leave the character when it needs to be the same and change it to its hexvalue when it is something else. Here is what my if statement looks like:

   for(int n = 0; n < line.length(); ++n)
	    {
	    	char c = line.charAt(n) ;
	    	String hexValue = Integer.toHexString(c);
	    	
	    	if(' ' != c)
	    	{	newLine = newLine + c;
	    	
	    	}
	    	else if(' ' == c)
	    	{
	    		newLine = newLine + '+';
	    	}
	    	else
	    	{
	    		newLine = newLine + '%' + hexValue ;
	    	}
	    	
	    	
	    }

Was This Post Helpful? 0
  • +
  • -

#12 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2197
  • View blog
  • Posts: 5,224
  • Joined: 10-September 10

Re: beginner java program, output problems

Posted 04 February 2012 - 01:21 PM

You can use the numerical characteristics of the char primitive to specify a range of acceptable char values to build conditional statements that include more than a single character.

Here are some simple examples of what I mean. Your if statements could be more complex than these.

    public static void main(String[] args)
    {
        char c = 'c';
        
        System.out.println( Integer.toHexString( c ) );
        
        if ( c > 15 )
        {
            System.out.println( c );
        }
        
        if ( c > 50 )
        {
            System.out.println( "c is greater than 50");
        }
        
        if ( c == 99 )
        {
            System.out.println( "c is exactly 99" );
        }
    }


Edit: you may already have one, but a good ASCII chart is a good tool to have handy when doing an assignment like this one. Here's an example.

This post has been edited by GregBrannon: 04 February 2012 - 01:34 PM

Was This Post Helpful? 0
  • +
  • -

#13 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 - 01:46 PM

So if I use the numerical character of the char primitive, which values should I use in the program going by the link you sent? Should it be Dec, Hx, or Oct? Thanks.
Was This Post Helpful? 0
  • +
  • -

#14 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2197
  • View blog
  • Posts: 5,224
  • Joined: 10-September 10

Re: beginner java program, output problems

Posted 04 February 2012 - 01:50 PM

See my last if statement in the example I sent you? Which base is that number using? Compare it to the results of the first print statement.
Was This Post Helpful? 0
  • +
  • -

#15 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 - 02:27 PM

ok I'll see how it goes, thanks again!
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2