• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • paul wheaton
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • Mikalai Zaikin
  • Himai Minh

How do i print this pattern symbol properly?

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello i was doing my activities on programming but after running my output of the code it gives me this pattern instead, where did i go wrong?


Here is the code:
 
Master Rancher
Posts: 4283
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you describe in English what the difference is between the current output and the desired output?
The description will help in deciding what changes need to be made to the program.
The code's formatting needs some fixing.  The extra {} should be removed and the for statement's ending } should align with the start of the for statement and not be indented.
 
Marshal
Posts: 72619
317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

Yes, these “print a pattern” programs are annoying to write, but they are good or making you think about what you are doing and about your counting. To print the pattern you want, you simply have to count how many spaces you need to the left of the caret (=^). Unfortunately you are making things unnecessarily difficult for yourself.
Please always regard the following format as the basic format for a for loop:-You will sometimes have to change that, but try to avoid <= and − 1 etc., because they all make the code more difficult to read. You have lots of <= and >= and − 1s, and you are mixing different indentation conventions. all those things make your code harder to read, and, I am afraid, you yourself are the person who will have the most difficulty with illegible code.
Don't write System.out.println(""); Write System.out.println(); instead.
Not something you would need to worry about for this sort of program, but I think multiple print instructions will give you slower execution. Print much, but rarely, rather than little and often. Use a StringBuilder object (use it as a local variable) and append your characters:-
 
Bartender
Posts: 2854
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I guess this program can be done using a single for loop.
 
salvin francis
Bartender
Posts: 2854
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's a table that visualizes your problem better. I have used "#" to symbolize space and "x" to symbolize the "^" symbol.
It looks like there's a 1:1 correspondence between the number of spaces vs the current iteration number (given that you count from 0)
There are two ways you can achieve this output
  • In each iteration, create (or print) a string of spaces equal to the current iteration count followed by a "^" symbol
  • Use the same String and just prepend spaces to it in every iteration
  • Hope this helps !
     
    Consider Paul's rocket mass heater.
    reply
      Bookmark Topic Watch Topic
    • New Topic