Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm having trouble with the following code. This program's function is to determine the class (class as in how precious it is) of a diamond depending on its weight. So far I have got it working in a way that it keeps allowing me to enter the weight of the diamonds. Because there are going to be numerous diamonds, but no set amount I have used arrays. The program should exit when the user enters -999. The array has a total of 10 slots, but I'm only looking to test it for each class of diamond "A-F" so after 6 I want to enter -999 to exit the program.

Java
import java.util.*;
public class forDiamond {
	
	public static void main(String[] args)
	{
	
		//Declaring variables and arrays.
		
		int[] weight;;
		char[] diamondClass;
		int[] diamond;
		weight = new int[10];
		diamond = new int [10];
		diamondClass = new char [10];
		Scanner in = new Scanner(System.in);
		
		//Getting the dweight to determine weather the while loop is true or false
		System.out.println("Please enter the weight of the Diamond: ");
		int dweight = in.nextInt();
		while (dweight != -999)
		{
			for(int i=0; i<10; i++)
			{
				weight[i] = dweight;
				diamond[i] = i+1;
				//finding out the diamond class
				if (weight[i] <=15)
					diamondClass[i] = 'F';
				
				else if (weight[i] > 15 && weight[i]<=40)
					diamondClass[i] = 'E';
				
				else if (weight[i] > 40 && weight[i]<=60)
					diamondClass[i] = 'D';
				
				else if (weight[i] > 60 && weight[i]<=80)
					diamondClass[i] = 'C';
				
				else if (weight[i] > 80 && weight[i]<=100)
					diamondClass[i] = 'B';
				
				else if (weight[i] > 100)
					diamondClass[i] = 'A';
				
				System.out.println("Please enter the weight of the Diamond: ");
				dweight = in.nextInt();
			}
			

		}
		//Printing out all the results
		for(int i=0; i>10; i++) 
		System.out.println("The weight of " + diamond[i] + " is: " + weight[i] + "the class of the diamond is :" + diamondClass[i]);

	}

}
Posted
Updated 7-Feb-12 0:25am
v2
Comments
merridus 7-Feb-12 6:22am    
What is your problem? Does it not exit when you enter -999?
big nasty 7-Feb-12 6:54am    
yeah thats the problem im having i will try the solutions below thanks for your help. not too familiar with array lists so will have to check up on them before attempting that.

C#
System.out.println("Please enter the weight of the Diamond: ");
dweight = in.nextInt();


This needs to be the last thing you do in the while loop, outside of the for loop. Currently if you enter -999 before the for loop has done 10 iterations it won't exit the application as the while condition is not checked again until the inner loop is complete.

Maybe you could use ArrayLists rather than Arrays and add the values to the end of the ArrayList each iteration in the while loop. Then you don't need to define the size ahead of time and you can do as many iterations as the user enters values.

Hope this helps.
 
Share this answer
 
Comments
big nasty 7-Feb-12 7:19am    
//Printing out all the results
for(int i=0; i>10; i++)
System.out.println("The weight of " + diamond[i] + " is: " + weight[i] + "the class of the diamond is :" + diamondClass[i]);

thanks so i made the change you state and now it exits whenever i type in -999. i have then tried to print the output by putting the above code outside the while loop but no joy and advice.
merridus 7-Feb-12 7:22am    
Does it just not output anything or does it give you an error?
big nasty 7-Feb-12 9:00am    
sorry merridus was away there. thats right it allows me to perform the input but it doesnt perform the output shown. it does when its inside the for loop but it prints the same thing ten times then bigins the while loop over again which is obvious when you have it in the for loop. but when i put it at the end or outside the while loop it does nothing!
merridus 7-Feb-12 9:08am    
You have the sign the wrong way round in the code you pasted above for printing all results. That's why it isn't outputting anything at the end upon exiting the loop. "i<10"
big nasty 7-Feb-12 9:17am    
lol that would do it all right. the long ive spent lookin at that an couldn find it. typical takes some one else to look at ur code de spot sohin so basic.
i should be able to deal with the rest
Cheers
If I got you then changing from
Java
for(int i=0; i<10; i++)

to
Java
for(int i=0; i<10 && dweight != -999; i++)

should be enough (this way, however the while loop is not needed).
 
Share this answer
 
Comments
big nasty 7-Feb-12 7:07am    
thanks for the solution, im kind of trying to use the while loop so i can familiarise myself with it. your help is appreciated though

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900