Jump to content

Noob question JAVA

Go to solution Solved by MrSuperb,
class Welcome {     public static void main (String[] arg) {         System.out.println("Welcome " + arg[0] + "!") ;    }}

arg (short for arguments) is a String array which contains the arguments you parse in the console.

 

e.g. if you write

java Welcome Scia

Scia is stored in arg[0] ... the first place in the arg array

Hi,

I just started taking Java 101 a week ago and I'm kinda stuck with my "homework" as we are supposed to write program which does the following:

java Welcome SciaWelcome Scia!

Now, I really can't get it to do that.

 

I got a slightly different program working which does:

class Welcome { public static void main (String[] arg) { System.out.println("Please enter your name! \n") ; String name = In.readLine() ; System.out.print("Welcome") ;System.out.print(name) ;System.out.print("!") ;}}

Which outputs:

java WelcomePlease enter your name!SciaWelcome Scia!

How do I get it to work for it to read the name right away once I type

java Welcome Scia

? ^^

 

Any help would be greatly appreciated ^^

Frost upon these cigarettes.... lipstick on the window pane...

Link to comment
https://linustechtips.com/topic/60920-noob-question-java/
Share on other sites

Link to post
Share on other sites

class Welcome {     public static void main (String[] arg) {         System.out.println("Welcome " + arg[0] + "!") ;    }}

arg (short for arguments) is a String array which contains the arguments you parse in the console.

 

e.g. if you write

java Welcome Scia

Scia is stored in arg[0] ... the first place in the arg array

Mini-Desktop: NCASE M1 Build Log
Mini-Server: M350 Build Log

Link to comment
https://linustechtips.com/topic/60920-noob-question-java/#findComment-828829
Share on other sites

Link to post
Share on other sites

a more secure variant:

class Welcome {     public static void main (String[] arg) {       if (arg.length > 0) {           System.out.println("Welcome " + arg[0] + "!") ;       }    }}

edit: omfg ... my java skills got bad -.-

Mini-Desktop: NCASE M1 Build Log
Mini-Server: M350 Build Log

Link to comment
https://linustechtips.com/topic/60920-noob-question-java/#findComment-828880
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×