Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: My modulus result is wrong.

  1. #1
    Junior Member
    Join Date
    Feb 2014
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default My modulus result is wrong.

    Hi Java professionals,
    my remainder result is always wrong and I cannot understand why. The following is my code,please help,many thanks in advance.


    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    public class Question extends JFrame implements ActionListener
    {
        private JLabel amt,dollar,fifty,title,result,error,creator;
        private TextField tb1,tb2,tb3;
        private JButton count;
        public Question()
        {
            setSize(400,300);
            JPanel contentPane = new JPanel(null);
            contentPane.setBorder(BorderFactory.createLineBorder(Color.gray));
            setContentPane(contentPane);
            count = new JButton("=");
            title = new JLabel("Calculate Change");
            amt = new JLabel("Enter the amount:");
            dollar = new JLabel("1 Dollar Coin:");
            fifty = new JLabel("50 Cent Coin:");
            error = new JLabel("wrong information.");
            creator = new JLabel("Created by ?????");
            Font bold = new Font("Arial",Font.BOLD,15);
            Font wrong = new Font("Arial",Font.ITALIC,12);
            Font smaller = new Font("Arial",Font.ITALIC,10);
            error.setForeground(Color.red);
            tb1 = new TextField(13);
            tb2=new TextField(12);
            tb3= new TextField(12);
            tb2.setEnabled(false);
            tb3.setEnabled(false);
            creator.setFont(smaller);
            error.setFont(wrong);
            title.setFont(bold);
            title.setBounds(130,-14,150,40);
            amt.setBounds(20, 20, 120, 20);
            dollar.setBounds(40, 45, 120, 20);
            fifty.setBounds(40, 70, 120, 20);
            tb1.setBounds(160, 21, 90, 24);
            tb2.setBounds(160, 45, 90, 20);
            tb3.setBounds(160, 70, 90, 20);
            error.setBounds(260,170,150,20);
            count.setBounds(100,200,90,20);
            creator.setBounds(240,220,180,20);
            contentPane.add(title);
            contentPane.add(amt);
            contentPane.add(dollar);
            contentPane.add(fifty);
            contentPane.add(tb1);
            contentPane.add(tb2);
            contentPane.add(tb3);
            contentPane.add(error);
            contentPane.add(creator);
            contentPane.add(count);
            count.addActionListener(this);
        }
     
        public void actionPerformed(ActionEvent e)
        {
            try
            {
                //String input = tb1.getText();
                double convert = Double.parseDouble(tb1.getText());
                if((e.getSource() == count) && (convert >=1)) 
                {
                    double result;
                    result = convert /1;
                    convert = convert % 1;
                   int result2 = (int)result;
                    String result3 = String.valueOf(result2);
                    tb2.setText(result3);
     
                }
                if((convert >= 0.5) && (convert < 0.99))
                {
                    tb3.setText("1");
                    convert = convert % 0.5;
                      String result4 = String.valueOf(convert);
                    error.setText(result4);
                }
               }catch(Exception ex)
            {
                error.setText("Invalid input.");
            }
        }
     
        public static void main(String []args)
        {
            Question frame = new Question();
            frame.setVisible(true);
        }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: My modulus result is wrong.

    Please post the code's output that shows the problem.
    What values are not being computed like you expect?
    Give some examples of the input to the program and the program's output.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Apr 2012
    Posts
    161
    Thanks
    0
    Thanked 27 Times in 27 Posts

    Default Re: My modulus result is wrong.

    Are you sure you're using the modulus operator like you expect?
    convert = convert % 1;
    after this statement convert will always equal 1 such that convert >= 1
    When convert is < 1 convert will equal the original number

Similar Threads

  1. Using Modulus to alternate between 2 functions?
    By Rexoa in forum Java Theory & Questions
    Replies: 5
    Last Post: February 25th, 2014, 11:59 PM
  2. modulus question
    By meangreen2003 in forum Java Theory & Questions
    Replies: 2
    Last Post: February 18th, 2014, 05:19 PM
  3. Neural Network Programming - Wrong result after training
    By DarioP in forum What's Wrong With My Code?
    Replies: 9
    Last Post: June 18th, 2013, 04:50 PM
  4. BruceForce Method and Modulus HELP!
    By JAVAHELPP in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 13th, 2013, 03:38 AM
  5. Modulus with BigIntegers
    By JDreben in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 24th, 2012, 10:00 PM