Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: how to test arrays in a terminal in java?

  1. #1
    Member
    Join Date
    Jan 2013
    Posts
    31
    My Mood
    Confused
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default how to test arrays in a terminal in java?

    public class OneA {
    	public int product( int [] a ) {
    		int product = 1;
    		for ( int i = 0; i < a.length; i++ ) {
    		product *= a[i];
    		}
    		return product;
    		}
    		}

    the above code should give me results like the following:
    product(new int[] {1, 2, 3, 4, 5}) -> 120
    product(new int[] {2, 3, 7}) -> 42

    i know the code is right but how do i test it in the terminal? after compiling the class i.e javac OneA.java, what should be the next command in the terminal?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: how to test arrays in a terminal in java?

    After using the javac command to compile a program,
    use the java command to execute the class created by the javac command:
    java OneA
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Jan 2013
    Posts
    31
    My Mood
    Confused
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: how to test arrays in a terminal in java?

    If i just type java OneA i get the following error: Exception in thread "main" java.lang.NoSuchMethodError: main

    I know it's java OneA but after that what should be written so that it can give me the product of say 2 3 and 4.

    Like if i have a java program Add.java and i have 2 arguments defined so at the terminal i would say javac Add.java and then java Add 2 3 which will give me 5

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: how to test arrays in a terminal in java?

    Take a look at the tutorial about how to write a java program and execute it:
    "Hello World!" for Microsoft Windows (The Java™ Tutorials > Getting Started > The "Hello World!" Application)
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Jan 2013
    Posts
    31
    My Mood
    Confused
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: how to test arrays in a terminal in java?

    about the link Norm provided..i know that...i need the explanation to "test" "arrays" in a terminal.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: how to test arrays in a terminal in java?

    Please explain what you mean by "test arrays".


    java.lang.NoSuchMethodError: main
    A class must have a main() method to be executed using the java command.
    The link provided in post#4 shows how to code a main() method.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Integrated Java With Terminal
    By MFD-Stark in forum Java Theory & Questions
    Replies: 2
    Last Post: January 2nd, 2013, 12:23 PM
  2. Display HTMl code in JAVA terminal...
    By Alkirk in forum Java Theory & Questions
    Replies: 3
    Last Post: November 16th, 2011, 03:34 AM
  3. [SOLVED] Stopping java in terminal
    By Scotty in forum Java Theory & Questions
    Replies: 1
    Last Post: May 7th, 2011, 06:53 PM
  4. Terminal Tamagotchi
    By FlurrYx in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 12th, 2011, 01:05 PM
  5. Need some help with OSX Java (Terminal)
    By mkoop in forum What's Wrong With My Code?
    Replies: 5
    Last Post: January 14th, 2011, 12:23 PM