• Post Reply Bookmark Topic Watch Topic
  • New Topic

Need help with code to find number of lines printed  RSS feed

 
Dante Hunter
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • 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);
 
Knute Snortum
Sheriff
Posts: 4274
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • 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.
 
Paweł Baczyński
Bartender
Posts: 2077
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Application is netbeans
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • 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.
 
Alberto Tejos
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Knute Snortum
Sheriff
Posts: 4274
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • 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.
 
Guillermo Ishi
Ranch Hand
Posts: 789
C++ Linux Python
  • Mark post as helpful
  • send pies
  • 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.
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!