A very simple program

Hi Experts,

I am downloading J2sdk and eclipse-sdk to write and run Java code. In the same time, can you tell me how to write a very simple program something like:

void main(String args[])
{   double[][] myArray;
    //set some values to myArray
    call MySubroutine(myArray);
    system.out.print myArray;
{
void MySubroutine(myArray)
{   //calculat and change something in myArray
}

And do I need to use Class above void main() and public static before void main()?
daveaskAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CEHJCommented:
public static void main
0
CEHJCommented:

public static void main(String args[])
{  double[][] myArray = { YOU MUST INITIALISE HERE ! };
   //set some values to myArray
   MySubroutine(myArray);
   for(int i = 0;i < myArray.length;i++)
   {
         System.out.println(myArray[i]);
     
   }
   
}
static void MySubroutine(double[][] myArray)
{   //calculate and change something in myArray
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
petmagdyCommented:
ok,

try this class:

import java.util.Hashtable;
import java.util.Iterator;
import java.util.Set;

/*
 * Created on 30/10/2004
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */

public class MyClass {

      public Hashtable addressBook;
      
      public MyClass()
      {
            addressBook = new Hashtable();
      }
      
      public static void main(String[] args) {
            MyClass myClass = new MyClass();
            myClass.addAddressBook("John", "6765765756");
            myClass.displayAddressBook();
      }
      
      public void addAddressBook(String name, String telephone )
      {
            addressBook.put(name, telephone);
      }
      
      public void displayAddressBook()
      {
             Set set=addressBook.keySet();
              Iterator iterator=set.iterator();
              while(iterator.hasNext())
              {
                  Object key=iterator.next();
                  System.err.println("Name : "+key);
                  System.err.println("Telephone :"+addressBook.get(key));
              }
            
      }
}
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

daveaskAuthor Commented:
CEHJ,

Thank you for so prompt response!

So, you said I don't need to use Class here, why?
Why I need put static before void?
Notice, I used a two dimesional array double[][], how can I initialise like what you suggested?
0
petmagdyCommented:
like this:

            double[][] myArray = { {1.0, 2.0} , {3.0, 4.0} };


0
petmagdyCommented:
daveask, u can also use my class skeleton
0
CEHJCommented:
>>So, you said I don't need to use Class here, why?

No you do - i assumed you knew that.

>>Why I need put static before void?

Because a Java application is started by this method before any instance is present. This is necessary
0
CEHJCommented:
public class X
{
      public static void main(String args[])
      {  double[][] myArray = { YOU MUST INITIALISE HERE ! };
         //set some values to myArray
         MySubroutine(myArray);
         for(int i = 0;i < myArray.length;i++)
         {
            System.out.println(myArray[i]);

         }

      }
      static void MySubroutine(double[][] myArray)
      {   //calculate and change something in myArray
      }
}      
      
      
0
daveaskAuthor Commented:
CEHJ,
Thank you.
I might need to set many values to myArray in a loop! How can I initialise values like what you said?
0
daveaskAuthor Commented:
petmagdy,

As you can see, I know only little in Java....why your class skeleton is so complex?
0
CEHJCommented:
Yes, you could do something like:

double[][] myArray = new double[10][10];
for(int i = 0;i < myArray.length;i++) {
      for(int j = 0;j < myArray[i].length;j++) {
            myArray[i][j] = Math.random() * 100;
      }
}
0
CEHJCommented:
That would give you a 10 X 10 array of random numbers below 100
0
daveaskAuthor Commented:
CEHJ,

Thank you. That is great!
Can you also tell me how to set the array size dynamically:

double[][] myArray;
int mySize = 10;
myArray = new double[mySize][mySize];
0
CEHJCommented:
>>Can you also tell me how to set the array size dynamically:

Your own code example does just that, and since the code in my loops is *not* hard-coded, it will work with any valid value that you give to 'mySize'
0
daveaskAuthor Commented:
OK, I can run my program now!
However, I got error:
cannot read: MyClass.java
1 error
I did nothing in my program:
public class MyClass
{
     public static void main(String args[])
     {  double[][] myArray = new double[10][10];
        MySubroutine(myArray);
        for(int i = 0;i < myArray.length;i++)
        {
        }
     }
     static void MySubroutine(double[][] myArray)
     {  
     }
}    
0
CEHJCommented:
You must save the source file as MyClass.java, and it's case-sensitive
0
CEHJCommented:
If you're in the same directory

javac MyClass.java

java MyClass
0
daveaskAuthor Commented:
Yes, you are right, case-sensitive!

Now it said
Exception in thread "main" Java.lang.NoClassDefFoundError: c:\...MyClass
0
CEHJCommented:
java -classpath . MyClass
0
daveaskAuthor Commented:
I tried every thing, but still got the same exception......I tried to put MyClass.class in the bin\ and tried to set path and classpath as the other expert suggested but none of them works.....
0
CEHJCommented:
Please post your command line and say in what directory your class and source are
0
CEHJCommented:
8-)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.