Programming
Argument Help!

Question Description

How can I make the mail from line come before the recpt to line in the output...

public class ArgsExample {<br>&nbsp; &nbsp;public static void main(String[] args) {<br>&nbsp; &nbsp; &nbsp;String from = "";<br>&nbsp; &nbsp; &nbsp;String to = "";<br>&nbsp; &nbsp; &nbsp;String subject = "";<br>&nbsp; &nbsp; &nbsp;String emailText = "";<br>&nbsp; &nbsp; &nbsp;System.out.println("number of args " + args.length);<br>&nbsp; &nbsp; &nbsp;for (int i = 0; i < args.length; i++) <br>&nbsp; &nbsp; &nbsp;{<br>&nbsp; &nbsp; &nbsp; &nbsp;if (args[i].startsWith("From:")) {<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;from = args[i];<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.out.println("MAIL " + from);<br>&nbsp; &nbsp; &nbsp; &nbsp;} else if (args[i].startsWith("To:")) {<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;to = args[i];<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.out.println("RCPT " + to);<br>&nbsp; &nbsp; &nbsp; &nbsp;} else if (args[i].startsWith("Subject:")) {<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;subject = args[i];<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.out.println(subject);<br>&nbsp; &nbsp; &nbsp; &nbsp;} else { // The rest<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;emailText += args[i]+" ";<br>&nbsp; &nbsp; &nbsp; &nbsp;}<br>&nbsp; &nbsp; &nbsp;}// for<br>&nbsp; &nbsp; &nbsp;System.out.println(emailText);<br>&nbsp; &nbsp;}// main<br>}<br><br

The argument line is:

java ArgsExample To:peter999@gmail From:1234567@yahoo.com Subject:Meeting Body:See you at 1pm


The output is: 

number of args ###
MAIL FROM: 1234567@yahoo.com
RCPT TO: peter999@gmail
Subject: Meeting
Body: See you at 1pm


Final Answer

This will work, it all depends on the order of results you are displaying. Don't print the results right after you read em, just store them in variables and then print it in the order you desire ! Below is the modified code and the output.

Let me know if you need anything else, cheers!

public class ArgsExample {
   public static void main(String[] args) {
     String from = "";
     String to = "";
     String subject = "";
     String emailText = "";
     System.out.println("number of args " + args.length);
     for (int i = 0; i < args.length; i++) 
     {
       if (args[i].startsWith("From:")) {
         from = args[i];
       } else if (args[i].startsWith("To:")) {
         to = args[i];
       } else if (args[i].startsWith("Subject:")) {
         subject = args[i];
       } else { // The rest
           emailText += args[i]+" ";
       }
     }// for
     
        System.out.println("MAIL " + from);
        System.out.println("RCPT " + to);
        System.out.println(subject);
System.out.println(emailText);
   }// main
}
OUTPUT
number of args 7
MAIL From:1234567@yahoo.com
RCPT To:peter999@gmail
Subject:Meeting
Body:See you at 1pm 

fixit (96)
New York University

Anonymous
I was on a very tight deadline but thanks to Studypool I was able to deliver my assignment on time.

Anonymous
The tutor was pretty knowledgeable, efficient and polite. Great service!

Anonymous
Heard about Studypool for a while and finally tried it. Glad I did caus this was really helpful.

Studypool
4.7
Trustpilot
4.5
Sitejabber
4.4

Brown University





1271 Tutors

California Institute of Technology




2131 Tutors

Carnegie Mellon University




982 Tutors

Columbia University





1256 Tutors

Dartmouth University





2113 Tutors

Emory University





2279 Tutors

Harvard University





599 Tutors

Massachusetts Institute of Technology



2319 Tutors

New York University





1645 Tutors

Notre Dam University





1911 Tutors

Oklahoma University





2122 Tutors

Pennsylvania State University





932 Tutors

Princeton University





1211 Tutors

Stanford University





983 Tutors

University of California





1282 Tutors

Oxford University





123 Tutors

Yale University





2325 Tutors