KamelK
KamelK

Reputation: 71

Issue with the Scanner input

I am writing a code to get data from a radiometer sensor connected to one of the "COMs" of the computer, to get the measurement i have to communicate with that sensor throw "COM7" and write the command "gi" to get a value like ""9.919e-08" for example from the command user interface.

Now i have a problem with the code and it is giving me that error "No line found"

enter image description here

and here is the code

package reading_data;

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Scanner;

import com.fazecast.jSerialComm.SerialPort;

public class main {

public static void main(String[] args) throws IOException, InterruptedException {
        
    SerialPort sp= SerialPort.getCommPort("COM7");
    sp.setComPortParameters(115200, 8, 1, 0);
    sp.setComPortTimeouts(SerialPort.TIMEOUT_WRITE_BLOCKING, 0, 0);

    sp.openPort();
    
    if(sp.isOpen()) {
    System.out.println("Port is open");
        
    PrintWriter output=new PrintWriter(sp.getOutputStream());
    Scanner data=new Scanner(sp.getInputStream());
    output.println("gi");
    String ssss=data.nextLine();
    System.out.println("--++++---->"+ssss);
    
    }else {
        System.out.println("Port is not open");
    }   
    
    sp.closePort();

}
}

and here is the error i get

Port is open
Exception in thread "main" java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Unknown Source)
at reading_data.main.main(main.java:26)

May you please tell me where is my mistake?

thanks in advance

Upvotes: 0

Views: 101

Answers (1)

Qbi
Qbi

Reputation: 21

Try like this:

output.flush();    
if(data.hasNextLine()) {
  String ssss=data.nextLine();
}

If no next line, then don't call the nextLine. Edit.: flush needs to be done.

Upvotes: 2

Related Questions