![]() |
|
#1
|
||||
|
||||
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:
Last edited by admin : 11-Nov-2005 at 20:42.
Reason: Please insert your JAVA code between [java] & [/java] tags
|
|
#2
|
||||
|
||||
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:
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
|
||||
|
||||
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
|
||||
|
||||
Re: String pad.Please be more clear.
Quote:
Quote:
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
|
||||
|
||||
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
|
||||
|
||||
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:
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:
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:
JAVA Code:
JAVA Code:
So, we have created a character array successfully. Now, onto converting it to a string and returning it to the main function: JAVA Code:
So, the function will look like: JAVA Code:
In your main function, we should call like this: JAVA Code:
Did you understand this? So, the output string will be the like this: "c_________" Code:
Now, onto padding with boolean, long and double: First, consider this one: Quote:
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:
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:
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:
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
|
||||
|
||||
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 GIDBlog
Stupid Management Policies by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
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