10 Replies - 493 Views - Last Post: 15 November 2012 - 02:34 PM Rate Topic: -----

#1 Power25  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 13
  • Joined: 04-November 12

Useargument.java

Posted 15 November 2012 - 01:37 PM

When when i type this code i get this error?? Help pls
public class UseArgument
  {
  public static void main(String [] args)
    {
    System.out.print("Hi, ");
    System.out.print(args [ 0 ] );
    System.out.println(". How are you?");
  }
}
    





Hi, java.lang.ArrayIndexOutOfBoundsException: 0
at UseArgument.main(UseArgument.java:6)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)

Is This A Good Question/Topic? 0
  • +

Replies To: Useargument.java

#2 WolverineX  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 49
  • Joined: 11-November 12

Re: Useargument.java

Posted 15 November 2012 - 01:41 PM

What are you trying to do in line 06? If you remove it it'll work normally.
Was This Post Helpful? -1
  • +
  • -

#3 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7807
  • View blog
  • Posts: 13,203
  • Joined: 19-March 11

Re: Useargument.java

Posted 15 November 2012 - 01:48 PM

This will work if you provide some command-line argument (such as a name) when you call the program

>> java UseArgument Power25

will produce

Hi, Power25. How are you?

View PostWolverineX, on 15 November 2012 - 03:41 PM, said:

What are you trying to do in line 06? If you remove it it'll work normally.



What he's trying to do is to use a command-line argument in the program. So removing that line would defeat the purpose.
Was This Post Helpful? 1
  • +
  • -

#4 Power25  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 13
  • Joined: 04-November 12

Re: Useargument.java

Posted 15 November 2012 - 01:53 PM

Actually i try to do this exercise but sth must be wrong with my pc i think.
http://introcs.cs.pr...ument.java.html

it doesen;t let me to add something, it prints this error

java.lang.ArrayIndexOutOfBoundsException: 0
at UseArgument.main(UseArgument.java:6)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)
Was This Post Helpful? 0
  • +
  • -

#5 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7807
  • View blog
  • Posts: 13,203
  • Joined: 19-March 11

Re: Useargument.java

Posted 15 November 2012 - 01:57 PM

I suppose you're using an IDE of some sort.You can probably feed it command-line arguments, but I don't know how you go about that.

Better you throw your Eclipse or your NetBeans over your shoulder and use an editor and javac, so you learn how to program properly.
Was This Post Helpful? 1
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,858
  • Joined: 06-March 08

Re: Useargument.java

Posted 15 November 2012 - 02:09 PM

View PostWolverineX, on 15 November 2012 - 04:41 PM, said:

What are you trying to do in line 06? If you remove it it'll work normally.

No, the OP wants to use an argument as the Topic title seems to claim
Was This Post Helpful? 0
  • +
  • -

#7 Power25  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 13
  • Joined: 04-November 12

Re: Useargument.java

Posted 15 November 2012 - 02:12 PM

i don't understand what is the problem? (Sry for my English)
Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,858
  • Joined: 06-March 08

Re: Useargument.java

Posted 15 November 2012 - 02:12 PM

View PostPower25, on 15 November 2012 - 04:53 PM, said:

Actually i try to do this exercise but sth must be wrong with my pc i think.

lol))) I seriously doubt :)
Have you tried from the command line ?
|If you use an IDE (bad idea at the stage you are in) you will have to:
- do it at the console prompt
- instruct your IDE that you want to pass parameter at execution time
Was This Post Helpful? 0
  • +
  • -

#9 Power25  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 13
  • Joined: 04-November 12

Re: Useargument.java

Posted 15 November 2012 - 02:19 PM

I only use newest version of Dr.Java with Eclipse Compiler 0.A48
Was This Post Helpful? 0
  • +
  • -

#10 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,858
  • Joined: 06-March 08

Re: Useargument.java

Posted 15 November 2012 - 02:25 PM

Open a console window...
cd to the directory where is UseArgument.class

type

>> java UseArgument Power25

and it should work if the java/bin directory is in your %PATH% variable

Another typical example where IDE should not be sed in the first days of Java :)
Was This Post Helpful? 1
  • +
  • -

#11 Power25  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 13
  • Joined: 04-November 12

Re: Useargument.java

Posted 15 November 2012 - 02:34 PM

thanks
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1