Variables cannot be resolved

前端 未结 3 1775
难免孤独
难免孤独 2021-01-21 15:07

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 previou

相关标签:
3条回答
  • 2021-01-21 15:24

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

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

    This means you can only use them inside that block.

    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;
        }
    }
    
    0 讨论(0)
  • 2021-01-21 15:30

    In this line

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

    The variable data , input are out of scope, declare them as instance variables and use it

    like

    public class Application {
         private static String input;
         private static String data;
    

    Please prefer to read :Java language specification on blocks

    0 讨论(0)
  • 2021-01-21 15:37

    The variables data and input which you've used in the String emailBody is no where declared and intilized.

    also

     if(allowEmails) {
     }
    

    is enough rather than

    if(allowEmails == true)
    
    0 讨论(0)
提交回复
热议问题