Advertisement
Parasect

uni[correct]

Dec 2nd, 2013
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.62 KB | None | 0 0
  1.  
  2. import javax.swing.JOptionPane;
  3. public class pears {
  4. public static void main(String[] args) {
  5. int total = 0;
  6. String X = JOptionPane.showInputDialog(null, "enter nums please");
  7. int prevIndex = 0;
  8. int index = 0;
  9. while (index > -1) {
  10. index = X.indexOf(' ', prevIndex);
  11. if (index > -1) {
  12. total += Integer.parseInt(X.substring(prevIndex, index));
  13. prevIndex = index + 1;
  14. } else {
  15. total += Integer.parseInt(X.substring(X.lastIndexOf(' ')+1));
  16. break;
  17. }
  18. }
  19. JOptionPane.showMessageDialog(null, "the sum is " + total);
  20. }
  21. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement