Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.JOptionPane;
- public class pears {
- public static void main(String[] args) {
- int total = 0;
- String X = JOptionPane.showInputDialog(null, "enter nums please");
- int prevIndex = 0;
- int index = 0;
- while (index > -1) {
- index = X.indexOf(' ', prevIndex);
- if (index > -1) {
- total += Integer.parseInt(X.substring(prevIndex, index));
- prevIndex = index + 1;
- } else {
- total += Integer.parseInt(X.substring(X.lastIndexOf(' ')+1));
- break;
- }
- }
- JOptionPane.showMessageDialog(null, "the sum is " + total);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement