954,549 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Java Console Output

hello,

I am in the process of working on a design project for my fourth year of uni and am running into some trouble. At the moment, we have a serial communication coming from an Arduino and displays in the console box of Eclipse. Each lines displays. However while this may seem like a simple solution, I would like to know how I can read each line of the console(there is no input or anything I've tried StringTokenizer and BufferReaders but always get stuck) if they range between the letter A-Z then store in an array..

Hope someone can help.. Thank you

pink_sapphire
Newbie Poster
24 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
 
how I can read each line of the console


The Scanner class has easy to use methods for reading from the console.if they range between the letter A-Z then store in an array..
Are you talking about single characters: A or b or K
or about Strings of characters: ADBEDE
For single characters you could use an if condition
For Strings a regular expression could test if the String contains only those letters.

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 

So basically my console would read:
RIGHT Blink
RIGHT Ready
LEFT Blink
LEFT Ready
...
A
..
LEFT Blink
LEFT Ready
...
B
...
G

So I simply want to read the lines that have a letter.. These are the lines of code that output:

int available = input.available();
byte chunk[] = new byte[available];
input.read(chunk, 0, available);

String blink = new String(chunk);
System.out.print(blink);

//scan the output
//if detects letter[A-Z]
      //then store in arraylist
        // once detects string "SPEAK"
        //store content of array list to text file and clear list
        //repeat while loop...
pink_sapphire
Newbie Poster
24 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
 

Do you have further questions about the problem?

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 

Yes I am still unsure on how to proceed with using String blink and using a Scanner to find the letters in the console...

pink_sapphire
Newbie Poster
24 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
 

What do you mean by "find the letters in the console."
Are you trying to get characters that have been written to a console?
What program is writing to the console?

I thought you meant you wanted to read data in from the console as a user entered it.

how to proceed with using String blink


For debugging: Print the String: blink to see what it contains.
When you know its contents you should be able to write code to extract the parts that you want.

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 

"Are you trying to get characters that have been written to a console?"

Precisely, this is what I am trying to do but have been unable to..

(also I am an elec eng so programming is not my forte..i apologize for asking such basic questions.. I know how to scan the console for user input but not for when data has already been outputted to the console...)

pink_sapphire
Newbie Poster
24 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
 
String blink = new String(chunk);
System.out.print(blink


);

Doesn't that mean that you have every line of console output in the String blinbk before it's written to the console?

JamesCherrill
Posting Genius
Moderator
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

unfortunately yes, but is there a way of extracting the Strings [A-Z] from String blink before doing the System.out.print(blink).... Ive been testing several ideas but all have fall thru;..

pink_sapphire
Newbie Poster
24 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
 
is there a way of extracting the Strings [A-Z] from String blink before doing the System.out.print(blink)


Can you give an example of the contents of the String blink (a small one will do)
and show what you want to extract from it?

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 
unfortunately yes, but is there a way of extracting the Strings [A-Z] from String blink before doing the System.out.print(blink).... Ive been testing several ideas but all have fall thru;..

Not unfortunate at all! That means you can take a copy of blink just before writing it to the console and you can do all the analysis and processing you want on it.
Now is a good time to look at the API doc for the String class where you will find loads of methods, some of which are just what you need - in particular you can check if the String is exactly one character long.

JamesCherrill
Posting Genius
Moderator
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

So when you do system.out.println(blink);
output in console is:

RIGHT Blink
RIGHT Ready
LEFT Blink
LEFT Ready
RIGHT Blink
RIGHT Ready
LEFT Blink
LEFT Ready
A
LEFT Blink
LEFT Ready
RIGHT Blink
RIGHT Ready
LEFT Blink
LEFT Ready
RIGHT Blink
RIGHT Ready
B
...
G

Thus I would like to extract A B G and store into an ArrayList

pink_sapphire
Newbie Poster
24 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
 

Yes, you said that already. Please re-read my previous post - it gives you info to start solving this yourself.

JamesCherrill
Posting Genius
Moderator
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 
Can you give an example of the contents of the String blink (a small one will do) and show what you want to extract from it?

Here is an output for System.out.println(blink);

RIGHT Blink
RIGHT Ready
LEFT Blink
LEFT Ready
RIGHT Blink
RIGHT Ready
RIGHT Blink
RIGHT Ready

A
RIGHT Blink
RIGHT Ready
RIGHT Blink
RIGHT Ready
LEFT Blink
LEFT Ready
LEFT Blink
LEFT Ready
B
...
G

Thus I would like to extract the Strings A B G ... and store into an Array List

pink_sapphire
Newbie Poster
24 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
 

How do you find the Strings that you want to extract?
Are they the lines with a single character?

The Scanner class has a constructor that you can pass a String to.
Then use the next method to "read" the tokens one by one looking for those with a length of one.

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 

How do you find the Strings that you want to extract? Are they the lines with a single character?

The Scanner class has a constructor that you can pass a String to. Then use the next method to "read" the tokens one by one looking for those with a length of one.

The Strings I want to extract range between the letters A to Z so yes they are the lines with single characters...

I've tried the belong code but nothing appears

public synchronized void serialEvent(SerialPortEvent oEvent) {

		try{
                       //if Ardunio is on and serial com is receiving data, process chunk
			if (oEvent.getEventType() == SerialPortEvent.DATA_AVAILABLE) {

				try {

					int available = input.available();
					byte chunk[] = new byte[available];
					input.read(chunk, 0, available);

					String blink = new String(chunk);
					//System.out.print(blink);
					ArrayList<String> list = new ArrayList<String>();

					Scanner scanner = new Scanner(blink).useDelimiter("RIGHT Blink");
						
					try {
						
						//first use a Scanner to get each line
						while (scanner.hasNextLine() ){
							
							if (scanner.hasNext()  ){
								String f_letter = scanner.next();
								//if ( f_letter.length()<1)
								System.out.println("Letters Spoken are " + f_letter.trim());
							}
							else {
								System.out.println(" invalid line. Unable to process.");
							}
							
							
						}
					}
					finally {

						scanner.close();
					}
                   } catch (Exception e) {
					System.err.println(e.toString());
				}

			}
		}catch(Exception e) {

			System.out.println("Error during reading/writing");
		}

	}


The output resembles the following which is totally not what I want...If I don't use the delimiter it is unable to process..I simply want it to extract the single characters of String ranging from A to Z and ignore all the RIGHT & LEFT stuff...

Letters Spoken are RI
Letters Spoken are GHT blink
Letters Spoken are RIGH
Letters Spoken are T ready
Letters Spoken are LEFT
Letters Spoken are ready
Letters Spoken are LE
Letters Spoken are FT blink
S
Letters Spoken are R
Letters Spoken are IGHT blink
Letters Spoken are R
Letters Spoken are IGHT ready
Letters Spoken are LE
Letters Spoken are FT ready

pink_sapphire
Newbie Poster
24 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
 

Where does your code test the length of the String looking for a length of 1?

What does the printed output that you posted show?

For testing, use a small input with only a few lines in it so you can easily see what is happening.

Print out the value of blink before trying to scan/parse it to see what the input looks like.

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 

Where does your code test the length of the String looking for a length of 1?

What does the printed output that you posted show?

Here is how I test for length after changing the following lines:

Scanner scanner = new Scanner(blink); //useDelimiter("\\RIGHT blink");

try {
						
       //first use a Scanner to get each line + check string length
	while (scanner.hasNextLine() && blink.length()==1 ){
						
	if (scanner.hasNext() ){
								
	String f_letter = scanner.next();
								
	System.out.println("Letters Spoken are " + f_letter.trim());
	}
	else {
	System.out.println("Empty or invalid line. Unable to process.");
	}

The output in the console is now:

Letters Spoken are R
Letters Spoken are L
Letters Spoken are L
Letters Spoken are L
Letters Spoken are R
Letters Spoken are R
Letters Spoken are L
Letters Spoken are R
Letters Spoken are R
etc

pink_sapphire
Newbie Poster
24 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
 

For testing, use a small input with only a few lines in it so you can easily see what is happening.

Print out the value of blink before trying to scan/parse it to see what the input looks like.

What was the input for what you just posted? It looks like the first letter of each line, not the contents of a line with only one character on it.

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 

For testing, use a small input with only a few lines in it so you can easily see what is happening.

Print out the value of blink before trying to scan/parse it to see what the input looks like.

What was the input for what you just posted? It looks like the first letter of each line, not the contents of a line with only one character on it.

I already know the what the input coming from the Arduino Uno is as seen a few posts above and this outputs to the console of Eclipse when the Java programs detects serial communication. I will repost here

RIGHT blink
RIGHT ready
LEFT blink
LEFT ready
RIGHT blink
RIGHT ready
RIGHT blink
RIGHT ready
A
RIGHT blink
RIGHT ready
LEFT blink
LEFT ready
LEFT blink
LEFT ready
LEFT blink
LEFT ready
B
etc this keeps going until person stops blinking..

This is what I had initially, should I simply create a new method scanning the console and when the so called input is added and then extract the 'A' 'B' and save to file instead?? This is ultimately what I want to do... But I am still unsure how to do this as input is coming from another program...

pink_sapphire
Newbie Poster
24 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You