0

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

5
Contributors
10
Replies
12
Views
6 Years
Discussion Span
Last Post by kdmuk10
0

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)) );
0

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.

0

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);
  
  }
}
0

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

0

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

0

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.

0

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)

0

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 ;)

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.