简体   繁体   English

如何调用一个字符串出现在同一个包的另一个类中

[英]How to call a string to appear in another class in same package

I have this class called Main.java.我有一个叫做 Main.java 的类。 In this the output is shown by inputLine which is a string taken serially from the com port.在此输出由 inputLine 显示,它是从 com 端口串行获取的字符串。 I want this string value to be used in some other class which is in the same default package.我希望在同一个默认包中的其他一些类中使用这个字符串值。 How do I call this string in other class?我如何在其他类中调用这个字符串? I have made a static and declared it outside the method.我做了一个静态的并在方法之外声明它。 Now do I call in other class.现在我打电话给其他班级。 When I try calling it in other class by Main.inputLine=1 then it says incomparable types.当我尝试通过 Main.inputLine=1 在其他类中调用它时,它表示无法比较的类型。

Also I'm attaching a snapshot of the error: http://oi45.tinypic.com/2gvo5u9.jpg我还附上了错误的快照: http : //oi45.tinypic.com/2gvo5u9.jpg

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;
import gnu.io.SerialPortEvent;
import gnu.io.SerialPortEventListener;
import java.lang.String;
import java.util.Enumeration;


public class Main implements SerialPortEventListener
{
    SerialPort serialPort;
        /** The port we're normally going to use. */
    private static final String PORT_NAMES[] = {
            "COM30", // Windows
    };
    /**
    * A BufferedReader which will be fed by a InputStreamReader
    * converting the bytes into characters
    * making the displayed results code page independent
    */
    public BufferedReader input;
    /** The output stream to the port */
    public OutputStream output;
    /** Milliseconds to block while waiting for port open */
    private static final int TIME_OUT = 2000;
    /** Default bits per second for COM port. */
    private static final int DATA_RATE = 9600;

    public void initialize() {
        CommPortIdentifier portId = null;
        Enumeration portEnum = CommPortIdentifier.getPortIdentifiers();

        //First, Find an instance of serial port as set in PORT_NAMES.
        while (portEnum.hasMoreElements()) {
            CommPortIdentifier currPortId = (CommPortIdentifier) portEnum.nextElement();
            for (String portName : PORT_NAMES) {
                if (currPortId.getName().equals(portName)) {
                    portId = currPortId;
                    break;
                }
            }
        }
        if (portId == null) {
            System.out.println("Could not find COM port.");
            return;
        }

        try {
            // open serial port, and use class name for the appName.
            serialPort = (SerialPort) portId.open(this.getClass().getName(),
                    TIME_OUT);

            // set port parameters
            serialPort.setSerialPortParams(DATA_RATE,
                    SerialPort.DATABITS_8,
                    SerialPort.STOPBITS_1,
                    SerialPort.PARITY_NONE);

            // open the streams
            input = new BufferedReader(new InputStreamReader(serialPort.getInputStream()));
            output = serialPort.getOutputStream();

            // add event listeners
            serialPort.addEventListener(this);
            serialPort.notifyOnDataAvailable(true);
        } catch (Exception e) {
            System.err.println(e.toString());
        }
    }

    /**
     * This should be called when you stop using the port.
     * This will prevent port locking on platforms like Linux.
     */
    public synchronized void close() {
        if (serialPort != null) {
            serialPort.removeEventListener();
            serialPort.close();
        }
    }

    /**
     * Handle an event on the serial port. Read the data and print it.
     */
        public static String inputLine;
    public synchronized void serialEvent(SerialPortEvent oEvent) {
        if (oEvent.getEventType() == SerialPortEvent.DATA_AVAILABLE) {
            try {
                inputLine=input.readLine();
                                System.out.println(inputLine);

            } catch (Exception e) {
                System.err.println(e.toString());
            }
        }
        // Ignore all the other eventTypes, but you should consider the other ones.
    }

    public static void main(String[] args) throws Exception {
        Main main = new Main();
        main.initialize();
        Thread t=new Thread() {
            public void run() {
                //the following line will keep this app alive for 1000 seconds,
                //waiting for events to occur and responding to them (printing incoming messages to console).
                try {Thread.sleep(1000000);} catch (InterruptedException ie) {}
            }
        };
        t.start();
        System.out.println("Started");
    }
}

You're trying to compare two things and one of them is an Object (String).您正在尝试比较两件事,其中之一是对象(字符串)。 Use equals(Object o) .使用equals(Object o)

(Besides, comparison between primitives is done using == . (此外,基元之间的比较是使用==完成的。

You're trying to compare to an integer value.您正在尝试与整数值进行比较。 You need to wrap the integer value to a String for this to make much sense.您需要将整数值包装到一个字符串中以使其更有意义。

You want:你要:

if(Main.inputLine.equals("1"))
{
    ...
}

If you are trying to assign 1 to Main.inputLine then use the following :如果您尝试将1分配给Main.inputLine则使用以下内容:

Main.inputLine="1" 

instead of代替

Main.inputLine=1

And if you are comparing the String to check for 1 then use the following:如果您正在比较字符串以检查 1,则使用以下内容:

Main.inputLine.equals("1");

instead of代替

Main.inputLine == "1"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何调用一个字符串出现在另一个类中 - How to call a string to appear in another class 如何调用同一个包的另一个类中的方法? - How to call a method in another class of the same package? 如何从同一包中的另一个类调用一个类,显示如何在Java中修复该类错误? - How to call a class from another in th same package shows an error how to fix it in java? 如何将变量传递到同一包中的另一个类中 - how to pass a variable into another class in same package 我想从另一个类调用一个方法,但在同一个包或文件中。如何做到这一点? - i want to call a method from another class but in same package or file.how to do this? 为什么不能在同一个包中调用另一个类的包私有方法呢? - Why can't I call a package-private method of another class that's a subclass of a class in the same package? 如何在Java中的相同程序包和相同目录中调用类? - How to Call a class within same package and same directory in java? 如何在同一个类中调用一个方法到另一个方法 - How to call a Method to another Method in the same class 如果数据在同一包中,如何将数据从一个类传递到另一个类 - How to pass data from a class to another class if they are in the same package Java(eclipse):如何从同一个项目中的另一个类调用int或string? - Java (eclipse): How to call a int or string from another class in same project?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM