command line inputs

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
GesusAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

GesusAuthor Commented:
whoops, it was meant to say java PROGRAMNAME (no space)
0
CEHJCommented:
Are you using 1.4? If so

String[] inputs = line.split();
0
CEHJCommented:
Sorry - misreading the question:


  public static void main(String[] args) {
    String[] inputs = new String[8];
    inputs[0] = args[0];
    // etc
  }
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

girionisCommented:
 ... 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
GesusAuthor Commented:
how does line.split() work?  what variables does it take?  and is the result an array?
0
GesusAuthor Commented:
cheers, i'll have a play around with these methods
0
girionisCommented:
 CEHJ sorry, did not see your answer.

  Actually this: counter<arguments.length);

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

 
0
CEHJCommented:
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
GesusAuthor Commented:
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
girionisCommented:
 boolean number = true;
  try
  {
    int num = Integer.parseInt(arguments[0]);
  }
  catch (NumberormatException nfe)
  {
     number = false;
  }
0
CEHJCommented:
>>catch (NumberormatException nfe)

of course should be

>>catch (NumberFormatException nfe)
0
GesusAuthor Commented:
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
CEHJCommented:
>>i can't say double i = stackname.pop()

but you can say

>>double i = ((Double)stackname.pop()).doubleValue();
0
GesusAuthor Commented:
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
CEHJCommented:
If opp's a String you mean? If so

if (opp.equals("+")) {
  // take action
}
0
GesusAuthor Commented:
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
CEHJCommented:
What version Java you using?
0
CEHJCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
GesusAuthor Commented:
whast the new String[8];  thing?

the command line doesn't neccessarilly have 8 args in
0
CEHJCommented:
Well you'll have to use a collection. You still haven't told me what Java version you're using.
0
GesusAuthor Commented:
sorry, its 1.4

so i do

String[] inputs = line.split();

and then the same as before?
0
GesusAuthor Commented:
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
CEHJCommented:
Yes, or in this case,

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

if your command line arguments are in double quotes
0
GesusAuthor Commented:
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
GesusAuthor Commented:
sorry bout that double post, network is playing up
0
GesusAuthor Commented:
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
CEHJCommented:
public static void main(String args[])
 {
   if (args.length > 0)
   {
     inputs = args[0].split();
   }
   check1(input);
 }
0
CEHJCommented:
public static void main(String args[])
 {
   if (args.length > 0)
   {
     inputs = args[0].split();
     check1(input);
   }
 }
0
GesusAuthor Commented:
it can't resolve symbol split
0
GesusAuthor Commented:

  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
CEHJCommented:
>>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
CEHJCommented:
>>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
GesusAuthor Commented:
i couldn't get it to work.  i'm trying to tokenizer
0
CEHJCommented:
Please show your command line
0
GesusAuthor Commented:
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
CEHJCommented:
>>inputs[j]=input[j];


shouldn't that be

>>inputs[j]=input[i];
0
GesusAuthor Commented:
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
CEHJCommented:
while(lifo.size()!= 1)

?
0
GesusAuthor Commented:
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
GesusAuthor Commented:
cheers

*steadily growing to hate java*
0
CEHJCommented:
if(args.length < 1){
 System.out.println("Usage: java Stacks <arguments>");
 System.exit(-1);
}

Stick with it!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.