| | |
print a pascal type char pattern
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Jun 2009
Posts: 99
Reputation:
Solved Threads: 4
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
******** 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.
•
•
Join Date: Jan 2008
Posts: 3,844
Reputation:
Solved Threads: 503
•
•
•
•
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)
r er ter uter puter mputer omputer computer
Use a nested for-loop:
Java Syntax (Toggle Plain Text)
String theWord = "computer"; int numLetters = theWord.length (); for (int numSpaces = numLetters - 1; numSpaces >= 0; numSpaces--) { // display numSpaces spaces using for-loop. // display last numLetters - numSpaces letters of theWord. // display newline. }
•
•
Join Date: Jan 2008
Posts: 3,844
Reputation:
Solved Threads: 503
•
•
•
•
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.
•
•
Join Date: Jun 2009
Posts: 99
Reputation:
Solved Threads: 4
java Syntax (Toggle Plain Text)
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(); } } }
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
•
•
Join Date: Jan 2008
Posts: 3,844
Reputation:
Solved Threads: 503
•
•
•
•
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)
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(); } } }
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)
String theWord = "computer"; int numLetters = theWord.length (); for (int numSpaces = numLetters - 1; numSpaces >= 0; numSpaces--) { for (int i = numLetters - 1; i >= 0; i--) { System.out.print (" "); } for (int i = numSpaces; i < numLetters; i++) { System.out.print (theWord.charAt (i)); } System.out.println (); }
•
•
Join Date: Jun 2009
Posts: 99
Reputation:
Solved Threads: 4
Java Syntax (Toggle Plain Text)
i am not able to express my desired output i dont see it in this window the way i want 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.
•
•
Join Date: Jan 2008
Posts: 3,844
Reputation:
Solved Threads: 503
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?
Use the "Preview Post" button instead of "Submit Reply" and change as needed till it's right. Then submit your post.
[code]
// paste pattern here
[/code]
Is this what you were trying to post?
Java Syntax (Toggle Plain Text)
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
Use the "Preview Post" button instead of "Submit Reply" and change as needed till it's right. Then submit your post.
•
•
Join Date: Jan 2008
Posts: 3,844
Reputation:
Solved Threads: 503
•
•
•
•
Code tags!
[code]
// paste code or pattern here
[/code]
JAVA Syntax (Toggle Plain Text)
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(); } } }
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)
String theWord = "computer"; int numLetters = theWord.length (); for (int numSpaces = numLetters - 1; numSpaces >= 0; numSpaces--) { for (int i = numLetters - 1; i >= 0; i--) { System.out.print (" "); } for (int i = numSpaces; i < numLetters; i++) { System.out.print (theWord.charAt (i)); } System.out.println (); }
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)
r er ter uter puter mputer omputer computer
then line 6 needs to be changed to this:
JAVA Syntax (Toggle Plain Text)
String theWord = "computer"; int numLetters = theWord.length (); for (int numSpaces = numLetters - 1; numSpaces >= 0; numSpaces--) { for (int i = numLetters - 1; i >= 0; i--) { System.out.print (" "); } for (int i = numSpaces; i < numLetters; i++) { System.out.print (theWord.charAt (i)); } System.out.println (); }
Java Syntax (Toggle Plain Text)
for (int i = numSpaces - 1; i >= 0; i--)
![]() |
Similar Threads
- Can't firgure out this compile error (C++)
- passing a string to parameter of type char* (C++)
- program.c:50: warning: subscript has type `char' (C)
- Need Help With Printing Pascal's Triangle In C (C)
- char ** s = new[], char s[i] = new [], for (i < size) delete [] s[i] = Segfault (C)
- help understanding "char" data type (C++)
- Printing using Web Control Print button VB.NET (JavaScript / DHTML / AJAX)
Other Threads in the Java Forum
- Previous Thread: interfaces in java
- Next Thread: Big errors i got plz help me out illegal type of expression type,
Views: 591 | Replies: 14
| Thread Tools | Search this Thread |
Tag cloud for Java
account android api apple applet application arguments array arrays automation binary bluetooth chat class classes client code columns component data database draw eclipse error event exception expand file filechooser fractal game givemetehcodez google graphics gui helpwithhomework homework html ide image inheritance input integer j2me java javaprojects jlabel jme jmf jni jpanel jtextfield julia linux list loop map method methods midlethttpconnection mobile monitoring netbeans newbie nullpointerexception number object open-source oracle print problem program programming project property recursion ria scanner screen search server set size sms socket sort sourcelabs splash sql sqlite static string swing test testautomation threads time transfer tree windows






