5 Replies - 5058 Views - Last Post: 04 November 2009 - 04:19 PM Rate Topic: -----

#1 sethkrepps   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 24-October 09

Solved: Problem with while loop nested in for loop. I think.

Post icon  Posted 03 November 2009 - 07:33 PM

Hello everyone. This is my very FIRST post, well, inquiry rather. I have been long at work at a seemingly simple problem. It will be easier if I just quote the "programming project."

"Design and implement an application that reads a number of integers that are in the range of 0 to 50 inclusive and counts how many times each one is entered. After all input has been processed, print all of the value, with the number of times each was entered."

This is what i have thus far, and being a beginner, i guess (as I have been told) my coding style is a little sloppy. =\.
import java.util.*;


public class zerotofifty {

	public static void main(String[] args){
		int number, temp=0;
		int[][] numbers = { {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
							{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
		boolean valid = false, same = false;

		Scanner input = new Scanner(System.in);
		
		
		System.out.println("Please enter 30 numbers in the range of 0 to 50");
		
		for (int i = 0; i < numbers.length; i ++)
		{	//set valid to false so we can actually get back into the while
			//after the user successfully enters an integer
			valid=false;
			//check to see if number entered is valid, if not, repeat!
			while (!valid)
			{
			
			number = input.nextInt();
						
				if (number < 0 || number >50)
					{valid = false;
					System.out.println("Please enter a number that IS BETWEEN (inclusive) 0 AND 50!!!");}
				else
					{valid = true;
					numbers[0][i] = number;}	
			}
		}
		//once done entering numbers, let's sort it using insertion sort!
		for(int index = 1; index < numbers.length; index++)
		{
			int key = numbers[0][index];
			int position = index;
			//shift larger values to the right
			while(position > 0 && numbers[0][position-1] > key)
			{
				numbers[0][position]= numbers[0][position-1];
				position--;
			}
			numbers[0][position] = key;			
		}
		
		
		for (int i = 0; i < numbers.length-1; i++)
			{
				for (int x = 1; x < numbers.length; x++)
				{
					//add for an occurrence of the new number, then goto the 'same loop' to see if there are any others
						temp++;
						same = true;
					//check for more equal integers
					while (same)
					{
						if (numbers[0][i] == numbers[0][x])
							temp++;
					//if not equal to each other, set the field below it equal to the number of occurrences in the entire array 
						else
							{numbers[1][i] = temp;
							temp = 0;
							i=x;}
					}
				}
					
			//once done counting, lets print out the info.
				for (int i1 = 0; i1 <numbers.length; i1++)
				{	if(numbers[1][i1] != 0)
					System.out.println(numbers[0][i1]+": "+numbers[1][i1]+" times.");
				
				}
			
		}
		
	}
}




I've stepped through this with the debugger in Eclipse countless times. It seems that when i enter an integer out of the range after entering an integer within the range, and then i proceed to enter an integer within the range, it just hangs on
 number = input.nextInt(); 

any ideas? Maybe its going into some hidden ultra-crazy infinite loop?

It's been an hour or so, or, wow, a lot longer, I've done a lot of thinking. Maybe it is going into an infinite loop somewhere in the Scanner class? plausible? ...pshhhh naaaah :blush:

This post has been edited by sethkrepps: 04 November 2009 - 04:57 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Solved: Problem with while loop nested in for loop. I think.

#2 Momerath   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1021
  • View blog
  • Posts: 2,463
  • Joined: 04-October 09

Re: Solved: Problem with while loop nested in for loop. I think.

Posted 03 November 2009 - 09:59 PM

View Postsethkrepps, on 3 Nov, 2009 - 06:33 PM, said:

Maybe it is going into an infinite loop somewhere in the Scanner class? plausible?
I don't know what the problem here is, but I had to comment on this statement. The idea that an infinite loop in a library class that has gone undiscovered until someone who is new a programming trys to use it is the very last thing I would think :)
Was This Post Helpful? 0
  • +
  • -

#3 Fuzzyness   User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: Solved: Problem with while loop nested in for loop. I think.

Posted 03 November 2009 - 10:23 PM

same = true;
					//check for more equal integers
					while (same)
					{
						if (numbers[0][i] == numbers[0][x])
							temp++;
					//if not equal to each other, set the field below it equal to the number of occurrences in the entire array 
						else
							{numbers[1][i] = temp;
							temp = 0;
							i=x;}
					}

You never change same back to false anywhere in the while loop, thus a infinite loop.
Was This Post Helpful? 0
  • +
  • -

#4 sethkrepps   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 24-October 09

Re: Solved: Problem with while loop nested in for loop. I think.

Posted 03 November 2009 - 10:42 PM

@momer...something(REALLY bad memory, i swear i looked at the handle like 15 times hahah)
That was the last thing I thought. And I don't believe it to be true =P

@Fuzz...
Well, you saved me from a future error, so thanks! BUT, if i enter a number in the range, then out of the range, then in the range, it still hangs in limbo, more ideas?
Was This Post Helpful? 0
  • +
  • -

#5 Fuzzyness   User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: Solved: Problem with while loop nested in for loop. I think.

Posted 03 November 2009 - 10:53 PM

for (int i = 0; i < numbers.length; i ++)
		{	//set valid to false so we can actually get back into the while
			//after the user successfully enters an integer
			valid=false;
			//check to see if number entered is valid, if not, repeat!
			while (!valid)
			{
			
			number = input.nextInt();
						
				if (number < 0 || number >50)
					{valid = false;
					System.out.println("Please enter a number that IS BETWEEN (inclusive) 0 AND 50!!!");}
				else
					{valid = true;
					numbers[0][i] = number;}	
			}
		}


You only propt again for another number if they ask out of the range. so if they put it in the range then it wont prmopt again and will get hung up. SO just prompt them again even if it is in the range. the first prompt is outside of the for loop. So inside the for loop you read it in, but nothing was prompted.

Follow?
Was This Post Helpful? 0
  • +
  • -

#6 sethkrepps   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 24-October 09

Re: Solved: Problem with while loop nested in for loop. I think.

Posted 04 November 2009 - 04:19 PM

@Fuzz.
If the number they entered is in range, it sets valid to true, which takes it out of the while loop... but since it is still in the for loop, it goes through for another iteration. and since it sets valid to false it hops in the while loop, requests a number, via input.nextInt(), then checks for validity, and keeps going through the process, but i found my problem!

I declare an array that is 2x30, so when i say
 numbers.length(); 
it returns two instead of thirty. So what i did to fix it was change all the numbers.length() to
 numbers[0].length() 
and it worked! sooo...
MORAL OF THE STORY: always make sure you are checking the right dimension for length in an array! :D

here's my code for the completed program.
import java.util.*;


public class zerotofifty {

	public static void main(String[] args){
		int number, temp=0;
		int[][] numbers = { {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
							{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
		boolean valid, same = false;

		Scanner input = new Scanner(System.in);
		
		
		System.out.println("Please enter 30 numbers in the range of 0 to 50");
	
		for (int i = 0; i < numbers[0].length; i ++)
		{	System.out.println(i+" numbers left");
			//set valid to false so we can actually get back into the while
			//after the user successfully enters an integer
			valid=false;
			//check to see if number entered is valid, if not, repeat!
			while (!valid)
			{
			
			number = input.nextInt();
						
				if (number < 0 || number >50)
					{valid = false;
					System.out.println("Please enter a number that IS BETWEEN (inclusive) 0 AND 50!!!");}
				else
					{valid = true;
					numbers[0][i] = number;}	
			}
		}
		//once done entering numbers, let's sort it using insertion sort!
		for(int index = 1; index < numbers[0].length; index++)
		{
			int key = numbers[0][index];
			int position = index;
			//shift larger values to the right
			while(position > 0 && numbers[0][position-1] > key)
			{
				numbers[0][position]= numbers[0][position-1];
				position--;
			}
			numbers[0][position] = key;			
		}
		
		
		for (int i = 0; i < numbers[0].length-1; i++)
			{
				for (int x = 1; x < numbers[0].length; x++)
				{
					//add for an occurrence of the new number, then goto the 'same loop' to see if there are any others
						temp++;
						same = true;
					//check for more equal integers
					while (same)
					{
						if (numbers[0][i] == numbers[0][x])
							{temp++;
							if (x + 1 < numbers[0].length)
								{x++;}
							else 
							{break;}}
					//if not equal to each other, set the field below it equal to the number of occurrences in the entire array 
						else
							
							{numbers[1][i] = temp;
							temp = 0;
							i=x;
							same = false;}
					}
				}
					
			
		}
		//once done counting, lets print out the info.
		for (int i1 = 0; i1 <numbers[1].length; i1++)
		{	if(numbers[1][i1] != 0)
			System.out.println(numbers[0][i1]+": "+numbers[1][i1]+" times.");
		
		}
	
		
	}
}




so... RESOLVED!!! ^_^

Thanks for your help guys!

This post has been edited by sethkrepps: 04 November 2009 - 04:21 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1