943,846 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 980
  • Java RSS
You are currently viewing page 1 of this multi-page discussion thread
Jun 30th, 2009
0

print a pascal type char pattern

Expand Post »
i wish to print something like this
******** r
******* er
ter
uter
puter
mputer
omputer
computer
where * is a blank space for each line
i used array of strings
String []s1=new String [20]
for 5 blank spaces i used s1+" " + " "..+(5)""
then 4 blank spaces and print required charcters
i want to know how i can put it in a good logical general code
Last edited by akulkarni; Jun 30th, 2009 at 4:26 pm.
Similar Threads
Reputation Points: 11
Solved Threads: 4
Junior Poster
akulkarni is offline Offline
111 posts
since Jun 2009
Jun 30th, 2009
0

Re: print a pascal type char pattern

Click to Expand / Collapse  Quote originally posted by akulkarni ...
i wish to print something like this
******** r
******* er
ter
uter
puter
mputer
omputer
computer
where * is a blank space for each line
i used array of strings
String []s1=new String [20]
for 5 blank spaces i used s1+" " + " "..+(5)""
then 4 blank spaces and print required charcters
i want to know how i can put it in a good logical general code

If you use code tags when posting (even though it's not code), your spacing won't be stripped out.


[code]
// paste pattern here
[/code]


Java Syntax (Toggle Plain Text)
  1. r
  2. er
  3. ter
  4. uter
  5. puter
  6. mputer
  7. omputer
  8. computer


Use a nested for-loop:

Java Syntax (Toggle Plain Text)
  1. String theWord = "computer";
  2. int numLetters = theWord.length ();
  3.  
  4. for (int numSpaces = numLetters - 1; numSpaces >= 0; numSpaces--)
  5. {
  6. // display numSpaces spaces using for-loop.
  7. // display last numLetters - numSpaces letters of theWord.
  8. // display newline.
  9. }
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,375 posts
since Jan 2008
Jun 30th, 2009
0

Re: print a pascal type char pattern

i am not getting the blank space using for loop.i am worried i dont understand for properly can u please give me the entire program i will be grateful i know to print the letters but it goes left to right
r
er
ter
uter
puter
mputer
omputer
computer
i am not getting the blank spaces
Reputation Points: 11
Solved Threads: 4
Junior Poster
akulkarni is offline Offline
111 posts
since Jun 2009
Jun 30th, 2009
0

Re: print a pascal type char pattern

Click to Expand / Collapse  Quote originally posted by akulkarni ...
i am not getting the blank space using for loop.i am worried i dont understand for properly can u please give me the entire program i will be grateful i know to print the letters but it goes left to right
r
er
ter
uter
puter
mputer
omputer
computer
i am not getting the blank spaces

Please post your results using code tags and post the program that you used to get those results. I can't give you the whole program. It's against the forum rules.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,375 posts
since Jan 2008
Jun 30th, 2009
0

Re: print a pascal type char pattern

java Syntax (Toggle Plain Text)
  1. class gratefultodani
  2. {
  3. public static void main(String args[])
  4. {
  5. String s1="computer";
  6. /*reverse*/
  7. String s2=new StringBuffer(s1).reverse().toString();
  8. char []s3=s2.toCharArray();
  9. int i,j;
  10. for(i=0;i<=7;i++)//8 characters 8th is null
  11. {
  12. for(j=0;j<=i;j++)
  13. {
  14. System.out.print(s3[j]);
  15. }
  16. System.out.println();
  17. }
  18. }
  19. }
output
r
er
ter
uter
puter
mputer
omputer
computer
no clue about spaces; tabs,string =" ",'\0' all tried but for loop use gives me a weird output
Last edited by Tekmaven; Jul 1st, 2009 at 6:28 pm. Reason: Code Tags
Reputation Points: 11
Solved Threads: 4
Junior Poster
akulkarni is offline Offline
111 posts
since Jun 2009
Jun 30th, 2009
0

Re: print a pascal type char pattern

Click to Expand / Collapse  Quote originally posted by akulkarni ...
class gratefultodani
{
public static void main(String args[])
{
String s1="computer";
/*reverse*/
String s2=new StringBuffer(s1).reverse().toString();
char []s3=s2.toCharArray();
int i,j;
for(i=0;i<=7;i++)//8 characters 8th is null
{
for(j=0;j<=i;j++)
{
System.out.print(s3[j]);
}
System.out.println();
}
}
}
output
r
er
ter
uter
puter
mputer
omputer
computer
no clue about spaces; tabs,string =" ",'\0' all tried but for loop use gives me a weird output

Code tags!


[code]
// paste code or pattern here
[/code]




JAVA Syntax (Toggle Plain Text)
  1. class gratefultodani
  2. {
  3. public static void main(String args[])
  4. {
  5. String s1="computer";
  6. /*reverse*/
  7. String s2=new StringBuffer(s1).reverse().toString();
  8. char []s3=s2.toCharArray();
  9. int i,j;
  10. for(i=0;i<=7;i++)//8 characters 8th is null
  11. {
  12. for(j=0;j<=i;j++)
  13. {
  14. System.out.print(s3[j]);
  15. }
  16. System.out.println();
  17. }
  18. }
  19. }

Line 10 - Null terminator in a String is a C concept. Don't worry about it in Java.

I don't understand what your are "reversing" and why. You don't need to reverse anything. You could, of course, since there's more than one way of doing this, but you don't have to.

You should post the desired output, the actual output, and the code itself. Post ALL of this in code tags.

In your loop, there is no attempt to display a space. that's why you don't have any spaces. You don't need a character array. You have your string and that's good enough:

Java Syntax (Toggle Plain Text)
  1. String theWord = "computer";
  2. int numLetters = theWord.length ();
  3.  
  4. for (int numSpaces = numLetters - 1; numSpaces >= 0; numSpaces--)
  5. {
  6. for (int i = numLetters - 1; i >= 0; i--)
  7. {
  8. System.out.print (" ");
  9. }
  10. for (int i = numSpaces; i < numLetters; i++)
  11. {
  12. System.out.print (theWord.charAt (i));
  13. }
  14. System.out.println ();
  15. }
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,375 posts
since Jan 2008
Jun 30th, 2009
0

Re: print a pascal type char pattern space reduce by 1 after every line

[QUOTE=VernonDozier;904502]Code tags!


[code]
your code gives me space thats fine but it should reduce by 1 after every line
your program gives

r
er
ter
uter
puter



desired output
r
er
ter
uter
puter







/code]




[code=JAVA]
Reputation Points: 11
Solved Threads: 4
Junior Poster
akulkarni is offline Offline
111 posts
since Jun 2009
Jul 1st, 2009
0

Re: print a pascal type char pattern space reduce by 1 after every line

Java Syntax (Toggle Plain Text)
  1.  
  2.  
  3. i am not able to express my desired output
  4. i dont see it in this window the way i want
  5. the space should reduce by 1 after every line and then print the characters
Last edited by akulkarni; Jul 1st, 2009 at 12:01 am.
Reputation Points: 11
Solved Threads: 4
Junior Poster
akulkarni is offline Offline
111 posts
since Jun 2009
Jul 1st, 2009
0

Re: print a pascal type char pattern space reduce by 1 after every line

DON'T use my noparse tags. Those were there only to show you how to use code tags. Do exactly what is on the screen and nothing else.


[code]
// paste pattern here
[/code]


Is this what you were trying to post?

Java Syntax (Toggle Plain Text)
  1. your code gives me space thats fine but it should reduce by 1 after every line
  2. your program gives
  3.  
  4. r
  5. er
  6. ter
  7. uter
  8. puter
  9.  
  10.  
  11.  
  12. desired output
  13. r
  14. er
  15. ter
  16. uter
  17. puter


Use the "Preview Post" button instead of "Submit Reply" and change as needed till it's right. Then submit your post.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,375 posts
since Jan 2008
Jul 1st, 2009
0

Re: print a pascal type char pattern

Code tags!


[code]
// paste code or pattern here
[/code]




JAVA Syntax (Toggle Plain Text)
  1. class gratefultodani
  2. {
  3. public static void main(String args[])
  4. {
  5. String s1="computer";
  6. /*reverse*/
  7. String s2=new StringBuffer(s1).reverse().toString();
  8. char []s3=s2.toCharArray();
  9. int i,j;
  10. for(i=0;i<=7;i++)//8 characters 8th is null
  11. {
  12. for(j=0;j<=i;j++)
  13. {
  14. System.out.print(s3[j]);
  15. }
  16. System.out.println();
  17. }
  18. }
  19. }

Line 10 - Null terminator in a String is a C concept. Don't worry about it in Java.

I don't understand what your are "reversing" and why. You don't need to reverse anything. You could, of course, since there's more than one way of doing this, but you don't have to.

You should post the desired output, the actual output, and the code itself. Post ALL of this in code tags.

In your loop, there is no attempt to display a space. that's why you don't have any spaces. You don't need a character array. You have your string and that's good enough:

Java Syntax (Toggle Plain Text)
  1. String theWord = "computer";
  2. int numLetters = theWord.length ();
  3.  
  4. for (int numSpaces = numLetters - 1; numSpaces >= 0; numSpaces--)
  5. {
  6. for (int i = numLetters - 1; i >= 0; i--)
  7. {
  8. System.out.print (" ");
  9. }
  10. for (int i = numSpaces; i < numLetters; i++)
  11. {
  12. System.out.print (theWord.charAt (i));
  13. }
  14. System.out.println ();
  15. }

My bad, I think. I'm not sure whether I made a typo before or got confused or whether I 100% understood what the goal of the program was, but if THIS is the goal:

Java Syntax (Toggle Plain Text)
  1. r
  2. er
  3. ter
  4. uter
  5. puter
  6. mputer
  7. omputer
  8. computer

then line 6 needs to be changed to this:

JAVA Syntax (Toggle Plain Text)
  1. String theWord = "computer";
  2. int numLetters = theWord.length ();
  3.  
  4. for (int numSpaces = numLetters - 1; numSpaces >= 0; numSpaces--)
  5. {
  6. for (int i = numLetters - 1; i >= 0; i--)
  7. {
  8. System.out.print (" ");
  9. }
  10. for (int i = numSpaces; i < numLetters; i++)
  11. {
  12. System.out.print (theWord.charAt (i));
  13. }
  14. System.out.println ();
  15. }

Java Syntax (Toggle Plain Text)
  1. for (int i = numSpaces - 1; i >= 0; i--)
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,375 posts
since Jan 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: interfaces in java
Next Thread in Java Forum Timeline: Big errors i got plz help me out illegal type of expression type,





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC