?
Solved

A very simple program

Posted on 2004-10-30
22
Medium Priority
?
240 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
Comment
Question by:daveask
  • 12
  • 7
  • 3
22 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 12453375
public static void main
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 600 total points
ID: 12453379

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
petmagdy earned 200 total points
ID: 12453390
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
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 

Author Comment

by:daveask
ID: 12453392
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
ID: 12453426
like this:

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


0
 
LVL 13

Expert Comment

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

Expert Comment

by:CEHJ
ID: 12453433
>>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
ID: 12453441
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
ID: 12453594
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
ID: 12453606
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
ID: 12453615
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
ID: 12453619
That would give you a 10 X 10 array of random numbers below 100
0
 

Author Comment

by:daveask
ID: 12453640
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
ID: 12453652
>>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
ID: 12453725
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
ID: 12453732
You must save the source file as MyClass.java, and it's case-sensitive
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12453737
If you're in the same directory

javac MyClass.java

java MyClass
0
 

Author Comment

by:daveask
ID: 12453765
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
ID: 12453779
java -classpath . MyClass
0
 

Author Comment

by:daveask
ID: 12453886
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
ID: 12453912
Please post your command line and say in what directory your class and source are
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12453980
8-)
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
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…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month5 days, 18 hours left to enroll

588 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