Results 1 to 7 of 7
  1. #1
    sohamde is offline Member
    Join Date
    Dec 2010
    Posts
    5
    Rep Power
    0

    Default 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!! :)

  2. #2
    sohamde is offline Member
    Join Date
    Dec 2010
    Posts
    5
    Rep Power
    0

    Default

    I can post the code if you guys want to see it....

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default

    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

  4. #4
    sohamde is offline Member
    Join Date
    Dec 2010
    Posts
    5
    Rep Power
    0

    Default

    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.

  5. #5
    sohamde is offline Member
    Join Date
    Dec 2010
    Posts
    5
    Rep Power
    0

    Default

    The post isn't showing the gap between the 96 and the '+' sign.....

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    Use code tags.

  7. #7
    sohamde is offline Member
    Join Date
    Dec 2010
    Posts
    5
    Rep Power
    0

    Default

    Hey i solved the problem myself. It was a problem with my string handling, and not at all related to JLabel (which I obviously understood later).

Similar Threads

  1. Adding a JLabel to a JPanel - jlabel not showing
    By Bongeh in forum New To Java
    Replies: 17
    Last Post: 04-07-2010, 12:02 AM
  2. Extra Sun Voucher available
    By PriyaK in forum Reviews / Advertising
    Replies: 4
    Last Post: 01-15-2010, 07:41 AM
  3. Java Project for extra Credit
    By Ankiel24 in forum New To Java
    Replies: 5
    Last Post: 04-24-2009, 10:56 AM
  4. extra line in GridLayout
    By aznboarder in forum SWT / JFace
    Replies: 3
    Last Post: 03-25-2009, 06:36 AM
  5. Extra bracket
    By CrazyShells Slam in forum New To Java
    Replies: 5
    Last Post: 05-16-2008, 07:12 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •