Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

Write a Java method which accepts two string arguments as parameters

  Asked By: Bailey    Date: Nov 15    Category: Java    Views: 3245
  

Can anyone help with these questions?

Thanks....

1. Write a Java method which accepts two string arguments as
parameters and does the following.

a. Test is both strings contain only digits.

b. Converts each string value to an integer and returns the sum
of the two integers.

(Hint: use str.charAt(p) which returns the character at position p
in the string variable str)

2. Write a java method called `StringLength' which accepts two
string arguments as parameters and returns the following:

a. 0: If both strings are the same length.

b. 1: If the first string is longer than the second string

c. 2: If the second string is longer than the first string



3. Write a java method called `StringCompare' which accepts two
string arguments as parameters and returns the following:

a. 0: If both strings are the same

b. 1: If the first string is greater than the second string

c. 2: If the second string is greater than the first string

Share: 

 

1 Answer Found

 
Answer #1    Answered By: Hilma Miller     Answered On: Nov 15

This may help  u.

public class TestJava1{
public static void main(String args[]){
if(args.length < 2 ){
System.out.println("Java Usage: Please
provide 2 arguments.");
System.exit(0);
}
isContainsOnlyDigits(args[0], args[1]);
int sum = convertInteger(args[0], args[1]);
if(sum != -1)
System.out.println("The Sum of Two integer
is "+sum);

int lenValue = stringLength(args[0], args[1]);
System.out.println("The length  of the strings  got
from method  is "+lenValue);

int value = stringCompare(args[0], args[1]);
System.out.println("The return value got from method
is "+value);
}

private static void isContainsOnlyDigits(String str1, String
str2) {

/* Using charAt method
String strArr[] = {str1, str2};
boolean isDigit;
for(int i = 0; i < 2; i++){
isDigit = true;
for(int j = 0; j < strArr[i].length(); j++ ){
char a = strArr[i].charAt(j);
Character ch = new Character(a);
if(!ch.isDigit(a)){
isDigit = false;
System.out.println("The
string '"+strArr[i]+"' contains other characters.");
break;

}
else continue;
//System.out.print(" Break is
called ");
}
if(isDigit)
System.out.println("The
string '"+strArr[i]+"' contains only digits.");


}
*/

// direct method.
try{
Integer.parseInt(str1);
System.out.println("The string  '"+str1+"'
contains only digits.");

}catch(Exception e){
System.out.println("The string '"+str1+"'
contains other characters.");

}

try{
Integer.parseInt(str2);
System.out.println("The string '"+str2+"'
contains only digits.");

}catch(Exception e){
System.out.println("The string '"+str2+"'
contains other characters.");

}

}

public static int convertInteger(String str1, String str2) {
try{
int int1 = Integer.parseInt(str1);
int int2 = Integer.parseInt(str2);

return int1+int2;
}catch(Exception e){
System.out.println("Sum of two integers
cannot be found, The entered string is not integer");
return -1;
}
}

public static int stringLength(String str1, String str2){
//System.out.println("str1.compareTo(str2)
=>"+str1.compareTo(str2));
int val = str1.compareTo(str2);
int len1 = str1.length();
int len2 = str2.length();
if(len1 == len2 ) return 0;
if(len1 > len2 ) return 1;
if(len1 < len2 ) return 2;
return -1;
}

public static int stringCompare(String str1, String str2){
System.out.println("str1.compareTo(str2)
=>"+str1.compareTo(str2));
int val = str1.compareTo(str2);
if(val > 0) val = 1;
if (val < 0) val = 2;
return val;
//if(str1.compareTo(str2)){

}

}

 




Tagged: