This is not the basic Scanner(System.in).
I have some recursive methods that I saw online that does exactly what I want but now I need to count the number of lines that have been "outputed" to the console.
So, I run the program. It does some computations and then it writes on the console some lines (as you can see above) and I need to , in the end on the computations (after the execution of the main), count the number of that lines and write it to somewhere.
Example of the output:
Does anyone know how it can be done?
Thanks.
You could use the class TeeOutputStream from Apache Common IO that split the outputs into two streams. This way you can still publish your stream in the default output and log them in a file. With the outputs in the file you can do whatever you wanted (count lines...).
Why don´t you declare a variable counter and initialize it with 0. After every System.out.println() statement, you increment the counter by adding counter++.
As final Statement in your main-Method, you print the variable to the console:
System.out.println("Counter: "+counter");
If that doesn´t work for you, you could still write the output of the program to a file instead of the console, read that file afterwards and look for line breaks.
Related
A few days back I wrote a Textinterpreter plugin in Eclipse which basically takes a text file and simply printout it's content in the console. It does this by first taking a text file and converts it to a string.
then it makes an Arraylist out of it from which each line is printed out in the console.
List<String> mLines = new LinkedList<String>(Arrays.asList(string)
while(!mLines.isEmpty())) {
String line = mLines.remove(0);
if(line.equals("Stop...")){
debug(DebugAction.Suspend);
}
System.out.println(">>> " + line + " <<<");
}
You can see an if statement in code above which checks whether "Stop..." is written on any line in the text file and if it is then the debug() funtion is called(which suspends running unless the user press resume() button in debugmode.)
Now I want to do the same for .java files. i.e write a Java interpreter plugin which execute a java file normally until it finds "Stop..." written in code.
Any Suggestions?
I don't think you really want to implement a Java interpreter, that'd be a huge project keeping you busy for some years maybe.
The most natural solution for you task might be to scan the Java source file and automatically create breakpoints at each Stop statement. Then run the application in debug mode and you get the desired behaviour. Since you only need the line number for creating the breakpoint you can actually keep reading/scanning files line-by-line.
To get additional statements executed (like calling debug(..)) add your snippet as a breakpoint condition (followed by return true; to tell the debug to stop indeed).
I get that this isn't possible to do with normal java, although if there are any libraries out this it would be very useful.
Essentially, I'm designing a console app and running into an issue that when output happens while something is typed in the input line, that input text will move up and appear before the line that just got output. Is it possible to fix this in some form so that the text you are inputting that stays at the bottom?
EX:
I'm typing something as input into my commandline app, and then the program prints something WHILE I'm typing - this causes what was originally on the input line to be scrolled up with whatever the output text was. When you are trying to type something in this can obviously be detrimental. I know it's possible to prevent this.. (Other programs have done it... EX: Minecraft Server)
(If I need to be more descriptive I can.)
You could use the help of threads. One that listens to user input, the other process the actual output. This problem is similar to basic race condition problems when multiple threads attempt to read and write to a shared resource.
Your shared resource is that console. You need to keep the Input/Output operations synchronized. Have a look at race condition.
I have a testing program (that I didn't write) that is giving a very weird error, which only happens some of the time. At the end of the program a string that contains the test report is output using
logger.warn(reportString);
Anywhere from 1/10 to 1/3 tries (it varies) this string isn't being displayed, but everything before it is.
Before this I added this line to ensure that the string is always created properly, which it is since this gives the same length every time.
logger.info("Length: " + reportString.length());
To experiment I added another logging line after, so the end of the program now looks like this:
logger.info("Length: " + reportString.length());
logger.warn(reportString);
logger.info("REACHED END OF PROGRAM");
When doing this the reportString always seems to be printed/logged, but the "REACHED END OF PROGRAM" string is only printed/logged about 50% of the time. Maybe it's something with my windows command prompt? I know this is a pretty vague problem but if anyone has any ideas they would be greatly appreciated.
Maybe you need to flush the all buffered logs to send all to the output. Add the next:
logger.warn(reportString);
LogManager.shutdown();
Just in case you have it in your code, you can try removing the sentence:
System.exit(0);
This probably has to do with how log4j buffers the data it outputs, try explicitly adding \n in the last string the logger prints, and see if it prints consistently now.
I have a package with a GUI, and in this GUI I need to read a text file (call it list.txt) that may look like
8:00am something
9:00am somethingelse
1:00pm something different
With each time/event on a separate line.
How do I read each line separately and extract the time portion from each line? What I need to accomplish is to compare times in a list like this to a range of times I have previously determined, and reprint the list with only events/times in that range, but I'm not sure on how to read it line by line.
How does the question not make sense? All I am asking is how to take a textFile (one written in a separate java package) and how to basically read it one line at a time and take the time portion from each line. The time is always at the start of each line. I'm sorry, but I don't know how to be more clear on that, that is about all there is to it.
And The user provides the text file name via GUI application, as well as a time range. But that part I have down.
Use java.io.BufferedReader's readLine() method to read file line-by-line.
Use java.lang.String's indexOf() method to locate the first space.
Use java.lang.String's substring() method to extract before and after the space.
I have a program that can run either by a Swing Application or a Command-Line
My question is this: How can I avoid the following code:
String message = "performing task #1; data abcd";
System.out.println(message);
jTextArea.append(message);
stringBuffer.append(message);
I need those 3 lines for the following reasons:
line 1 - so I can see the console output (I use eclipse)
line 2 - so I can see the output on the swing application
line 3 - at the end, I write all the log into a file.txt
is this something that can be achieved in ONE line? (not sure but maybe a log4j?!?!, if so how?)
Thanks!
Well, just making an assumption here: the place in which you want 1 line and not 3 is where you have the line you want to write to (up to) 3 places. So why not write your own method; you can make it static and even have it test which kind of logging to do based on application properties or whatever, then your code with the message can invoke it as:
Logger.logMessage(message)
And IT decides how many places to write it to.