11 Replies Latest reply: Mar 7, 2009 4:40 PM by 800313 RSS

    call String from another method

    843785
      hey,

      Im pretty new to java and Im having problems with calling a String from another method
      I have two public voids, in one of them I have a String which I wanna use in another method.

      Thanks
        • 1. Re: call String from another method
          800313
          public void method1(){
          //void means method does not return anything
              String aString = getString();
          }
          
          public String getString(){
          //string means this method returns a string
               String myString = "String";
               return myString;
          }
          Calypso
          • 2. Re: call String from another method
            843785
            Thanks for your response but I have two public voids
            • 3. Re: call String from another method
              800313
              A) change void to String then
              B)another option may be to declare your string as a global variable, that way any method in the class can access it

              ie
              String myString = "string";
              
              public void methodA(){
                //can use myString here
              }
              
              public void methodB(){
                //can also use myString here
              }
              if your still having problems post your code
              • 4. Re: call String from another method
                843785
                here is some of my code:
                public void getOrder() {
                        try {
                
                            DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
                            DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
                            Document doc = docBuilder.parse(new File("c:/orders.xml"));
                
                            // normalize text representation
                            doc.getDocumentElement().normalize();
                            System.out.println("Root element of the doc is " + doc.getDocumentElement().getNodeName());
                
                            // lees orders
                            NodeList orderlijst = doc.getElementsByTagName("order_naam");
                            System.out.println("totale aantal orders : " + orderlijst.getLength());
                         //    Element orderitem = (Element) orderlijst.item(0);
                System.out.println("name=" + doc.getElementsByTagName("order_naam").item(0).getFirstChild().getNodeValue());
                
                          String st =doc.getElementsByTagName("order_naam").item(0).getFirstChild().getNodeValue();
                          
                  public void createGUI () {
                        this.removeAll();
                  
                
                        
                        lbltest = new JLabel();
                        lbltest.setBounds(20, 40, 150, 20);
                        lbltest.setFont(new Font("Verdana", Font.BOLD, 12));
                        this.add(lbltest);
                
                    
                i want to use the String st in the jLabel in createGUI
                • 5. Re: call String from another method
                  800313
                  Option A
                  public String getOrder() {
                            String st =doc.getElementsByTagName("order_naam").item(0).getFirstChild().getNodeValue();
                            return st;
                  }
                   public void createGUI () {
                  JLabel myLabel = new JLabel(getOrder());
                  
                  }
                  Option B
                  String st = null;
                  public void getOrder() {
                            st =doc.getElementsByTagName("order_naam").item(0).getFirstChild().getNodeValue();
                  }
                   public void createGUI () {
                  JLabel myLabel = new JLabel(st);
                  
                  }
                  Well i think i have explained your problem well enough already really, im surprised you could not pick it up from the previous two examples, but to reitterate here it is again!

                  Calypso
                  • 6. Re: call String from another method
                    843785
                    Well I've tried option B on my own but that doesnt work (I get an empty label)
                    and for A I've to change my method which I dont want.
                    • 7. Re: call String from another method
                      800313
                      you probably get an empty label becasue you execute createGui() before getOrder()
                      • 8. Re: call String from another method
                        843785
                        I"ve this in my constructor
                        this.getOrder();
                        this.createGUI();

                        and also my createGUI repaints
                        • 9. Re: call String from another method
                          800313
                          well have you tried using System.out.println() to check sd is actually not zero in the getOrder() method
                          • 10. Re: call String from another method
                            843785
                            I''ve tried that and isnt empty
                            • 11. Re: call String from another method
                              800313
                              Global variables and getting output from methods is one of the basic things in java, im not really sure what your doing wrong but i think you need to go over the basics before carrying on with your Gui.

                              The only other thing i think of is that you havent quite followed my example correctly and still have "String" before "sd" in the getOutput method