?
Solved

command line inputs

Posted on 2003-03-12
41
Medium Priority
?
198 Views
Last Modified: 2010-03-31
i want the command line inputs to go into an array.

e.g.:

java PROGRAM NAME 100 56 9 999 0 4 hello

will create an array such that

array[0] = 100
array[1] = 56
array[2] = 9
array[3] = 999
array[4] = 0
array[5] = 4
array[6] = hello

i'm sure its quite basic, but i'm having trouble getting head round it
0
Comment
Question by:Gesus
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 20
  • 18
  • 3
41 Comments
 

Author Comment

by:Gesus
ID: 8119717
whoops, it was meant to say java PROGRAMNAME (no space)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8119725
Are you using 1.4? If so

String[] inputs = line.split();
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8119789
Sorry - misreading the question:


  public static void main(String[] args) {
    String[] inputs = new String[8];
    inputs[0] = args[0];
    // etc
  }
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 35

Expert Comment

by:girionis
ID: 8119793
 ... otherwise just do a:

  String [] input = new String[arguments.length];

  where "arguments" is the String array you pass in your main method. Then do:

  for (int counter=0; counter<arguments.length); counter++)
  {
    input[i] = arguments[i];
  }

  Hope it helps.
0
 

Author Comment

by:Gesus
ID: 8119801
how does line.split() work?  what variables does it take?  and is the result an array?
0
 

Author Comment

by:Gesus
ID: 8119810
cheers, i'll have a play around with these methods
0
 
LVL 35

Expert Comment

by:girionis
ID: 8119841
 CEHJ sorry, did not see your answer.

  Actually this: counter<arguments.length);

  should be: counter<arguments.length; (with no parenthesis at the end).

 
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8119923
If you just want to make a copy of the arguments passed to the program you can do the following for greatest efficiency:

public static void main(String[] args) {
  if(args.length < 1){
    System.exit(-1);
  }
  String[] inputs = new String[args.length];
  System.arraycopy(args, 0, inputs, 0, inputs.length);
}
0
 

Author Comment

by:Gesus
ID: 8120234
hey, another thing.

how can i check if the input is a number?

say i wanted to do something like:

if arguments[0] is a number, do this...
else
do this
0
 
LVL 35

Expert Comment

by:girionis
ID: 8120277
 boolean number = true;
  try
  {
    int num = Integer.parseInt(arguments[0]);
  }
  catch (NumberormatException nfe)
  {
     number = false;
  }
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8121069
>>catch (NumberormatException nfe)

of course should be

>>catch (NumberFormatException nfe)
0
 

Author Comment

by:Gesus
ID: 8121136
i just used      

catch (Exception e) and its working so far

do you know anything bout stacks


to push a double i the stack i'm using:

lifo.push ( new Double(i) );

but how do i pop?

i can't say double i = stackname.pop()

as it returns an object instead of a double.
theres probably a method like the new double(i) thing needed to put it onto the stack?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8121199
>>i can't say double i = stackname.pop()

but you can say

>>double i = ((Double)stackname.pop()).doubleValue();
0
 

Author Comment

by:Gesus
ID: 8121329
ok, last thing (i hope)

what about if i'm checking if the thing i've put onto the stack is a +

i thought it would if be

  if (opp == '+')
  {

or

  if (opp == "+")
  {


but neither work
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8121350
If opp's a String you mean? If so

if (opp.equals("+")) {
  // take action
}
0
 

Author Comment

by:Gesus
ID: 8121484
just found another mistake from my first question.

i missed the " " things.
and coz i've got those, i won't be able to break it up as easily as the previous suggestions


java PROGRAMNAME "100 56 9 999 0 4 hello"
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8121514
What version Java you using?
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 210 total points
ID: 8121530
If you're using 1.4, you can use split() as i suggested above, otherwise:


String[] inputs = new String[8];
int index = 0;
StringTokenizer st = new StringTokenizer(args[0]);

while (st.hasMoreTokens()){
  inputs[index] = st.nextToken();
  index++;
}
0
 

Author Comment

by:Gesus
ID: 8121541
whast the new String[8];  thing?

the command line doesn't neccessarilly have 8 args in
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8121554
Well you'll have to use a collection. You still haven't told me what Java version you're using.
0
 

Author Comment

by:Gesus
ID: 8121577
sorry, its 1.4

so i do

String[] inputs = line.split();

and then the same as before?
0
 

Author Comment

by:Gesus
ID: 8121631
at the moment i've got:


  public static void main(String args[])
  {
    String [] input = new String[args.length];
    for (int i=0; i<args.length; i++)
    {
      input[i] = args[i];
    }
    check1(input);
  }



how would i get the line.split() thing in?

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8121634
Yes, or in this case,

String[] inputs = args[0].split();

if your command line arguments are in double quotes
0
 

Author Comment

by:Gesus
ID: 8121640
at the moment i've got:


  public static void main(String args[])
  {
    String [] input = new String[args.length];
    for (int i=0; i<args.length; i++)
    {
      input[i] = args[i];
    }
    check1(input);
  }



how would i get the line.split() thing in?

0
 

Author Comment

by:Gesus
ID: 8121655
sorry bout that double post, network is playing up
0
 

Author Comment

by:Gesus
ID: 8121678
so

  public static void main(String args[])
  {
    String[] inputs = new String[0];
    inputs = args[0].split();
    for (int i=0; i<inputs.length; i++)
    {
      input[i] = args[i];
    }
    check1(input);
  }

do i need to call any other java utils to get split to work, as it doesn't like it (can't resolve symbol) here
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8121740
public static void main(String args[])
 {
   if (args.length > 0)
   {
     inputs = args[0].split();
   }
   check1(input);
 }
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8121751
public static void main(String args[])
 {
   if (args.length > 0)
   {
     inputs = args[0].split();
     check1(input);
   }
 }
0
 

Author Comment

by:Gesus
ID: 8121778
it can't resolve symbol split
0
 

Author Comment

by:Gesus
ID: 8121953

  public static void main(String args[])
  {
   String [] inputs = new String[5000];
   if (args.length > 0)
    {
      inputs = args[0].split();
      check1(inputs);
    }
  }

it doesn't like split without a delimitor

e.g. it works for

      inputs = args[0].split(" ");

but then only takes in the 1st number
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8123282
>>it doesn't like split without a delimitor

Sorry, yes, that's necessary - thought there could be a default

>>but then only takes in the 1st number

Maybe there's only one parameter?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8123297
>>Maybe there's only one parameter?

i.e. if you have a command line like this

C:\>java ProgramName 1 2 3 4

it will only return the first, as opposed to:

C:\>java ProgramName "1 2 3 4"
0
 

Author Comment

by:Gesus
ID: 8123322
i couldn't get it to work.  i'm trying to tokenizer
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8123396
Please show your command line
0
 

Author Comment

by:Gesus
ID: 8123509
ok.  heres my entire program.  its not finished.  its a simple reverse polish calculator.


at the moment, if i put in 3 numbers, it loops and repeats the number over and over (no idea why)

if i put 2 numbers and a command, it errors taking the things off the list. (says theres nothing on list)


import java.util.Stack;
import java.util.StringTokenizer;
import java.lang.Double;

public class Stacks
{

  public static void main (String[] args)
  {
    if (args.length != 1)
    {
      System.out.println("Java Usage Stacks: Need to input 1 string in command line");
      System.exit(-1);
    }
    StringTokenizer st = new StringTokenizer (args[0], " ");
    String[] input = new String[10000];
    int i = 0;
    while(st.hasMoreTokens())
    {
      String s=st.nextToken();
      input[i]=s;
      i++;
    }
    String[] inputs = new String[i];
    for (int j = 0; i!=j; j++ )
    {
      inputs[j]=input[j];
    }
    check1(inputs);
  }


  static double dosum(double first, double second, String opp)
  {
  if (opp.equals("+"))
  {
    return (first+second);
  }
  if (opp.equals("-"))
  {
    return (first-second);
  }
  if (opp.equals("*"))
  {
    return (first*second);
  }
  if (opp.equals("/"))
  {
    return (first/second);
  }
  System.out.println("Inputs must be either numbers or + - * or /");
  System.exit(-1);
  return 0;
}




  static void makestack(String ARR[], int arrlength)
  {
    Stack lifo = new Stack();
    boolean tf = true;
    lifo.push ( new Double(ARR[0]) );
    lifo.push ( new Double(ARR[1]) );
    double first,second,answer = 0;

    for (int i = 2;i<arrlength;i++)
    {
      while(!lifo.empty())
      {
        tf = true;
        try
        {
          Double.parseDouble(ARR[i]);
        }
        catch (NumberFormatException nfe)
        {
          second = (((Double)lifo.pop()).doubleValue());
          first = (((Double)lifo.pop()).doubleValue());
          answer = dosum(first, second, ARR[i]);        
          lifo.push (new Double(answer));
          tf = false;
        }
        if (tf == true)
        {
          System.out.println(ARR[i]);
          lifo.push ( new Double(ARR[i]) );
        }
      }  
    }

    System.out.println(answer);
    return;
  }



  static void check2(String ARR[])
  {
    int arrlength = ARR.length;
    if (arrlength == 2)
    {
      System.out.println("Need Two numbers and Operator");
      return;
    }
    if (arrlength > 2)
    {
      try
      {
        Integer.parseInt(ARR[1]);
      }
        catch (Exception e)
      {
        System.out.println("Need 2 Numbers before operator");
        return;
      }
    }      
    makestack(ARR,arrlength);
  }





  static void check1(String ARR[])
  {
    int arrlength = ARR.length;
    int i = ARR.length;
    if (arrlength == 1)
    {
      try
      {
        Double.parseDouble(ARR[0]);
      }
        catch (Exception e)
      {
        System.out.println("Arguments Can't Start With an Operator");
        return;
      }
      System.out.println (ARR[0]);
      return;
    }
    check2(ARR);
  }
}
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8123567
>>inputs[j]=input[j];


shouldn't that be

>>inputs[j]=input[i];
0
 

Author Comment

by:Gesus
ID: 8123590
no.  its added the jth element from input to the jth element of inputs.


the problem is in makestack.

how do i say

while lifo doesn't have just one thing in it.


instead of
while(!lifo.empty())

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8123616
while(lifo.size()!= 1)

?
0
 

Author Comment

by:Gesus
ID: 8123742
it all works except if i input " " or "" on command line.  how can i get those errors?

i'll check for them in Check1

is it as simple as if (ARR = null)
{
System.out.println("Input is Null");
system.exit(-1);
}
0
 

Author Comment

by:Gesus
ID: 8123781
cheers

*steadily growing to hate java*
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8123817
if(args.length < 1){
 System.out.println("Usage: java Stacks <arguments>");
 System.exit(-1);
}

Stick with it!
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses
Course of the Month12 days, 5 hours left to enroll

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question