Thursday, May 25, 2006

Programming problem

1. String score = "2,2,2,3";
Write a program to add the numbers and print the sum.

import java.io.*;

public class Splits{
public static void main(String[] args) {

String score = "2,2,2,3";
PrintWriter out = new PrintWriter(System.out, true);
String sc[] = score.split(",");

int in = 0;

for(int i=0;i
out.println(sc[i]);
in = in + Integer.parseInt(sc[i]);
}
out.println("Total: " + in);
}
}

2. Write a program to reverse the following string.
String s = "abc xyz";

StringBuffer sb = new StringBuffer(s);
System.out.println(sb.reverse());

0 Comments:

Post a Comment

<< Home