Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Help with while loop

  1. #1
    Member
    Join Date
    Aug 2011
    Posts
    55
    Thanks
    5
    Thanked 3 Times in 3 Posts

    Default Help with while loop

    I am trying to write a program that takes an int as input. Then it prints out the number with spaces and the sum. Example:

    input: 123
    output: 1 2 3 The sum is: 6


    here is the output I am getting: 1 The sum is: 6

    here is my code
    import java.util.*;
     
    public class ch5PrEx1
    {
    	static Scanner console = new Scanner(System.in);
     
    	public static void main(String[] args)
    	{
    	  String input;
    	  int sum = 0, numericInput, length;
     
    	  System.out.println("Please enter an integer: ");
    	  System.out.println();
     
    	  input = console.next();
    	  numericInput = Integer.parseInt(input);
    	  numericInput = Math.abs(numericInput);
    	  input = Integer.toString(numericInput);
     
    	  length = input.length();
     
     
    	  System.out.println(input.charAt(length-length) + " " + Addition(length, input) + " " + "The sum is: " + Sum(sum, length, numericInput));
     
     
     
    	}
     
    	public static int Sum(int sum, int length, int numericInput)
    	{
    		int counter = 0;
    		while(counter < length)
    		{
     
    			sum += numericInput % 10;
    			numericInput /= 10;
    			counter = counter + 1;
    		}
     
    		return sum;	
    	}
     
    	public static String Addition(int length, String input)
    	{
    		String output = " ", index; 
    		int add = 0, counter = 0;
     
     
     
    		while(counter < length)
    		{
    			index = input.substring(add + 1, add + 1);
    			output = String.format("%s", index);
    			add = add + 1;
    			counter++;
     
     
    		}
     
    		return output;
     
     
     
    	}
     
    }

    Thanks in advance!


  2. #2
    Member Staticity's Avatar
    Join Date
    Jul 2011
    Location
    Texas
    Posts
    105
    My Mood
    Inspired
    Thanks
    3
    Thanked 5 Times in 5 Posts

    Default Re: Help with while loop

    Quote Originally Posted by mwr76 View Post
    System.out.println(input.charAt(length-length) + " " + Addition(length, input) + " " + "The sum is: " + Sum(sum, length, numericInput));
    IF you look at the first thing you put in this Print statement:
    "input.charAt(length-length)" -> no matter the value of length, the value of "length-length" will be 0.

    Therefore, you will print out the String's Index of 0, which will always be the first number. In order to print out all of the values, you will need a method to do so...
    Simplicity calls for Complexity. Think about it.

  3. #3
    Member
    Join Date
    Aug 2011
    Posts
    55
    Thanks
    5
    Thanked 3 Times in 3 Posts

    Default Re: Help with while loop

    I do have a method to do that Addition(). I want it to print the index at 0 first then print the rest of the indexes. Which the Addition methods does, except it only returns the last value.

  4. #4
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: Help with while loop

    Addition() (that's a bad name for a Java method - follow the convention in the API - you should be using a lowerCaseFirstLetter()) uses String.subtring(int, int) where both ints are the same. Read the API doc for that method - particularly the bit around
    Thus the length of the substring

  5. #5
    Member Staticity's Avatar
    Join Date
    Jul 2011
    Location
    Texas
    Posts
    105
    My Mood
    Inspired
    Thanks
    3
    Thanked 5 Times in 5 Posts

    Default Re: Help with while loop

    Here's a tip:

    Your output is this (_ is the equivalent to a space):

    1__The_sum_is:_6

    And inside your println parameters you have..

    input.charAt(length-length) + " " + Addition(length, input) + " " + "The sum is: " + Sum(sum, length, numericInput)

    Therefore..

    "1_" = input.charAt(length-length) + " "
    "_" = Addition(length, input)
    "The_sum_is:_6" = "The sum is: " + Sum(sum, length, numericInput)

    Try to locate the source of your error... and after all of this is done, (if you'd like) I can show you my approach to this program.
    Simplicity calls for Complexity. Think about it.

Similar Threads

  1. For Loop Help
    By nekromanik in forum Loops & Control Statements
    Replies: 2
    Last Post: March 17th, 2011, 10:36 PM
  2. [SOLVED] My while loop has run into an infinite loop...?
    By kari4848 in forum Loops & Control Statements
    Replies: 3
    Last Post: March 1st, 2011, 12:05 PM
  3. for loop and while loop problems
    By Pulse_Irl in forum Loops & Control Statements
    Replies: 4
    Last Post: May 3rd, 2010, 02:09 AM
  4. hi. i want to rewrite this do loop into a while loop.
    By etidd in forum Loops & Control Statements
    Replies: 3
    Last Post: January 26th, 2010, 05:27 PM
  5. Need help with loop
    By SwEeTAcTioN in forum Loops & Control Statements
    Replies: 8
    Last Post: October 25th, 2009, 05:59 PM