SIMPLE PROBLEM WITH LOOP. NEED HELP!

I need help with this assignment. I've done 90% of it but the loop

Page 1 of 1

7 Replies - 1380 Views - Last Post: 26 April 2007 - 06:57 AM Rate Topic: -----

#1 blueRonK  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 25-April 07

SIMPLE PROBLEM WITH LOOP. NEED HELP!

Post icon  Posted 25 April 2007 - 06:28 AM

/* PROGRAM NAME: CONVERT_METRES
 * THIS PROGRAM IS INTENDED TO CONVERT DISTANCES
 * STUDENT NAME:A.K.
 * DATE: 23.04.07
*/

import javax.swing.JOptionPane;
import java.text.DecimalFormat;

public class ConvertMetres
{

   public static void main (String [] args)
	{
				//variable declarations
			String d1, n2;
			int index=0, index1=0, conversions=0;
			
			double [] numbers = new double [10];//array to store distance


	boolean invalidNumber = true;
			
							//exception handling 
	   
	 while (invalidNumber)
		{
		   n2= JOptionPane.showInputDialog ("How many numbers do you want to convert?");//prompt for number of distances to convert

		 try
			{

			  conversions = Integer.parseInt (n2);
			  invalidNumber =false;
			 }
		  catch (NumberFormatException e)
			 {

			  JOptionPane.showMessageDialog(null, "You must enter an integer");//error message for try/catch
			 }
			}

	 for (int count=1; count <= conversions; count++)//loop for distance input
	   {
	  do
		{
							   //exception handling
		   boolean invalidNumber2= true;//boolean declaration for try and catch
	 while (invalidNumber2) 
		  {
			 d1 = JOptionPane.showInputDialog ( " Enter a distance " + count + " in metres");//prompt for distance to convert
			try
			   {
				 numbers [index]  = Double.parseDouble (d1);
				  invalidNumber2= false;
				}

			 catch (NumberFormatException f)
				{
				   JOptionPane.showMessageDialog(null, "Invalid entry");//error message for try/catch
				 }
			}

		   }
		   
	  while (numbers [index]  < 1);
	}
	int count2=0;
	count2++;
	do 
	
	{	 
		
		   
		   //index ++;

		   String choice1 = JOptionPane.showInputDialog ( "Metres to convert: " + numbers [index] 
			+ "\n 1: Convert to Kilometres \n 2: Convert to inches \n 3: Convert to feet"
			+ "\n 4: Quit the conversion \n  \n Enter your choice " );// choice dialog box
				
			int choice = Integer.parseInt(choice1); 
				
							//if statements for choice
		  if (choice==1)

			  showKilometres (numbers [index]);
				
		 else if (choice==2)
				
			  showInch (numbers [index]);
					  
		 else if (choice==3)

			  showFeet (numbers [index]);

		 else if (choice==4)

			   System.exit (0);

		 else{

				JOptionPane.showMessageDialog(null, "Invalid entry");//error message for invalid choice entry
				}
			}
	  
		
		while (count2 < numbers.length);
	}
	
	   
public static void showKilometres (double dist)
{
	 double kilo = dist *  0.001;
	 DecimalFormat num = new DecimalFormat ("0.00");//definition of display format

	 JOptionPane.showMessageDialog(null, +dist+   "metres = " + num.format (kilo) 
	 + " kilometres");
	}

	public static void showInch  ( double dist) //method to convert to inches

	{
	  double inch = dist *  39.37;
	  DecimalFormat num = new DecimalFormat ("0.00");//definition of display format 

	 JOptionPane.showMessageDialog(null, +dist+   "metres = " + num.format(inch) 
	 + " inches");//display of inches

	}


	public static void showFeet  ( double dist) //method to convert to feet

	{

	  double feet = dist *  3.281;
	  DecimalFormat num = new DecimalFormat ("0.00");//definition of display format

	  JOptionPane.showMessageDialog(null, + dist +   "metres = " + num.format(feet) 
	  + " feet");//display of feet

   }
}


~Admin Edit: Added [code] code here [/code ] tags.

Attached File(s)



Is This A Good Question/Topic? 0
  • +

Replies To: SIMPLE PROBLEM WITH LOOP. NEED HELP!

#2 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: SIMPLE PROBLEM WITH LOOP. NEED HELP!

Posted 25 April 2007 - 06:40 AM

Can you describe the problem, along with any error messages you are receiving?
Was This Post Helpful? 0
  • +
  • -

#3 AngeluS  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 1
  • View blog
  • Posts: 231
  • Joined: 11-April 07

Re: SIMPLE PROBLEM WITH LOOP. NEED HELP!

Posted 25 April 2007 - 06:56 AM

And place your code in between code tags.
At least that makes it a bit easier to read without having to copy/paste it to an IDE.
Was This Post Helpful? 0
  • +
  • -

#4 blueRonK  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 25-April 07

Re: SIMPLE PROBLEM WITH LOOP. NEED HELP!

Posted 25 April 2007 - 07:31 AM

View PostAmadeus, on 25 Apr, 2007 - 06:40 AM, said:

Can you describe the problem, along with any error messages you are receiving?


It has no error messages/ no syntax errors. Just that the program converts the first value as expected and loops but then converts the FIRST value AGAIN intead of moving down in the array. Hope that's descriptive enough. Please check out the attachment for details. I'm a new member.

thanks again.
Was This Post Helpful? 0
  • +
  • -

#5 blueRonK  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 25-April 07

Re: SIMPLE PROBLEM WITH LOOP. NEED HELP!

Posted 25 April 2007 - 07:50 AM

View PostAmadeus, on 25 Apr, 2007 - 06:40 AM, said:

Can you describe the problem, along with any error messages you are receiving?


 


/* PROGRAM NAME: CONVERT_METRES
 * THIS PROGRAM IS INTENDED TO CONVERT DISTANCES
 * STUDENT NAME: KAMAKIL ARON KEMBOI
 * DATE: 23.04.07
*/

import javax.swing.JOptionPane;
import java.text.DecimalFormat;

public class ConvertMetres
{

   public static void main (String [] args)
	{
				//variable declarations
			String d1, n2;
			int index=0, index1=0, conversions=0;
			
			double [] numbers = new double [10];//array to store distance


	boolean invalidNumber = true;
			
							//exception handling 
	   
	 while (invalidNumber)
		{
		   n2= JOptionPane.showInputDialog ("How many numbers do you want to convert?");//prompt for number of distances to convert

		 try
			{

			  conversions = Integer.parseInt (n2);
			  invalidNumber =false;
			 }
		  catch (NumberFormatException e)
			 {

			  JOptionPane.showMessageDialog(null, "You must enter an integer");//error message for try/catch
			 }
			}

	 for (int count=1; count <= conversions; count++)//loop for distance input
	   {
	  do
		{
							   //exception handling
		   boolean invalidNumber2= true;//boolean declaration for try and catch
	 while (invalidNumber2) 
		  {
			 d1 = JOptionPane.showInputDialog ( " Enter a distance " + count + " in metres");//prompt for distance to convert
			try
			   {
				 numbers [index]  = Double.parseDouble (d1);
				  invalidNumber2= false;
				}

			 catch (NumberFormatException f)
				{
				   JOptionPane.showMessageDialog(null, "Invalid entry");//error message for try/catch
				 }
			}

		   }
		   
	  while (numbers [index]  < 1);
	}
	int count2=0;
	count2++;
	do 
	
	{	 
		
		   
		   //index ++;

		   String choice1 = JOptionPane.showInputDialog ( "Metres to convert: " + numbers [index] 
			+ "\n 1: Convert to Kilometres \n 2: Convert to inches \n 3: Convert to feet"
			+ "\n 4: Quit the conversion \n  \n Enter your choice " );// choice dialog box
				
			int choice = Integer.parseInt(choice1); 
				
							//if statements for choice
		  if (choice==1)

			  showKilometres (numbers [index]);
				
		 else if (choice==2)
				
			  showInch (numbers [index]);
					  
		 else if (choice==3)

			  showFeet (numbers [index]);

		 else if (choice==4)

			   System.exit (0);

		 else{

				JOptionPane.showMessageDialog(null, "Invalid entry");//error message for invalid choice entry
				}
			}
	  
		
		while (count2 < numbers.length);
	}
	
	   
public static void showKilometres (double dist)
{
	 double kilo = dist *  0.001;
	 DecimalFormat num = new DecimalFormat ("0.00");//definition of display format

	 JOptionPane.showMessageDialog(null, +dist+   "metres = " + num.format (kilo) 
	 + " kilometres");
	}

	public static void showInch  ( double dist) //method to convert to inches

	{
	  double inch = dist *  39.37;
	  DecimalFormat num = new DecimalFormat ("0.00");//definition of display format 

	 JOptionPane.showMessageDialog(null, +dist+   "metres = " + num.format(inch) 
	 + " inches");//display of inches

	}


	public static void showFeet  ( double dist) //method to convert to feet

	{

	  double feet = dist *  3.281;
	  DecimalFormat num = new DecimalFormat ("0.00");//definition of display format

	  JOptionPane.showMessageDialog(null, + dist +   "metres = " + num.format(feet) 
	  + " feet");//display of feet

   }
}





View PostAngeluS, on 25 Apr, 2007 - 06:56 AM, said:

And place your code in between code tags.
At least that makes it a bit easier to read without having to copy/paste it to an IDE.



Alright. dont think think i did the tag thing right...
Was This Post Helpful? 0
  • +
  • -

#6 AngeluS  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 1
  • View blog
  • Posts: 231
  • Joined: 11-April 07

Re: SIMPLE PROBLEM WITH LOOP. NEED HELP!

Posted 25 April 2007 - 10:22 AM

Ok, much better to read like that.
However I don't have the time at the moment to take a deep look at your code.

Are you using Eclipse or Netbeans or another IDE like that?
If you do then you can run through your code line by line and see what happens in the loop. that way you will get some insight in how the loop works and you will see what happens. I believe you will probably see your mistake quite fast then. If not then I'm sure there is someone here that can tell you where the mistake is...
Was This Post Helpful? 0
  • +
  • -

#7 blueRonK  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 25-April 07

Re: SIMPLE PROBLEM WITH LOOP. NEED HELP!

Posted 26 April 2007 - 04:11 AM

View PostAngeluS, on 25 Apr, 2007 - 10:22 AM, said:

Ok, much better to read like that.
However I don't have the time at the moment to take a deep look at your code.

Are you using Eclipse or Netbeans or another IDE like that?
If you do then you can run through your code line by line and see what happens in the loop. that way you will get some insight in how the loop works and you will see what happens. I believe you will probably see your mistake quite fast then. If not then I'm sure there is someone here that can tell you where the mistake is...


I've tried and i've solved one problem. The problem i have now is that the program always picks the last value in the array and disregards the rest. Please help. Im desperate.
Was This Post Helpful? 0
  • +
  • -

#8 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: SIMPLE PROBLEM WITH LOOP. NEED HELP!

Posted 26 April 2007 - 06:57 AM

Can you post the updated code?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1