• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Need help with code to find number of lines printed

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How to i find the total numbers of hello printed?

e.g.

System.out.println("Hello");
System.out.println("Hello2")
System.out.println("Hello3")
System.out.println("Hello4")

System.out.println("Total of number of hellos are " + count);
 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not sure what the application is, but you could initialize count to zero at the top and increment it each time you print.
 
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not that simple. What if you wanted to call System.out.println("a\nb\nc")?
Would you add one or three to count?
 
Dante Hunter
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Application is netbeans
 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Knute was referring to your application and not the IDE you are using to write it, ie what is the intended purpose of your application.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Knute Snortum
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch, Alberto.

Something to keep in mind in the future: we don't like to provide complete solutions. The point is to help the OP figure things out on their own. Small snippets are fine to illustrate a point.
 
Ranch Hand
Posts: 789
Python C++ Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dante Hunter wrote:How to i find the total numbers of hello printed?

e.g.

System.out.println("Hello");
System.out.println("Hello2")
System.out.println("Hello3")
System.out.println("Hello4")

System.out.println("Total of number of hellos are " + count);



You would have to have a variable cnt (good abbreviation for count) that coincides with each hello printed.

Like -
int cnt = 0; //cnt is 0
System.out.println("Hello");
cnt++; //increase cnt by 1
System.out.println("Hello2")
cnt++; //increase cnt by 1
System.out.println("Hello3")
cnt++; //increase cnt by 1
System.out.println("Hello4")
cnt++; //cnt is 4 after this line

System.out.println(cnt); // prints 4

At the end, cnt could tell you how many hellos were printed. But usually it would be in a loop like the example Alberto gave -- and that is more flexible too. I'm showing you the basic thing that has to happen.
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Guillermo Ishi wrote: . . .
You would have to have a variable cnt (good abbreviation for count) that coincides with each hello printed.
. . .

That is a bad name; the abbreviation might mean something different, and you might have all sorts of things you are counting. Try linesPrinted instead.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic