Solved

A very simple program

Posted on 2004-10-30
192 Views
Last Modified: 2010-03-31
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()?
0
Question by:daveask
    22 Comments
     
    LVL 86

    Expert Comment

    by:CEHJ
    public static void main
    0
     
    LVL 86

    Accepted Solution

    by:

    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
     
    LVL 13

    Assisted Solution

    by:petmagdy
    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
     

    Author Comment

    by:daveask
    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
     
    LVL 13

    Expert Comment

    by:petmagdy
    like this:

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


    0
     
    LVL 13

    Expert Comment

    by:petmagdy
    daveask, u can also use my class skeleton
    0
     
    LVL 86

    Expert Comment

    by:CEHJ
    >>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
     
    LVL 86

    Expert Comment

    by:CEHJ
    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
     

    Author Comment

    by:daveask
    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
     

    Author Comment

    by:daveask
    petmagdy,

    As you can see, I know only little in Java....why your class skeleton is so complex?
    0
     
    LVL 86

    Expert Comment

    by:CEHJ
    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
     
    LVL 86

    Expert Comment

    by:CEHJ
    That would give you a 10 X 10 array of random numbers below 100
    0
     

    Author Comment

    by:daveask
    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
     
    LVL 86

    Expert Comment

    by:CEHJ
    >>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
     

    Author Comment

    by:daveask
    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
     
    LVL 86

    Expert Comment

    by:CEHJ
    You must save the source file as MyClass.java, and it's case-sensitive
    0
     
    LVL 86

    Expert Comment

    by:CEHJ
    If you're in the same directory

    javac MyClass.java

    java MyClass
    0
     

    Author Comment

    by:daveask
    Yes, you are right, case-sensitive!

    Now it said
    Exception in thread "main" Java.lang.NoClassDefFoundError: c:\...MyClass
    0
     
    LVL 86

    Expert Comment

    by:CEHJ
    java -classpath . MyClass
    0
     

    Author Comment

    by:daveask
    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
     
    LVL 86

    Expert Comment

    by:CEHJ
    Please post your command line and say in what directory your class and source are
    0
     
    LVL 86

    Expert Comment

    by:CEHJ
    8-)
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    Suggested Solutions

    An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
    Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
    This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
    This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

    913 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

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now