4 Replies - 294 Views - Last Post: 10 March 2013 - 04:55 AM Rate Topic: -----

#1 louisianapadawan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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. :angry2:

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  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1363
  • View blog
  • Posts: 3,002
  • 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.
Was This Post Helpful? 1
  • +
  • -

#3 Takk  Icon User is offline

  • D.I.C Head

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

Re: Manipulating a String

Posted 09 March 2013 - 04:20 PM

check into the parseInt method ;)
Was This Post Helpful? 1
  • +
  • -

#4 pbl  Icon User is offline

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

Reputation: 8346
  • View blog
  • Posts: 31,910
  • 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)...
Was This Post Helpful? 2
  • +
  • -

#5 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2831
  • View blog
  • Posts: 11,993
  • 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)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1