Hi ,
I want to get a String from command line argument and add the values.

class Sample
{
public static void main(String args[])throws IOException
  {
  String Given_Id=args[0];//Getting input
  int Add=0;;
  int Store=0;
  System.out.println("The length of the String is "+Given_Id.length());//Calculating length
  for(int i=1;i<Given_Id.length();i++)
  {
  Add=Given_Id.charAt(i);
 Store=Store+Add;//Adding values 
 }
System.out.println("The total is "+Store);
  
  }
}

But it is displaying wrong output.
Given Value :p1234
Omitting P character in for loop and adding the balance values.
But not getting the output it displays some junk characters.

Expected Output:
10

Occured Output:
Junk characters

can any one help me out .

Thank you,
With Regards.
Prem

Maybe you should convert the char that you get to an int.
Convert this: '2' to the int 2.
Instead when you do this:

int i = '1';

You are not setting the int 1 but the ASCII value of the character '1' which is something else.

Try something like this:

Add = Integer.parseInt( String.valueOf(Given_Id.charAt(i)) );

The value of a char digit is NOT equal to its decimal value representation.
For example the digit: '0' has an int value of 48 base 10 or 0x30

ASCII characters representing ints need to be converted to int values by using the Integer.parseInt() method.

Take a look at this code, it does what you want and try to understand it and ask where you don't understand.

class Sample
{
public static void main(String args[])
  {
  int Add=0;;
  int Store=0;
  char[] myStringArray = args[0].toCharArray();
  System.out.println("The length of the String is "+myStringArray.length);//Calculating length
  
  for(char c: myStringArray)
  {
	  Add = Add+Character.getNumericValue(c);
	  //System.out.println("Add is now "+Add);
 }
   System.out.println("The total is "+Add);
  
  }
}

One caution about using: Character.getNumericValue()
It returns positive values for lots of non-numeric characters, if that's any concern.

Thanks to all of you.I have done it by using your code samples.

evstevemd:

when i used your code, it brings arrayoutofbounds exeption at

char[] myStringArray = args[0].toCharArray();

i had the same problem with a similar code. i apprecaite to get your advice on how to handle this.

If your problem is solved mark it so. If you have another one start new thread

char[] myStringArray = args[0].toCharArray();

You need to test if the args array is empty first. There is not guarantee that the user passed any args to the program (assuming that this is the String[] args passed to main)

You need to test if the args array is empty first. There is not guarantee that the user passed any args to the program (assuming that this is the String[] args passed to main)

yes, that is useful footnote ;)

thank you guys

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.