Welcome to Dream.In.Code
Become a Java Expert!

Join 149,483 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,388 people online right now. Registration is fast and FREE... Join Now!




SIMPLE PROBLEM WITH LOOP. NEED HELP!

 
Reply to this topicStart new topic

SIMPLE PROBLEM WITH LOOP. NEED HELP!, I need help with this assignment. I've done 90% of it but the loop

blueRonK
25 Apr, 2007 - 05:28 AM
Post #1

New D.I.C Head
*

Joined: 25 Apr, 2007
Posts: 4


My Contributions
CODE
/* 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)
Attached File  convert_metres.DOC ( 327.5k ) Number of downloads: 19
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: SIMPLE PROBLEM WITH LOOP. NEED HELP!
25 Apr, 2007 - 05:40 AM
Post #2

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,349



Thanked: 51 times
Dream Kudos: 25
My Contributions
Can you describe the problem, along with any error messages you are receiving?
User is online!Profile CardPM
+Quote Post

AngeluS
RE: SIMPLE PROBLEM WITH LOOP. NEED HELP!
25 Apr, 2007 - 05:56 AM
Post #3

D.I.C Head
Group Icon

Joined: 10 Apr, 2007
Posts: 229


Dream Kudos: 25
My Contributions
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.
User is offlineProfile CardPM
+Quote Post

blueRonK
RE: SIMPLE PROBLEM WITH LOOP. NEED HELP!
25 Apr, 2007 - 06:31 AM
Post #4

New D.I.C Head
*

Joined: 25 Apr, 2007
Posts: 4


My Contributions
QUOTE(Amadeus @ 25 Apr, 2007 - 06:40 AM) *

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.
User is offlineProfile CardPM
+Quote Post

blueRonK
RE: SIMPLE PROBLEM WITH LOOP. NEED HELP!
25 Apr, 2007 - 06:50 AM
Post #5

New D.I.C Head
*

Joined: 25 Apr, 2007
Posts: 4


My Contributions
QUOTE(Amadeus @ 25 Apr, 2007 - 06:40 AM) *

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


CODE



/* 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

   }
}




QUOTE(AngeluS @ 25 Apr, 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.



Alright. dont think think i did the tag thing right...
User is offlineProfile CardPM
+Quote Post

AngeluS
RE: SIMPLE PROBLEM WITH LOOP. NEED HELP!
25 Apr, 2007 - 09:22 AM
Post #6

D.I.C Head
Group Icon

Joined: 10 Apr, 2007
Posts: 229


Dream Kudos: 25
My Contributions
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...
User is offlineProfile CardPM
+Quote Post

blueRonK
RE: SIMPLE PROBLEM WITH LOOP. NEED HELP!
26 Apr, 2007 - 03:11 AM
Post #7

New D.I.C Head
*

Joined: 25 Apr, 2007
Posts: 4


My Contributions
QUOTE(AngeluS @ 25 Apr, 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...


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.
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: SIMPLE PROBLEM WITH LOOP. NEED HELP!
26 Apr, 2007 - 05:57 AM
Post #8

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,349



Thanked: 51 times
Dream Kudos: 25
My Contributions
Can you post the updated code?
User is online!Profile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 04:38PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month