Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-29-2007, 12:51 AM
Member
 
Join Date: Nov 2007
Posts: 20
Gilgamesh is on a distinguished road
qu
public class peiramus {
public void main (String [] args) { /*inside here i write the sequence of the method i want right? or its not necessary? (can i leave it empty? is it gonna follow the flow of the text (reading 'word by word'?)? */
method1 (); //this is OK
method2 (); //but here it doesnt accept them (its obvious it has to do with the fact that i 'put things' inside the parentheses) so what i do here?
method3 ();

}
private void method1 () {
blah blah
}
private void method2 (String [] Array1){ //taken from method1
blah blah
}
private void method3 (String [] DictionaryArray, int method4){ //taken form method 2 and method 4)
blah blah
private int method4 (blah blah){
if.. blah blah
blah blah.. return -1; //send the result to method3 }

Last edited by Gilgamesh : 11-29-2007 at 02:13 AM.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 11-29-2007, 01:23 AM
Senior Member
 
Join Date: Nov 2007
Location: Newport, WA
Posts: 141
staykovmarin is on a distinguished road
Code:
private void method2 (String [] Array1){ ... }
That method accepts an Array of Strings in its argument. So if you make an array you can put it in there.

edit:
Well all those methods are void, so doing:
Code:
method1 (); method2 ();
is okay, since they dont return anything.

But method3() returns an integer, so i am assuming that you want to do something with it.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 11-29-2007, 01:31 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,222
hardwired is on a distinguished road
Code:
import java.util.*; public class ExpTest { public static void main(String[] args) { ExpTest test = new ExpTest(); String[] s = test.method1(); System.out.printf("s = %s%n", Arrays.toString(s)); String[] caps = test.method2(s); System.out.printf("caps = %s%n", Arrays.toString(caps)); int n = test.method4(s); System.out.println("n = " + n); test.method3(caps, n); } private String[] method1() { return new String[] { "hello", "world" }; } //taken from method1 private String[] method2(String[] array1) { String[] returnVal = new String[array1.length]; for(int j = 0; j < array1.length; j++) { String s = ""; for(int k = 0; k < array1[j].length(); k++) { s += String.valueOf(array1[j].charAt(k)).toUpperCase(); } returnVal[j] = s; } return returnVal; } //taken form method 2 and method 4 private void method3(String[] array, int method4) { System.out.println("Selected index = " + method4 + " value = " + array[method4]); } //send the result to method3 private int method4(String[] array) { int index= 0; if(array.length % 2 == 0) index = array.length-1; return index; } }
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 11-29-2007, 02:05 AM
Member
 
Join Date: Nov 2007
Posts: 20
Gilgamesh is on a distinguished road
well my question was focusing on the red comments (i.e. how we organize the main class). I have finished my coding. My problem is how to refer methods that they have arguments in the main method. I can put easily methods with no arguments, like
Code:
method1();
in the main method, but I cant refer the methods that have arguments inside the main method.

Code:
public void main (String [] args) { method1 (); //here ok, no problem. method2 (arguments* here e.g. String string, String [] array etc); //if I do this java starts yelling at me }
*I mean the same arguments thar are necessary to write the method2. Do we write them as arguments when we refer method2 at the main method? OR we are supposed to write inside the parentheses, the 'result' of the method? OR nothing (but anyway if I just type method2(); it doesnt accept it)?

Last edited by Gilgamesh : 11-29-2007 at 02:14 AM.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 11-29-2007, 02:15 AM
Senior Member
 
Join Date: Nov 2007
Location: Newport, WA
Posts: 141
staykovmarin is on a distinguished road
You do it exactly the way hardwired showed you
Code:
ExpTest test = new ExpTest();
The main method in any java program is static. That means that you cant call non-static methods from it (ie: method1()). You have to first create an instance of that class (the code above) then execute the methods:
Code:
test.method();
//taken from method1
How do you mean? method1 is void, it doesn't return anything.

Read over hardwired more carefully.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 11-29-2007, 03:15 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,222
hardwired is on a distinguished road
method2(arguments* here e.g. String string, String [] array etc)
Code:
String s = "hello world"; // use "s" as an argument to the takesAString method: takesAString(s); // To call a method that takes a Rectangle argument // make a Rectangle Rectangle square = new Rectangle(100,100,200,200); // and pass it to the method takesRectangle(rect); ... void takesAString(String str) { // do something with "str" } void takesRectangle(Rectangle r) { // do something with "r" }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +3. The time now is 03:08 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org