Click here to Skip to main content
15,885,992 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
private void btnCalculateMouseClicked(java.awt.event.MouseEvent evt) {                                          
        String hexNumber;
        int decNumber = 0;
        int counter;

        hexNumber = txtHexNumber.getText();

        for (counter = (hexNumber.length() - 1); counter >= 0; counter--);
        {
            System.out.println(counter);
        }
        txtDecNumber.setText(decNumber + "");
    } 


It doesn't care how long the String hexNumber is, the value of counter is always -1!
But I don't understand why!

What I have tried:

I tried to run the code in the single-step mode but I couldn't find the problem.
Posted
Updated 28-Dec-16 9:02am
v2
Comments
[no name] 28-Dec-16 12:32pm    
Take the semi-colon off of the end of your for line.

The culprit is that semi-colon that terminates your loop:
for (counter = (hexNumber.length() - 1); counter >= 0; counter--);
 
Share this answer
 
v2
Comments
OnLearn 28-Dec-16 12:43pm    
Thank you soo much! That are the little mistakes, which you won't find in hours!
Peter Leow 28-Dec-16 13:04pm    
You are welcome.
The problem is the semi at the end of this line.
C++
for (counter = (hexNumber.length() - 1); counter >= 0; counter--);


You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
 
Share this answer
 

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