[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Why isn't my array printing w/in function?

Posted on 2008-10-06
10
Medium Priority
?
202 Views
Last Modified: 2012-05-05
Why isn't my array printing w/in the prinArray function?  It prints if in main, but I need it to reside in a function.  Please help.  Below is my code.


public class yamotdProtocol {
 
	static String[] message = new String[5];
	static Scanner is = null;
	static String m = null;
	private static String line;
	private static String msgget = "MSGGET";
	
	public static void main(String args[]) throws IOException {
	
	try {
		
		is = new Scanner(new FileInputStream("C:\\messages.txt"));
	}
	
	catch(FileNotFoundException e){}
	
	for(int i=0; i<message.length; i++)
	{
		line = is.nextLine();
		message[i] = line;
	}
	
	for(int n =0; n<message.length; n++)
		System.out.println(message[n]);
	}
	
	public void printArray(){
		
		for(int n =0; n<message.length; n++)
			System.out.println(message[n]);
	}
}

Open in new window

0
Comment
Question by:deliachang
  • 4
  • 4
  • 2
10 Comments
 
LVL 24

Expert Comment

by:sciuriware
ID: 22651345
Because 'printArray() is a separate method, that's never called.

;JOOP!
0
 
LVL 8

Accepted Solution

by:
mnrz earned 2000 total points
ID: 22651401
it will, but you have to call that method
check this:


public class yamotdProtocol {
 
        static String[] message = new String[5];
        static Scanner is = null;
        static String m = null;
        private static String line;
        private static String msgget = "MSGGET";
        
        public static void main(String args[]) throws IOException {
        
        try {
                
                is = new Scanner(new FileInputStream("C:\\messages.txt"));
        }
        
        catch(FileNotFoundException e){}
        
        for(int i=0; i<message.length; i++)
        {
                line = is.nextLine();
                message[i] = line;
        }
        
        /*for(int n =0; n<message.length; n++)
                System.out.println(message[n]);
         */
        yamotdProtocol instance = new yamotdProtocol();
        instance.printArray()  ;
        }
        
        public void printArray(){
                
                for(int n =0; n<message.length; n++)
                        System.out.println(message[n]);
        }
}

Open in new window

0
 
LVL 24

Expert Comment

by:sciuriware
ID: 22651409
Change your code to something like this:

public class yamotdProtocol
{
   static String[] message = new String[5];
   static Scanner is = null;
   static String m = null;
   private static String line;
   private static String msgget = "MSGGET";

   public static void main(String args[]) throws IOException
   {
      try
      {
         is = new Scanner(new FileInputStream("C:\\messages.txt"));
      }
      catch(FileNotFoundException e){ /* No action */ }

      for(int i = 0; i < message.length; i++)
      {
         line = is.nextLine();
         message[i] = line;
      }

      for(int n = 0; n < message.length; n++)
         System.out.println(message[n]);
     
      printArray();  // This makes sense!!!!
   }
   
   public static void printArray()
   {

      for(int n = 0; n < message.length; n++)
         System.out.println(message[n]);
   }

}

;JOOP!
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 8

Expert Comment

by:mnrz
ID: 22651423
or you can declare that method as static:

also please note in Java we don't say function, method is used instead

public static void printArray(){
                
                for(int n =0; n<message.length; n++)
                        System.out.println(message[n]);
        }

Open in new window

0
 
LVL 24

Expert Comment

by:sciuriware
ID: 22651705
I disagree with mnrz: printArray() was embedded inside 'main()'
by bad positioning of { } and it was not called.

;JOOP!
0
 

Author Comment

by:deliachang
ID: 22651749
mnrz, your solution work.  I do have another question and not sure if it can work.  Can I call this function from another class within the same package?

If I declare the an instance of the protocol class in another class, I get nothing but nulls.  Why is this?
public String printArray(String userInput){
		
		String theOutput = userInput;
				
		if (theOutput.equals("MSGGET"))
			theOutput = "It works." + message[0];
		
		return theOutput;
	}

Open in new window

0
 

Author Closing Comment

by:deliachang
ID: 31503472
Thank you so much.  I actually got it working - even calling it from another class.
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 22651948
Thanks deliachang.

;JOOP!
0
 
LVL 8

Expert Comment

by:mnrz
ID: 22652042
sciuriware
no the closing bracket was in right place but the indent was not correct.

deliachang
since the method is declared as public you can call this method from another class but the process in which your message array is populating is placed in main() method and this method is static, so you have to call it before printArray()

check this example out:
 
0
 
LVL 8

Expert Comment

by:mnrz
ID: 22652117
sorry this is example:

public class YourClassName {
 
        String[] message = new String[5];
        Scanner is = null;
        String m = null;
        private String line;
        private String msgget = "MSGGET";
        
        public void populateArray() throws IOException {
        
        try {
                
                is = new Scanner(new FileInputStream("C:\\messages.txt"));
        }
        
        catch(FileNotFoundException e){}
        
        for(int i=0; i<message.length; i++)
        {
                line = is.nextLine();
                message[i] = line;
        }
      }
        
        public void printArray(){
                
                for(int n =0; n<message.length; n++)
                        System.out.println(message[n]);
        }
 
public String printArray(String userInput){
                
                String theOutput = userInput;
                                
                if (theOutput.equals("MSGGET"))
                        theOutput = "It works." + message[0];
                
                return theOutput;
        }
}
 
// in other class
public class AnotherClass {
 
 
        public static void main(String[] a) {
             YourClassName instance = new YourClassName();
             instance.populateArray();
             instance.printArray();
              instance.printArray("MSGGET");
         }
}

Open in new window

0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Suggested Courses
Course of the Month17 days, 15 hours left to enroll

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question