# Manipulating a String

Page 1 of 1

## 4 Replies - 387 Views - Last Post: 10 March 2013 - 04:55 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=314894&amp;s=c670df595af6d8cb04d06c6f5b3823d8&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

Reputation: 0
• Posts: 34
• Joined: 28-February 13

# Manipulating a String

Posted 09 March 2013 - 04:12 PM

This is the problem I'm trying to solve :

Use the wrapper classes to create an application that reads a 5 digit number from the user and then reports back the sum of those digits. Next, perform the same operation without using wrapper classes.

I'm still stuck on the first part using wrapper classes, so I decided to try the second part of the problem and work on that part. Well, I got myself stuck on that part too.

So here's my program so far. Everything works except for at the end, when I add the numbers together, the calculations are wrong. I'm assuming that has something to do with they way I pulled the numbers from a string. What am I doing wrong?

```  public static void main(String[] args) {

String num = null;

int num6;

Scanner scan = new Scanner(System.in);

System.out.println("Enter a five digit number: ");
num = scan.next();
//        System.out.println(num);

// This gets the first number in the string

String input = num;
char num1 = input.charAt(0);
System.out.println(num1);

// This gets the second number in the string

String input2 = num;
char num2 = input2.charAt(1);
System.out.println(num2);

// This gets the third number in the string

String input3 = num;
char num3 = input3.charAt(2);
System.out.println(num3);

// This gets the fourth number in the string

String input4 = num;
char num4 = input4.charAt(3);
System.out.println(num4);

// This gets the fifth number in the string

String input5 = num;
char num5 = input5.charAt(4);
System.out.println(num5);

// This adds the five numbers together

num6 = num1 + num2 + num3 + num4 + num5;
System.out.println(num6);

}
}
```

Is This A Good Question/Topic? 0

## Replies To: Manipulating a String

### #2 Ryano121

• D.I.C Lover

Reputation: 1460
• Posts: 3,286
• Joined: 30-January 11

## Re: Manipulating a String

Posted 09 March 2013 - 04:15 PM

You are adding characters together not numbers meaning that you are really just adding together ASCII values. You first need to convert the chars into int's before you add them together.

### #3 Takk

Reputation: 40
• Posts: 111
• Joined: 08-March 13

## Re: Manipulating a String

Posted 09 March 2013 - 04:20 PM

check into the parseInt method

### #4 pbl

• There is nothing you can't do with a JTable

Reputation: 8370
• Posts: 31,956
• Joined: 06-March 08

## Re: Manipulating a String

Posted 09 March 2013 - 05:33 PM

And no need to create String input2, input3, input4...
the different characters are

num.charAt(0), num.chatAt(1), num.charAt(2)...

### #5 g00se

• D.I.C Lover

Reputation: 3225
• Posts: 14,244
• Joined: 20-September 08

## Re: Manipulating a String

Posted 10 March 2013 - 04:55 AM

```total += num.charAt(i) - '0';
```
is what you need (in a loop)