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.
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.
2 Attachment(s)
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.
Code java:
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();
}
}
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.
Re: Why isn't this working?
Updated again. Still having problems.
Code java:
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?
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.
Re: Why isn't this working?
Code java:
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.
Re: Why isn't this working?
Quote:
Originally Posted by
KevinWorkman
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.
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().
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?
Code java:
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);
}
}
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.
Re: Why isn't this working?
Quote:
Originally Posted by
newbie
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....
Re: Why isn't this working?
^:)^^:)^#-o#-o#-o#-o#-o#-o#-o#-o#-o#-o#-o#-o#-o
I see now what might be the problem. The decimal point is never being pushed over.
Re: Why isn't this working?
That and it's sometimes cloning the first digit.