The Artima Developer Community
Sponsored Link

Java Answers Forum
Command line arguments - Printing out one string

4 replies on 1 page. Most recent reply: Dec 9, 2004 12:50 AM by Matthias Neumair

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 4 replies on 1 page
Alan Fung

Posts: 5
Nickname: afu
Registered: Dec, 2004

Command line arguments - Printing out one string Posted: Dec 7, 2004 1:32 AM
Reply to this message Reply
Advertisement
I am reading arguments from a user. I have to print out the array as is and backwards. I used if else statement for them. If the user entered arguments with atleast one character then it'll print as is and backwards, else print "no arguments."

Now my problem is that I can't figure out how to print just the ONE string if the user only inputted one string. Because if the user inputted one string it would print out twice since I have two for statements to print both forward and backwards.

here's what I have so far:

if(args.length >0) {
if(args.length =args[0]) {
System.out.println(args[0]);
}
else {
//printing forwards
System.out.println("-Your arguments forwards-");
for (i=0; i < args.length; i++) {
System.out.println(args);
}//close for

//printing backwards
System.out.println("-Your arguments backwards-");
for(i = args.length-1; i>=0; i--) {
System.out.println(args);
}//close for
}//close else
}//close if

//if the user didn't enter any arguments
else {
System.out.println("No arguments");
}//close else


}//close main

Help is MUCH appreciated.


Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: Command line arguments - Printing out one string Posted: Dec 7, 2004 5:33 AM
Reply to this message Reply
//This is your method, I just wrote some comments.
    public static void main (String[] args) {
        if(args.length >0) {
            if(args.length = args[0]) { //how were you able to compile this? You are assigning a String to a int and expect a boolean in return.
                System.out.println(args[0]);
            } else {
                //printing forwards
                System.out.println("-Your arguments forwards-");
                    for (i=0; i < args.length; i++) {// did you declare i before the part of the code you posted?
                        System.out.println(args); //you're printing the whole Array?
                }
                //printing backwards
                System.out.println("-Your arguments backwards-");
                for(i = args.length-1; i>=0; i--) {// did you declare i before the part of the code you posted?
                    System.out.println(args);
                }
            }
        } else { //if the user didn't enter any arguments
            System.out.println("No arguments");
        }
    }
 
    //This is the first way that came into my mind
    public static void main (String[] args) {
        if (args.length == 1) {
            System.out.println(args[0]);
        }
        else if (args.length > 1) {
            System.out.println("-Your arguments forwards-");
            for (int i = 0; i < args.length; i++)
                System.out.println (args[i]);
            System.out.println("-Your arguments backwards-");
            for (int i = args.length - 1; i >= 0; i--)
                System.out.println (args[i]);
        }
        else
            System.out.println("No arguments");
    }
 
    //This is a more elegant version
    public static void main (String[] args) {
        switch (args.length) {
            case 0: 
                System.out.println("No arguments");
		break;
            case 1:
                System.out.println(args[0]);
                break;
            default:
                System.out.println("-Your arguments forwards-");
                for (int i = 0; i < args.length; i++)
                    System.out.println (args[i]);
                System.out.println("-Your arguments backwards-");
                for (int i = args.length - 1; i >= 0; i--)
                    System.out.println (args[i]);
                break;
        }
    }
 

Alan Fung

Posts: 5
Nickname: afu
Registered: Dec, 2004

Re: Command line arguments - Printing out one string Posted: Dec 7, 2004 8:53 PM
Reply to this message Reply
Wow, thank you so much. Much appreciated.

Alan Fung

Posts: 5
Nickname: afu
Registered: Dec, 2004

Re: Command line arguments - Printing out one string Posted: Dec 7, 2004 10:22 PM
Reply to this message Reply
Er, I have another question about this. I need to write the arguments the user inputted to a File. I've tried many things, but I get this in my out put file...

[Ljava.lang.String;@3179c3

No idea.

I have the same coding as the previous post with this added to it...

//writing the arguments to the output file
File fout = new File("output.txt");
FileWriter fw = new FileWriter(fout);
BufferedWriter bw = new BufferedWriter(fw);
PrintWriter pw = new PrintWriter(bw);

pw.println(args);
pw.close();

I used a while statement, but it just loops and never ends the program.

Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: Command line arguments - Printing out one string Posted: Dec 9, 2004 12:50 AM
Reply to this message Reply
args is an array.

If you try to save the array as it is to a file you will get only the args.toString() content wich in this case is the description of the array.

To write the CONTENT, you must use the index.


This is the method I wrote for my personal use:
    public static void saveToFile (String filename, String[] fileContent, boolean doOutput) {
        saveToFile (new File (filename), fileContent, doOutput);
    }
 
    public static void saveToFile (File file, String[] fileContent, boolean doOutput) {
        try {
            PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(file, false)));
            if (doOutput)
                System.out.print ("Writing ");
            for (int i = 0; i < fileContent.length; i++) {
                out.println(fileContent[i]);
                if (doOutput)
                    System.out.print (".");
            }
            if (doOutput)
                System.out.println (" done");
            out.close();
        } catch (IOException e) {
            System.out.println("I/O Error: " + e.getMessage());
        }
    }



Instead of using the index, with the new Java version you should be able to use the following trick:
    public static void saveToFile (File file, String[] fileContent, boolean doOutput) {
        try {
            PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(file, false)));
            if (doOutput)
                System.out.print ("Writing ");
            for (String line : fileContent) { //This is the for-each construct: it does exactly the same as the for-to statement shown abowe. In addition, it assigns line = fileContent[i]
                out.println(line);
                if (doOutput)
                    System.out.print (".");
            }
            if (doOutput)
                System.out.println (" done");
            out.close();
        } catch (IOException e) {
            System.out.println("I/O Error: " + e.getMessage());
        }
    }

Flat View: This topic has 4 replies on 1 page
Topic: thread pooling in java Previous Topic   Next Topic Topic: Affine Cipher

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use