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: Stumped by while loop results

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

    Default Stumped by while loop results

    My program compiles and runs fine:

    import java.util.*;
     
    public class Chapter5Ex1
    {
    	static Scanner console = new Scanner(System.in);
     
    	public static void main(String[] args)
    	{
    		String input = "";
    		int sum = 0, length, add = 1, counter = 0, counter2 = 0;
    		long number;
    		boolean found = false;
     
     
    			counter = 0;
    			System.out.println("Enter an integer: ");
    			System.out.println();
     
    			input = console.next();
    			number = Math.abs(Long.parseLong(input));
    			input = Long.toString(number);
     
    			length = input.length();
     
    			//System.out.println("the length is: " + length);
     
    			System.out.print(input.substring(0, 1));
     
    			while(counter < length - 1)
    			{
    				System.out.print(" " + input.substring(add, add + 1));
    				add++;
    				counter++;
    			}
    			while(counter2 < length)
    			{
    				sum += number % 10;
    				number /= 10;
    				counter2++;
    			}
    			System.out.print(" " + "The sum is: " + sum + "\n" + "\n");
     
    	}
    }

    but I want it to prompt the user again for another integer after the program executes. I have tried a few different loops, sentinel, flagged,
    but the second time it runs it gives me out of bonds exception for the counter variable. I have even tried initializing the counter to zero just outside the while(counter < length - 1) loop but I always get that out of bounds exception. Here is an example of the flag controlled loop I tried.

    import java.util.*;
     
    public class Chapter5Ex1
    {
    	static Scanner console = new Scanner(System.in);
     
    	public static void main(String[] args)
    	{
    		String input = "";
    		int sum = 0, length, add = 1, counter = 0, counter2 = 0;
    		long number;
    		boolean found = false;
     
    			while(!found)
    			{
    			counter = 0;
    			System.out.println("Enter an integer: ");
    			System.out.println();
     
    			input = console.next();
    			number = Math.abs(Long.parseLong(input));
    			input = Long.toString(number);
     
    			length = input.length();
     
    			//System.out.println("the length is: " + length);
     
    			System.out.print(input.substring(0, 1));
     
    			while(counter < length - 1)
    			{
    				System.out.print(" " + input.substring(add, add + 1));
    				add++;
    				counter++;
    			}
    			while(counter2 < length)
    			{
    				sum += number % 10;
    				number /= 10;
    				counter2++;
    			}
    			System.out.print(" " + "The sum is: " + sum + "\n" + "\n");
    	}
    	}
    }


  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: Stumped by while loop results

    Where exactly is your issue arising?
    Last edited by Staticity; October 2nd, 2011 at 10:07 PM.
    Simplicity calls for Complexity. Think about it.

  3. #3
    Junior Member
    Join Date
    Sep 2011
    Location
    philadelphia
    Posts
    8
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Stumped by while loop results

    You need to reset your "add" to 0 for each loop, try this:

    import java.util.*;

    public class Chapter5Ex1
    {
    static Scanner console = new Scanner(System.in);

    public static void main(String[] args)
    {
    String input = "";
    int sum = 0, length, add = 0, counter = 0, counter2 = 0;
    long number;
    boolean found = false;

    while(!found)
    {
    counter = 0;
    add=0;
    System.out.println("Enter an integer: ");
    System.out.println();

    input = console.next();
    number = Math.abs(Long.parseLong(input));
    input = Long.toString(number);

    length = input.length();

    //System.out.println("the length is: " + length);

    System.out.print(input.substring(0, 1));


    while(counter < length - 1)
    {
    System.out.print(" shang:" + input.substring(add, add + 1));
    add++;
    counter++;
    }
    while(counter2 < length)
    {
    sum += number % 10;
    number /= 10;
    counter2++;
    }
    System.out.print(" " + "The sum is: " + sum + "\n" + "\n");
    }
    }
    }
    Acoolme is an Online Marketing Software Platform And Social Community

  4. #4
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Stumped by while loop results

    seohulu, better close your code inside the tags.
    And use proper formatting. Use any editor for that.
    Thanks.

  5. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,296
    Thanks
    180
    Thanked 824 Times in 767 Posts
    Blog Entries
    5

    Default Re: Stumped by while loop results

    Quote Originally Posted by Mr.777 View Post
    seohulu, better close your code inside the tags.
    And use proper formatting. Use any editor for that.
    Thanks.
    And seohulu, please read the forum rules and the following link:
    http://www.javaprogrammingforums.com...n-feeding.html

Similar Threads

  1. Logic issue(I believe) stumped
    By mwr76 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: September 23rd, 2011, 01:19 AM
  2. Array question. Stumped!
    By tjanuranus in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 5th, 2011, 02:35 PM
  3. help im stumped :(
    By gonfreecks in forum What's Wrong With My Code?
    Replies: 6
    Last Post: November 9th, 2010, 11:51 AM
  4. Help so stumped....
    By Macgrubber in forum Loops & Control Statements
    Replies: 8
    Last Post: October 28th, 2010, 03:53 PM
  5. Stumped?
    By KevinGreen in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 3rd, 2009, 01:02 AM