GIDForums  

Go Back   GIDForums > Computer Programming Forums > Java Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 11-Nov-2005, 20:08
pjpav's Avatar
pjpav pjpav is offline
New Member
 
Join Date: Sep 2005
Location: Beautiful NorthEastern Pennsylvania!
Posts: 8
pjpav is on a distinguished road
Angry

String pad.


o.k. I have nine days to get this one in the bag because I am bombing this course!!! ugh! This is my assignment and I have no idea where to start or even what a string pad is? A string pad isn't mentioned in my text, what the heck? Can someone convert this thing into english for me?
What do I have to do?


Recall in Assignment #4 where you utilized the Format class file that was used for formatting output. In this assignment, you will have the opportunity to write your own version of the Format class that supports methods for formatting strings, numbers, characters, and Booleans. The default justification for strings, characters, and Boolean literals is to the left with additional spacing (if needed) added to the right. The default justification for numbers is to the right with leading spaces if necessary.

Samples:

Character: |c |
Boolean: |true |
String: |Hello |
Integer (+): | 12345|
Integer (-): | -347|
Double (+): | 132.26|
Double (+): | 0.01232|
Double (-): | -12.771|
Double (-): | -0.092|

JAVA Code:
/* 
* pad: returns a String of size -width- 
* with the value of "true" or "false" depending on the value of -b- 
* on the string's left, padded to the right with spaces 
* if -width- is less than the minimum size required, override with 
* required size 
*/ 
public static String pad (boolean b, int width) { 
TBS 
} 



/* 
* pad: returns a String of size -width- 
* with the character -c- on the string's left, padded to the right with spaces 
*/ 
public static String pad (char c, int width) { 
TBS 
} 


/* 
* pad: returns a String of size -width- 
* with the value of -number- right justified and blank filled to the left 
* if -width- is less than the minimum size required, override with 
* required size 
*/ 
public static String pad (long number, int width) { 
TBS 
} 


/* 
* pad: returns a String of size -width- 
* with the value of -number- right justified, rounded to -decimal- number of places 
* and blank filled to the left 
* if -width- is less than the minimum size required, override with 
* required size 
*/ 
public static String pad (double number, int width, int decimal) { 
TBS 
}
Last edited by admin : 11-Nov-2005 at 20:42. Reason: Please insert your JAVA code between [java] & [/java] tags
  #2  
Old 11-Nov-2005, 21:13
Paramesh's Avatar
Paramesh Paramesh is offline
Regular Member
 
Join Date: Sep 2005
Location: The Milky Way
Posts: 927
Paramesh is a jewel in the roughParamesh is a jewel in the roughParamesh is a jewel in the rough
Smile

Re: String pad.


Hi pjpav,

String padding is nothing but adding characters or numbers at the end of a string.

So, if we say that:
JAVA Code:
public static String pad (char c, int width) { 
, then we add the character c at the end of the string and width is the size of the string.

Regards,
Paramesh.
__________________

Don't walk in front of me, I may not follow.
Don't walk behind me, I may not lead.
Just walk beside me and be my friend.
  #3  
Old 11-Nov-2005, 21:41
pjpav's Avatar
pjpav pjpav is offline
New Member
 
Join Date: Sep 2005
Location: Beautiful NorthEastern Pennsylvania!
Posts: 8
pjpav is on a distinguished road

Re: String pad.


SO,
for ( hello c);
return width;

???
I have no idea, I am never going to use a computer after this semester!
  #4  
Old 13-Nov-2005, 06:40
Paramesh's Avatar
Paramesh Paramesh is offline
Regular Member
 
Join Date: Sep 2005
Location: The Milky Way
Posts: 927
Paramesh is a jewel in the roughParamesh is a jewel in the roughParamesh is a jewel in the rough

Re: String pad.


Please be more clear.

Quote:
Originally Posted by pjpav
SO,
for ( hello c);
return width;
I couldnt understand what you mention.

Quote:
Originally Posted by pjpav
I have no idea, I am never going to use a computer after this semester!
Even for fun?


Paramesh.
__________________

Don't walk in front of me, I may not follow.
Don't walk behind me, I may not lead.
Just walk beside me and be my friend.
  #5  
Old 13-Nov-2005, 10:36
pjpav's Avatar
pjpav pjpav is offline
New Member
 
Join Date: Sep 2005
Location: Beautiful NorthEastern Pennsylvania!
Posts: 8
pjpav is on a distinguished road

Re: String pad.


I recently read an article that stated that India is going to blow the U.S. out of the water when it comes to technological advances by the year 2020 and I can see why. You said:
" String padding is nothing but adding characters or numbers at the end of a string."

So, if we say that:

JAVA Code:
public static String pad (char c, int width) {
, then we add the character c at the end of the string and width is the size of the string.

I just don't understand this statement although I should. I'm presuming that if my string is ('H', 'E', 'L', 'L', 'O') adding c with padding left justified would be newString ('c' + 'H', 'E', 'L', 'L', 'O') is this correct? My next step would be to compare widths but I don't understand exactly what width I am suppossed to compare the string to, the original HELLO string width? I'm so confused. My text doesn't cover string padding techniques or system overrides so I am completely lost with this project. As far as "never using a computer again" I was just speaking out of frustration. I find myself on java and linux tutorials all of the time and I guess that when I am able to just mess around with programming applications I will be able to put together a program because I won't feel pressured to get it done. I don't know how you know all of this stuff, there is so much information to know. I really appreciate your assistance, with regards. P.J.
  #6  
Old 13-Nov-2005, 12:29
Paramesh's Avatar
Paramesh Paramesh is offline
Regular Member
 
Join Date: Sep 2005
Location: The Milky Way
Posts: 927
Paramesh is a jewel in the roughParamesh is a jewel in the roughParamesh is a jewel in the rough
Arrow

Re: String pad.


Hey pjpav,
Please dont post country issues.
I am already fed of it.

Here is a comprehensive example of string padding:

Let us consider padding a character first.
JAVA Code:
/* 
* pad: returns a String of size -width- 
* with the character -c- on the string's left, padded to the right with spaces 
*/ 
public static String pad (char c, int width) { 
TBS 
} 
The question itself says the answer:
returns a String of size -width-
We should return a string. its size should be width.

with the character -c- on the string's left
The first character of string should be the character c.
So, this padding is different from what i told: adding character to the left or starting of an string.

padded to the right with spaces
All the other characters in the string should be spaces.

So, if we pass like this:
JAVA Code:
String s = pad( 'a' , 10 );

Then the value in the string s should be:
a followed by 10 characters.:
"a_________";
Did you understand this thing?

First, lets discuss how to create a string of a specified number of characters:
The easiest way is to create a character array of size width and then convert the character into string.

Here is how we should do it:
1. Create a character array of size width.
JAVA Code:
char cc = new char[width];
2. Then store all the characters of the array with a space.
JAVA Code:
        for(int i = 0; i < width; i++)
        cc[i] = ' ';                    //make all the values as spaces
3. Store the first character with the character c.
JAVA Code:
        cc[0] = c;              //the first value is padded with character c

So, we have created a character array successfully.
Now, onto converting it to a string and returning it to the main function:
JAVA Code:
        return ( new String(cc) );        //return the new string.

So, the function will look like:
JAVA Code:
    public static String pad (char c, int width) 
    { 
       
        char cc[] = new char[width];     //create a character of given width
        
        for(int i = 0; i < width; i++)
        cc[i] = ' ';                    //make all the values as spaces
        
        cc[width - 1] = c;              //the last value is padded with character c
            
        return ( new String(cc) );        //return the new string.
    } 

In your main function, we should call like this:
JAVA Code:
    public static void main(String[] args) 
    {
        String s;
        
        s=pad('c',10);                  //create a new string of 10 characters and pad with c
        
        System.out.println("The padded string is \"" + s + "\"");          //print and see the output.


    }

Did you understand this?
So, the output string will be the like this: "c_________"
Code:
The padded string is "c "

Now, onto padding with boolean, long and double:
First, consider this one:
Quote:
Originally Posted by question
if -width- is less than the minimum size required, override with required size
What is meant by that?
This is explained by this example:

The padding with boolean would be the easiest thing of these three.
lets consider that:
1. We should return a string with size width and text true or false.
2. Suppose that the width is only three.
3. So, we cannot store either true or false with a size 3.
4. Hence, we should override the width so that it is 4 or 5 depending on whether it is true or false.

So, here is how we do that:
1. First, check for the boolean value. We should use if statements here.
JAVA Code:
if( b == true)
{
2. If it is true, then the returned string should be "true" along with the width.
3. So, we should now check for the size limit. The width should be greater than or equal to 4. Else replace width by 4.
JAVA Code:
if ( width < 4 )
 width = 4;
Do similarly for false.
4. Then create a character array and continue as told in the above method for padding characters.


The next difficult thing is padding with long numbers:

1. First, count the number of digits in the long. How would you do it?
Hint: Use modulus operator: %.
2. Then check whether width is greater than or equal to digits. If no, replace width by digits.
3. So, again, create the character array as before and store all values to be zero.
4. Then we should store the numbers in the array one by one using % operator.
Here comes the difficult thing.: converting integers into characters.
For this, we are creating a character array containing the digit values:
JAVA Code:
char digit[] = { '0' , '1' , '2' , //continues
and we can access the character equivalent of the integer by just calling digit[i].
For example, digit[1] returns '1'. digit[0] return 0.

The same thing goes to double also. One important thing you have to consider is that: you should insert a "." in the middle.

Hope this helped you a bit.
and gave you a start.

If you have any doubts regarding this, please feel free to ask in this forum.

Best Regards,
Paramesh.
__________________

Don't walk in front of me, I may not follow.
Don't walk behind me, I may not lead.
Just walk beside me and be my friend.
  #7  
Old 13-Nov-2005, 13:10
pjpav's Avatar
pjpav pjpav is offline
New Member
 
Join Date: Sep 2005
Location: Beautiful NorthEastern Pennsylvania!
Posts: 8
pjpav is on a distinguished road

Re: String pad.


I sincerely apologize for the international comment, my youngest sister is studying international relations and I picked up an interest in the subject as well. I will not post comments of such content again, I apologize. You helped me greatly with your assistance by clarifying the specifics for me and I am very appreciative of your help. Thanks to you I am confident that I will be able to understand the java language a lot better and I think that pretty soon I will be creating programs of my own :-) Thanks a bunch!
With Regards, P.J.
p.s. I am very sorry for the international remark, it won't happen again. Take Care.
 
 

Recent GIDBlogStupid Management Policies by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Read a .html file, check that file for links salemite C Programming Language 10 17-Jan-2008 08:56
variables return to previous value after i try to set them nasaiya MS Visual C++ / MFC Forum 2 14-Jun-2005 01:43
Help wit my source code compiler errors Krandygrl00 C++ Forum 1 06-Jun-2005 09:14
Converting string to float CT++ C++ Forum 2 10-May-2005 12:29
C++ string from file small_ticket C++ Forum 3 05-Jan-2005 08:29

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 11:54.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.