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

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.

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...

Do you have further questions about the problem?

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

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.

"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...)

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?

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;..

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?

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.

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

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

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

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.

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

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.

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

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.

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...

How does the program you are writing get the data that is to be scanned?
Does it come in small pieces that you need to put together to get a line. A line being a stream of characters followed by a line end character.
If you get the characters from the other program a line at a time, then you should be able to look at each line and see if it is a single character.

scanning the console

What do you mean by this? How does your program get to what is on the console?

How does the program you are writing get the data that is to be scanned?
Does it come in small pieces that you need to put together to get a line. A line being a stream of characters followed by a line end character.
If you get the characters from the other program a line at a time, then you should be able to look at each line and see if it is a single character.


What do you mean by this? How does your program get to what is on the console?

Using an RXTX library in Eclipse and then listening through a method SerialPortEventListener for a communication with an Ardunio uno board that is connected to my laptop via usb.. once it detects the connection, Eclipse starts receiving the data(blinks by user) that is directly outputted to the console... It comes line by line... so I only need the lines that are single letters/characters that need to ultimately be stored in a text file...

if line is a single character
store this character in an array of some sort
until "SPEAK" is detected on line of console
then store in text file.. (where the array of letters which will form a word will be synthesized to speech)
repeat..

the data(blinks by user) that is directly outputted to the console

That is the part I don't understand. If the other program writes directly to the console, how does the java program get it?

I thought that the data was being read into the java program which then prints it on the console.
I didn't understand your answer to my question:
How does the program you are writing get the data that is to be scanned?
Does it come in small pieces that you need to put together to get a line.
A line being a stream of characters followed by a line end character.

It comes line by line

If the data is received by the java program line by line, then the program can see if the line is one character long and do what you want done. There must be something else about this that you are not explaining.

That is the part I don't understand. If the other program writes directly to the console, how does the java program get it?

I thought that the data was being read into the java program which then prints it on the console.
I didn't understand your answer to my question:
How does the program you are writing get the data that is to be scanned?
Does it come in small pieces that you need to put together to get a line.
A line being a stream of characters followed by a line end character.

If the data is received by the java program line by line, then the program can see if the line is one character long and do what you want done. There must be something else about this that you are not explaining.

I apologize for not being clear, the ardunio program runs in the background detecting a user's left/right blinks by IR sensors, once a sequence of blinks is detected it is outputted to the console and depending what the sequence is, the next output will the LETTER associated with the BLINK.. etc

So yes, it comes in small pieces(line by line) and I need to take that last output of each blink sequence and put it into a text file where a batch file runs and using the built in mac function of 'say', the line of combined letter is spoken ..

I've been searching and trying different stuff out but I don't get the proper results...

it comes in small pieces(line by line)

There is a difference between small pieces and line by line
lines end with a line end character.
small pieces do not need to end with a line end character.
If you are reading characters from a device, you don't have a line until you read the line end character. The small pieces are parts of a line.

Perhaps the small bits need to be put together until you get the lineend character that means you have all of a line. Then test if the line is one character long.

There is a difference between small pieces and line by line
lines end with a line end character.
small pieces do not need to end with a line end character.
If you are reading characters from a device, you don't have a line until you read the line end character. The small pieces are parts of a line.

Perhaps the small bits need to be put together until you get the lineend character that means you have all of a line. Then test if the line is one character long.

Each piece of data is processed line by line so could you simply help me out with how to see if the line is of length of 1 and then store in an array please..it would be much appreciated..

Read the line into a String and test its length using the length() method.

For testing, print out every line that you read in to see what the data looks like:
System.out.println("line=" + line + "<");
Be sure to add some delimiters so you see where the line begins and ends.

Read the line into a String and test its length using the length() method.

For testing, print out every line that you read in to see what the data looks like:
System.out.println("line=" + line + "<");
Be sure to add some delimiters so you see where the line begins and ends.

so I've come up with:

try {

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

	String blink = new String(chunk);

          try {

		//Read the line into a String
		BufferedReader line = new BufferedReader(new StringReader(blink));
		String len = line.readLine();
		f(len.length () == 0)
		System.out.println("characters: " +len);

when len.length () == 0 -> nothing appears
when len.length () == 1 -> returns first letter of each line
when len.length () == 2 -> returns first two letter of each line

Does it do what you want now?

What is line 14 for? Does it compile?

Does it do what you want now?

What is line 14 for? Does it compile?

No it does not.. it outputs the first letter of each line instead of the lines with length of one

line 14 is an if statement checking for length of string blink.. is this the proper way of doing it?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.