Results 1 to 7 of 7
- 12-01-2010, 06:57 AM #1
Member
- Join Date
- Dec 2010
- Posts
- 5
- Rep Power
- 0
Extra spaces in JLabel...Please help..
Hi guys,
While using JLabel in an applet, while displaying (i used right alignment), there are extra spaces being added to the right of the string being displayed....The string does not contain the extra spaces though because i printed to the terminal window, and it printed fine... Morever, the extra spaces are not always being inserted...I was writing a program to build a calculator, and the extra spaces were being inserted only while displaying the result, and not while taking the inputs....as I am new to java, i cannot identify the problem...Any help would be highly appreciated!! :)
- 12-01-2010, 06:58 AM #2
Member
- Join Date
- Dec 2010
- Posts
- 5
- Rep Power
- 0
I can post the code if you guys want to see it....
- 12-01-2010, 08:22 AM #3
Don't post all your code, create a SSCCE that demonstrates the problem.
SSCCE : Java Glossary
And a sentence ends with one full stop, not three or four.
db
- 12-01-2010, 09:49 AM #4
Member
- Join Date
- Dec 2010
- Posts
- 5
- Rep Power
- 0
In the input area (I have used JLabel) if for example, I write :
43 + 53
and then if I press '+', it gives output :
96 +
inserting extra spaces between the result and '+' and thus not giving correct results for my next calculation.
The actionPerformed method() that I wrote is given below :
public void actionPerformed(ActionEvent ae)
{
String str=jlab.getText();
String s=ae.getActionCommand();
double a=0,b=0,c=0;
int n=0,i=0;
char ch,ch1;
int len=str.length();
if(str.charAt(len-1)=='!') str="0";
if(s.equals("1") || s.equals("2") || s.equals("3") || s.equals("4") || s.equals("5") || s.equals("6") || s.equals("7") || s.equals("8") || s.equals("9") || s.equals("0"))
{
if(str.equals("0")) str=s;
else str=(str+s);
}
if(s.equals("."))
{
if(str.charAt(len-1)==' ') str=(str+"0.");
else str=(str+s);
}
if(s.equals("AC")) str="0";
if(s.equals("DEL"))
{
if(str.equals("0"));
else if(str.charAt(len-1)==' ') str=str.substring(0,len-3);
else str=str.substring(0,len-1);
}
if(s.equals("^") || s.equals("=") || s.equals("+") || s.equals("-") || s.equals("*") || s.equals("/") || s.equals("%"))
{
while((ch=str.charAt(i))!=' ')
{
if(ch=='.')
{
n=i;
i++;
if(str.charAt(i)==' ' || i==len)
{
str="Invalid Entry!!";
jlab.setText(str);
return;
}
else continue;
}
if(n==0) a=(a*10)+(ch-48);
else a+=(((ch-48)*1.0)/(Math.pow(10,i-n)));
i++;
if(i==len)
{
if(s.equals("="));
else if(s.equals("%"))
{
a*=100;
str=convert(a);
}
else str=(str + " " + s + " ");
jlab.setText(str);
return;
}
}
i++;
n=0;
ch1=str.charAt(i);
i+=2;
if(i==len)
{
str="Invalid Entry!!";
jlab.setText(str);
return;
}
else
{
while((ch=str.charAt(i))!=' ')
{
if(ch=='.')
{
n=i;
i++;
if(str.charAt(i)==' ' || i==len)
{
str="Invalid Entry!!";
jlab.setText(str);
return;
}
else continue;
}
if(n==0) b=(b*10)+(ch-48);
else b+=(((ch-48)*1.0)/(Math.pow(10,i-n)));
i++;
if(i==len) break;
}
}
if(ch1=='+') c=a+b;
if(ch1=='-') c=a-b;
if(ch1=='*') c=a*b;
if(ch1=='/')
{
if(b==0)
{
str="Invalid Entry!!";
jlab.setText(str);
return;
}
c=a/b;
}
if(ch1=='^') c=Math.pow(a,b);
str=convert(c);
i=len-1;
str=str + " " + s + " ";
System.out.println(str+"-");
jlab.setText(str);
if(s.equals("=")) str=convert(c);
if(s.equals("%"))
{
c*=100;
str=convert(c);
}
}
jlab.setText(str);
}
}
Thank you for your time.
- 12-01-2010, 09:50 AM #5
Member
- Join Date
- Dec 2010
- Posts
- 5
- Rep Power
- 0
The post isn't showing the gap between the 96 and the '+' sign.....
- 12-01-2010, 10:50 AM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Use code tags.
- 12-06-2010, 03:53 AM #7
Member
- Join Date
- Dec 2010
- Posts
- 5
- Rep Power
- 0
Similar Threads
-
Adding a JLabel to a JPanel - jlabel not showing
By Bongeh in forum New To JavaReplies: 17Last Post: 04-06-2010, 11:02 PM -
Extra Sun Voucher available
By PriyaK in forum Reviews / AdvertisingReplies: 4Last Post: 01-15-2010, 06:41 AM -
Java Project for extra Credit
By Ankiel24 in forum New To JavaReplies: 5Last Post: 04-24-2009, 09:56 AM -
extra line in GridLayout
By aznboarder in forum SWT / JFaceReplies: 3Last Post: 03-25-2009, 05:36 AM -
Extra bracket
By CrazyShells Slam in forum New To JavaReplies: 5Last Post: 05-16-2008, 06:12 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks