9 Replies - 2567 Views - Last Post: 02 February 2012 - 03:34 PM Rate Topic: -----

#1 Befigard  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 29-November 11

Error when an invalid integer is entered...

Posted 02 February 2012 - 01:57 PM

I do not know what I am doing wrong. Can someone PLEASE point me in the right direction. I need my GUI to throw a GUI error, when the user enters a negative integer and one over 99999. This it does, however it does not bring the screen back up to reenter a valid integer. I know it is a simple fix, I just don't know what I'm missing to fix this. Can someone please help me so that when an invalid integer is entered it will bring up the screen to enter a valid one...Thanks.

import java.util.Scanner;
import javax.swing.JOptionPane;
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

            WeightCalculator wc = new WeightCalculator();
            boolean validData = false;
            double weight = 0;
            String strWeight = "";
            String strDescription = "";
            Scanner s = new Scanner(System.in);

            do {
                strDescription = JOptionPane.showInputDialog("Enter description of the object ");//input from user
                strWeight = JOptionPane.showInputDialog("Enter weight of the object ");//input from user


                weight = Double.parseDouble(strWeight);

                while (weight < 0 == weight < 99999) {//sets the parimeters for the error message
                    validData = true;


                    JOptionPane.showMessageDialog(null, "Please enter valid value for weight. It should be between 0 and 99999","Inane Error", JOptionPane.ERROR_MESSAGE);//output to user, error message     
                    validData = false;

                               

                }

            } while (validData = false);

            wc.setWeight(weight);
            wc.setDescription(strDescription);

            wc.setWeight(weight);
            wc.setDescription(strDescription);

            JOptionPane.showMessageDialog(null, "The weight of on Mercury is: " + wc.mercuryWeight(), "Result ", JOptionPane.PLAIN_MESSAGE);
            JOptionPane.showMessageDialog(null, "The weight of on Earth is: " + wc.earthWeight(), "Result ", JOptionPane.PLAIN_MESSAGE);
            JOptionPane.showMessageDialog(null, "The weight of on the Moon is: " + wc.moonWeight(), "Result ", JOptionPane.PLAIN_MESSAGE);
            JOptionPane.showMessageDialog(null, "The weight of  on Jupiter is: " + wc.jupiterWeight(), "Result ", JOptionPane.PLAIN_MESSAGE);



        }
    }



/** 
 * 
 * @author Bert 
 */
class WeightCalculator {

    private double weight = 0.0;
    private String description = null;

    public double earthWeight() {
        return weight;
    }

    public double mercuryWeight() {
        return weight * .378;
    }

    public double moonWeight() {
        return weight * .166;
    }

    public double jupiterWeight() {
        return weight * 2.364;
    }

    public String getDescription() {
        return description;
    }

    public void setWeight(double weight) {
        this.weight = weight;
    }

    public void setDescription(String description) {
        this.description = description;
    }
}


This post has been edited by blackcompe: 02 February 2012 - 02:32 PM
Reason for edit:: Please use [code] tags when posting to the forum.


Is This A Good Question/Topic? 0
  • +

Replies To: Error when an invalid integer is entered...

#2 tlhIn`toq  Icon User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6528
  • View blog
  • Posts: 14,439
  • Joined: 02-June 10

Re: Error when an invalid integer is entered...

Posted 02 February 2012 - 02:16 PM

For one.. code tags end with [/code]
:code:

Edit: My mistake. Hard to read with the code fraked up like this.

This post has been edited by tlhIn`toq: 02 February 2012 - 02:17 PM

Was This Post Helpful? 0
  • +
  • -

#3 Befigard  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 29-November 11

Re: Error when an invalid integer is entered...

Posted 02 February 2012 - 02:19 PM

Sorry I put the hashmark wrong in the closing code bracket.
Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq  Icon User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6528
  • View blog
  • Posts: 14,439
  • Joined: 02-June 10

Re: Error when an invalid integer is entered...

Posted 02 February 2012 - 02:19 PM

while (weight < 0 == weight < 99999)


That can't be right, is it? Is == supposed to be "or" in Java?

Does this even make logical sense? because you WANT the weight to be less than 999999 don't you? DOn't you want this to say

While weight less than zero or greater than 99999

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

#5 Befigard  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 29-November 11

Re: Error when an invalid integer is entered...

Posted 02 February 2012 - 02:22 PM

If the user enters a -3 or 99999, the GUI is supposed to throw an error message, which it does.

The ONLY thing I can't figure out is how to get it to pull the screen back up where the user is to re-enter a valid integer.
Was This Post Helpful? 0
  • +
  • -

#6 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2250
  • View blog
  • Posts: 5,340
  • Joined: 10-September 10

Re: Error when an invalid integer is entered...

Posted 02 February 2012 - 02:49 PM

Did you fix the while statement? If so, post updated code.
Was This Post Helpful? 0
  • +
  • -

#7 tlhIn`toq  Icon User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6528
  • View blog
  • Posts: 14,439
  • Joined: 02-June 10

Re: Error when an invalid integer is entered...

Posted 02 February 2012 - 02:51 PM

Because those messages aren't part of the do{} block.
And your have a while{} block, so that doesn't affect the do{} block above.
You seem to have mixed the purposes of
do
{
}
while ();


with

while {}
{
}


And again, look at your == operator in the While condition.
http://docs.oracle.c.../operators.html



Maybe you need to re-visit how a do...while block is constructed.
http://docs.oracle.c...olts/while.html

This post has been edited by tlhIn`toq: 02 February 2012 - 02:52 PM

Was This Post Helpful? 0
  • +
  • -

#8 Befigard  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 29-November 11

Re: Error when an invalid integer is entered...

Posted 02 February 2012 - 02:58 PM

I tried changing the == to or as you stated,and got an error message.
I also do not understand what you are saying about the do{ while....

When I tried to change my code to fit what you were saying I got so many errors, it didn't work at all... I apologize I am VERY new to this, and struggling just to maintain my grades.
Was This Post Helpful? 0
  • +
  • -

#9 tlhIn`toq  Icon User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6528
  • View blog
  • Posts: 14,439
  • Joined: 02-June 10

Re: Error when an invalid integer is entered...

Posted 02 February 2012 - 03:17 PM

Let's see the code you changed everything to, as well as the errors.
"I got some errors" is not helpful at all. We need to know what they are. But let's start with your updated code.

Quote

I tried changing the == to or

Did you really type the word 'or'? Or did you use the or operator of || ? There is a reason I linked the page of operator symbols to you.

View PostBefigard, on 02 February 2012 - 03:58 PM, said:

I also do not understand what you are saying about the do{ while....


You wrote this..


do
{
   // some stuff
}


and

while
{
  // some other stuff
}


But in this case those two blocks aren't affecting each other. Your do block will always execute because there are no constraints on it not to.

Two different blocks of codes surrounded by braces is not the same thing as a do{} while(); construct.

do
{
   // some stuff
}
while (SomeConditionIsTrue);



If you do not see how these two things are different you NEED to go to your professor with a printout of this thread in hand and ask him to talk you through it.
Was This Post Helpful? 1
  • +
  • -

#10 Befigard  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 29-November 11

Re: Error when an invalid integer is entered...

Posted 02 February 2012 - 03:34 PM

Yes, this makes a great deal more sense, thank you...will try and fix it now.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1