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 15 of 15

Thread: Why isn't this working?

  1. #1
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Unhappy Why isn't this working?

    import javax.swing.JFrame;
     
     
     
    import javax.swing.*;
    import java.util.*;
    import java.io.*;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    public class ScientificNotation extends JFrame
    {
     
    private JTextField field;
     
    public ScientificNotation()
    {
    setTitle("Scientific Notation");
    setVisible(true);
    field = new JTextField(25);
    setLayout(new GridLayout(2,2));
    add(field);
    field.setVisible(true);
    Container pane = getContentPane();
     
    field.addActionListener(new ActionListener() {
     
    public void actionPerformed(ActionEvent e)
    {
    String number = field.getText();
    String newString = "";
    int digits = 0;
     
    if (number.charAt(0) == '0' && number.charAt(1) == '.')
    {
    for (int i =2; i < number.length(); i++)
    {
    while (number.charAt(i) == '0')
    {
    digits--;
    }
     
    newString = newString + String.valueOf(number.charAt(i));
    }
     
    JOptionPane.showMessageDialog(null, newString + "E" + digits, "Scientific Notation is:", JOptionPane.INFORMATION_MESSAGE);
    }
     
    else
    {
    for (int i = 0; i < number.length(); i++)
    {
     
    while(number.charAt(i) != '.')
    {
    digits++;
    if (i ==0)
    {
    newString = newString + String.valueOf(number.charAt(i));
     
    }
     
    else if (i==1)
    {
    newString = newString + ".";
    }
     
    else
    {
     
    newString = newString + String.valueOf(number.charAt(i));
     
     
    }
     
    }
    newString = newString + String.valueOf(number.charAt(i));
    }
    JOptionPane.showMessageDialog(null, newString + "E" + digits, "Scientific Notation is:", JOptionPane.INFORMATION_MESSAGE);
    }
     
    }});
    //setContentPane(pane);
    }
     
     
    public static void main(String[] args)
    {
    ScientificNotation sn = new ScientificNotation();
     
    }
    }

    How do you get the JTextField to be "normal" sized?

    Also, why is my window locking up and the JOptionPane never appearing?


  2. #2
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Why isn't this working?

    Ok, It's not even going into the loops. I told it to println() the i values and it didn't print anything. The stupid JFrame just turns black and I have to shut it off with the big red button.

  3. #3
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Why isn't this working?

    Sigh.

    For somebody who has the "How to ask questions the smart way" link in his signature, you sure do break a lot of the rules outlined in there.

    When you post questions, you always use a meaningless title- "Why isn't this working?" is as meaningful a title as "Because something you did is wrong" is a meaningful answer.

    Then you always post all of your code (as opposed to an SSCCE), without proper indentation, and ask a vague question without actually going into the details of what's going on, what you expected would happen, what you tried, etc.

    Have you run through your code with a debugger?

    I'm not going to help you any further, until you start addressing these things, in addition to the problems with how you yourself "answer" the questions of others.

    You might think that's rude, but your continued violation of basic forum etiquette, which you've been told about over and over again, is even more rude.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  4. #4
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Unhappy Re: Why isn't this working?

    Well, these two pictures might help kinda clarify things.

    The before and enter are after I hit enter and activate the Action Listener.
    Code has been updated.
    import javax.swing.JFrame;
    import javax.swing.JLabel;
     
     
     
    import javax.swing.*;
    import java.util.*;
    import java.io.*;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    public class ScientificNotation extends JFrame
    {
     
    private JTextField field;
    private JLabel notation;
    public ScientificNotation()
    {
    setTitle("Scientific Notation");
    setVisible(true);
    field = new JTextField(25);
    add(field);
    notation = new JLabel("Nothing");
    add(notation);
    field.setBounds(50, 50, field.getPreferredSize().width, field.getPreferredSize().height);
    field.setVisible(true);
    Container pane = getContentPane();
     
    field.addActionListener(new ActionListener() {
     
    public void actionPerformed(ActionEvent e)
    {
    String number = field.getText();
    String newString = "";
    int digits = 0;
     
    if (number.charAt(0) == '0' && number.charAt(1) == '.')
    {
    for (int i =2; i < number.length(); i++)
    {
    while (number.charAt(i) == '0')
    {
    	System.out.println(i);
    digits--;
    }
    System.out.println(i);
    newString = newString + String.valueOf(number.charAt(i));
     
    }
     
    JOptionPane.showMessageDialog(null, newString + "E" + digits, "Scientific Notation is:", JOptionPane.INFORMATION_MESSAGE);
    notation.setText(newString + "E" + digits);
    }
     
    else
    {
    for (int i = 0; i < number.length(); i++)
    {
     
    while(number.charAt(i) != '.')
    {
    digits++;
    if (i ==0)
    {
    newString = newString + String.valueOf(number.charAt(i));
     
    }
     
    else if (i==1)
    {
    newString = newString + ".";
    }
     
    else
    {
     
    newString = newString + String.valueOf(number.charAt(i));
     
     
    }
     
    }
    newString = newString + String.valueOf(number.charAt(i));
    }
    JOptionPane.showMessageDialog(null, newString + "E" + digits, "Scientific Notation is:", JOptionPane.INFORMATION_MESSAGE);
    notation.setText(newString + "E" + digits);
    }
     
    }});
    //setContentPane(pane);
    }
     
     
    public static void main(String[] args)
    {
    ScientificNotation sn = new ScientificNotation();
     
    }
    }
    Attached Images Attached Images

  5. #5
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Why isn't this working?

    Also, I might add that I can't exit out of the "Black Screen of Doom" window by hitting the X button or even hitting the Close Window with Windows. I don't mean that the program still runs after I hit the X, I mean the window won't go away, unless I end it by hitting the Terminate button in Eclipse.

  6. #6
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Angry Re: Why isn't this working?

    Updated again. Still having problems.
    import javax.swing.JFrame;
    import javax.swing.JLabel;
     
     
     
    import javax.swing.*;
    import java.util.*;
    import java.io.*;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    public class ScientificNotation extends JFrame
    {
     
    private JTextField field;
    private JLabel notation;
    public ScientificNotation()
    {
    setTitle("Scientific Notation");
    setVisible(true);
    field = new JTextField(25);
    add(field);
    notation = new JLabel("Nothing");
    add(notation);
    field.setBounds(50, 50, field.getPreferredSize().width, field.getPreferredSize().height);
    field.setVisible(true);
    Container pane = getContentPane();
     
    field.addActionListener(new ActionListener() {
     
    public void actionPerformed(ActionEvent e)
    {
    String number = field.getText();
    String newString = "";
    int digits = 0;
     
    if (number.charAt(0) == '0' && number.charAt(1) == '.')
    {
    for (int i =2; i < number.length(); i++)
    {
    while (number.charAt(i) == '0')
    {
    	System.out.println(i);
    digits--;
    }
    System.out.println(i);
    newString = newString + String.valueOf(number.charAt(i));
     
    }
     
    JOptionPane.showMessageDialog(null, newString + "E" + digits, "Scientific Notation is:", JOptionPane.INFORMATION_MESSAGE);
    notation.setText(newString + "E" + digits);
    }
     
    else if(number.charAt(0) != '0' && (Character) number.charAt(1)!= null)
    {
    for (int i = 0; i < number.length(); i++)
    {
     
    while(number.charAt(i) != '.')
    {
    digits++;
    if (i ==0)
    {
    newString = newString + String.valueOf(number.charAt(i));
     
    }
     
    else if (i==1)
    {
    newString = newString + ".";
    }
     
    else
    {
     
    newString = newString + String.valueOf(number.charAt(i));
     
     
    }
     
    }
    newString = newString + String.valueOf(number.charAt(i));
    }
    JOptionPane.showMessageDialog(null, newString + "E" + digits, "Scientific Notation is:", JOptionPane.INFORMATION_MESSAGE);
    notation.setText(newString + "E" + digits);
    }
    else
    {
    	JOptionPane.showMessageDialog(null, number + "E" + digits, "Scientific Notation is:", JOptionPane.INFORMATION_MESSAGE);
    }
     
    }});
    //setContentPane(pane);
    }
     
     
    public static void main(String[] args)
    {
    ScientificNotation sn = new ScientificNotation();
     
    }
    }

    What's going on?

  7. #7
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Why isn't this working?

    Again: "still having problems" is as useful to us as us saying "then fix it" is useful to you. And again, you're still posting your unwarranted assumptions as advice to other users. Maybe somebody else is up to helping you, but I'm not going to point out your pretty obvious error (which a debugger would show you in about 2 seconds) until you address some of these issues.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  8. #8
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Why isn't this working?

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
     
    public class ScientificNotation extends JFrame {
     
        private JTextField field;
        private JLabel notation;
        private JPanel mypanel;
     
        public ScientificNotation() {
            mypanel = new JPanel();
            setTitle("Scientific Notation");
            notation = new JLabel("Nothing");
            field = new JTextField(25);
            mypanel.add(field);
     
            mypanel.add(notation);
            field.setBounds(50, 50, field.getPreferredSize().width, field.getPreferredSize().height);
            add(mypanel);
     
     
            field.addActionListener(new ActionListener()     {
     
                public void actionPerformed(ActionEvent e) {
                    try {
                        String number = field.getText();
                        String newString = "";
                        int digits = 0;
     
                        if (number.charAt(0) == '0' && number.charAt(1) == '.') {
                            for (int i = 2; i < number.length(); i++) {
                                while (number.charAt(i) == '0') {
                                    System.out.println(i);
                                    digits--;
                                }
                                System.out.println(i);
                                newString = newString + String.valueOf(number.charAt(i));
     
                            }
     
                            JOptionPane.showMessageDialog(null, newString + "E" + digits, "Scientific Notation is:", JOptionPane.INFORMATION_MESSAGE);
                            notation.setText(newString + "E" + digits);
                        } else if (number.charAt(0) != '0' && (Character) number.charAt(1) != null) {
                            for (int i = 0; i < number.length(); i++) {
     
                                while (digits < 10) { //while (number.charAt(i) != '.') { <--OLD CODE - WAS REASON FOR ERRORS!
                                    digits++;
                                    if (i == 0) {
                                        newString = newString + String.valueOf(number.charAt(i));
     
                                    } else if (i == 1) {
                                        newString = newString + ".";
                                    } else {
     
                                        newString = newString + String.valueOf(number.charAt(i));
     
     
                                    }
     
                                }
                                newString = newString + String.valueOf(number.charAt(i));
                            }
                            JOptionPane.showMessageDialog(null, newString + "E" + digits, "Scientific Notation is:", JOptionPane.INFORMATION_MESSAGE);
                            notation.setText(newString + "E" + digits);
                        } else {
                            JOptionPane.showMessageDialog(null, number + "E" + digits, "Scientific Notation is:", JOptionPane.INFORMATION_MESSAGE);
                        }
     
                    } catch (StringIndexOutOfBoundsException ex) {
                        System.out.println("Exception Caught Here");
                    }
                }
            });
     
     
        }
     
        public static void main(String[] args) {
            JFrame sn = new ScientificNotation();
            sn.setSize(500, 200);
            sn.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            sn.setVisible(true);
     
        }
    }

    Ive changed your while loop code to just show how it doesn't crash with the modified code, then if my assumptions are correct, consider the usage of the old code.
    -Also I changed your code so you aren't attempting to add two components onto the JFrame directly.

    Offtopic - KevinWorkman, you mention debugger would pick up whatever error he had rather quickly. I've never attempted any usage of a debugger, but do you have any good tutorials in mind for why a debugger is useful and how to use it? Much appreciated.
    Last edited by newbie; January 21st, 2011 at 03:43 PM.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  9. The Following User Says Thank You to newbie For This Useful Post:

    javapenguin (January 21st, 2011)

  10. #9
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Unhappy Re: Why isn't this working?

    Quote Originally Posted by KevinWorkman View Post
    Again: "still having problems" is as useful to us as us saying "then fix it" is useful to you. And again, you're still posting your unwarranted assumptions as advice to other users. Maybe somebody else is up to helping you, but I'm not going to point out your pretty obvious error (which a debugger would show you in about 2 seconds) until you address some of these issues.
    I showed what was the problem with the pictures. I thought you'd see them.

  11. #10
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Why isn't this working?

    Oh, I see, the i value wasn't going up inside the while loop. I feared that might happen, but I didn't know that's what was causing the error. Also, the code still is glitched. It's repeating the first digit a lot of times. By any chance is the String taking in the entire JTextField, including the blank areas? If so, I'll use trim().

  12. #11
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Why isn't this working?

    What was the try catch for? Also, why is the first digit repeating a bunch of times and then adding that to my exponent?
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
     
    public class ScientificNotation extends JFrame {
     
        private JTextField field;
        private JLabel notation;
        private JPanel mypanel;
        private int number2;
        private int power;
        public ScientificNotation() {
            mypanel = new JPanel();
            setTitle("Scientific Notation");
            notation = new JLabel("Nothing");
            field = new JTextField(25);
            mypanel.add(field);
     
            mypanel.add(notation);
            field.setBounds(50, 50, field.getPreferredSize().width, field.getPreferredSize().height);
            add(mypanel);
     
     
            field.addActionListener(new ActionListener()     {
     
                public void actionPerformed(ActionEvent e) {
                    try {
                        String number = field.getText().trim();
                        String newString = "";
                        int digits = 0;
     
                        if (number.charAt(0) == '0' && number.charAt(1) == '.') {
                            for (int i = 2; i < number.length(); i++) {
                                while (number.charAt(i) == '0') {
                                    System.out.println("i" + i);
                                    digits--;
                                }
                                System.out.println("i" + i);
                                newString = newString + String.valueOf(number.charAt(i));
     
                            }
     
                            JOptionPane.showMessageDialog(null, newString + "E" + digits, "Scientific Notation is:", JOptionPane.INFORMATION_MESSAGE);
                            notation.setText(newString + "E" + digits);
                        } else if (number.charAt(0) != '0' && (Character) number.charAt(1) != null) {
                            for (int i = 0; i < number.length(); i++) {
     
                                while (digits < 10) { //while (number.charAt(i) != '.') { <--OLD CODE - WAS REASON FOR ERRORS!
                                    digits++;
                                    if (i == 0) {
                                        newString = newString + String.valueOf(number.charAt(i));
     
                                    } else if (i == 1) {
                                        newString = newString + ".";
                                    } else {
     
                                        newString = newString + String.valueOf(number.charAt(i));
     
     
                                    }
     
                                }
                                newString = newString + String.valueOf(number.charAt(i));
                            }
                            JOptionPane.showMessageDialog(null, newString + "E" + digits, "Scientific Notation is:", JOptionPane.INFORMATION_MESSAGE);
                            notation.setText(newString + "E" + digits);
                        } else {
                            JOptionPane.showMessageDialog(null, number + "E" + digits, "Scientific Notation is:", JOptionPane.INFORMATION_MESSAGE);
                        }
     
                    } catch (StringIndexOutOfBoundsException ex) {
                        System.out.println("Exception Caught Here");
                    }
                }
            });
     
     
        }
     
        public void setPower(int power)
        {
        	this.power = power;
        }
     
        public int getPower()
        {
        	return power;
        }
     
        public void setNumber(int number2)
        {
        	this.number2 = number2;
        }
     
        public int getNumber()
        {
        	return number2;
        }
     
        public static void main(String[] args) {
            JFrame sn = new ScientificNotation();
            sn.setSize(500, 200);
            sn.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            sn.setVisible(true);
     
        }
    }

  13. #12
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Why isn't this working?

    Well as the code in your for loop is adding onto the String, not replacing, its going to add the value onto the String quite a few times:
    newString = newString + String.valueOf(number.charAt(i));

    I'll be honest, I haven't read your code to understand what your trying to do, but each pass of loop is adding onto the String instead of replacing.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  14. The Following User Says Thank You to newbie For This Useful Post:

    javapenguin (January 21st, 2011)

  15. #13
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Why isn't this working?

    Quote Originally Posted by newbie View Post
    Well as the code in your for loop is adding onto the String, not replacing, its going to add the value onto the String quite a few times:
    newString = newString + String.valueOf(number.charAt(i));

    I'll be honest, I haven't read your code to understand what your trying to do, but each pass of loop is adding onto the String instead of replacing.
    That's only supposed to be updating the String to include the new char. The String was originally blank and having the text from the JTextField pushed into it one char at a time till the condition is met.

    What's happening is that I think in the while loop, the i isn't being updated.
    Let me try something....

  16. #14
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Lightbulb Re: Why isn't this working?



    I see now what might be the problem. The decimal point is never being pushed over.

  17. #15
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Why isn't this working?

    That and it's sometimes cloning the first digit.

Similar Threads

  1. SQL query not working
    By mjpam in forum What's Wrong With My Code?
    Replies: 11
    Last Post: September 28th, 2010, 05:15 PM
  2. [SOLVED] Buttons not working..
    By khms in forum What's Wrong With My Code?
    Replies: 14
    Last Post: September 2nd, 2010, 06:01 PM
  3. Cannot seem to get this working
    By OttawaGuy in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 28th, 2010, 03:41 PM
  4. Boggle not working?
    By gandalf5166 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 6th, 2010, 10:53 AM
  5. Replies: 4
    Last Post: January 27th, 2009, 12:03 AM