Java Training Course/JT03: Difference between revisions

From tehowiki
Jump to navigation Jump to search
imported>Gfis
new
 
imported>Gfis
No edit summary
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
* Create a file <code>DupString.java</code> with the following modified copy of the <code>HelloWorld</code> program:
==Main Arguments and String Concatenation==
In this session we will pass 2 parameters from the commandline to the program. There we will access, concatenate and output them.
* Create a file <code>DupString.java</code> with the following modified copy of the <code>HelloWorld</code> program of session [[Java Training Course/JT02|JT02]]:
  public class DupString {
  public class DupString {
     public static void main(String[] args) {
     public static void main(String[] args) {
Line 9: Line 11:
     } // main
     } // main
  } // DupString
  } // DupString
* Compile the program to be sure that you entered it with the correct syntax.
* Compile the program in order to be sure that you entered it with the correct syntax.
* Before running the program, tell what you would expect as output from the following command:
* Before running the program, tell what you would expect as output from the following command:
  java DupString Hello World
  java DupString Hello World
* Bonus task: How would you make the output more readable with little effort?
[[Java Training Course/JT02|&lt; Previous: JT02]] "Hello, World!"<br />
[[Java Training Course/JT04|&gt; Next: JT04]] Integer Arithmetic

Latest revision as of 09:50, 24 September 2017

Main Arguments and String Concatenation

In this session we will pass 2 parameters from the commandline to the program. There we will access, concatenate and output them.

  • Create a file DupString.java with the following modified copy of the HelloWorld program of session JT02:
public class DupString {
    public static void main(String[] args) {
        String st0 = args[0]; // the 1st parameter on the commandline
        String st1 = args[1]; // the 2nd ...
        String st2 = st0 + st1;
        st2 = st2 + st2 + st2 + st2;
        System.out.println(st2);
    } // main
} // DupString
  • Compile the program in order to be sure that you entered it with the correct syntax.
  • Before running the program, tell what you would expect as output from the following command:
java DupString Hello World
  • Bonus task: How would you make the output more readable with little effort?

< Previous: JT02 "Hello, World!"
> Next: JT04 Integer Arithmetic