The Student Room Group

Stuck with Java: string split not working... (HND - Unit 41: Java Programming)

Hi!

I'm having trouble with a section of code for my Java project (a program that converts between Fractions, Decimals and Percentages). The problem currently is the conversion from fraction to decimal, where the program correctly performs when testing in console but won't work when changing the code to work with Swing.

Here's the code from the console:

public class Demo {
public static void main(String[] args) {
Scanner scanner = new Scanner (System.in);
System.out.print("Enter: ":wink:;
String input = scanner.nextLine();
String[] parts = input.split("/":wink:;
String Num = parts[0];
String Denom = parts[1]; int num = Integer.parseInt(Num);
int denom = Integer.parseInt(Denom);
double answer = num / (double) denom;
String FD = Double.toString(answer); System.out.print(FD);
}
}

With the output:
Enter: 1/2
0.5BUILD SUCCESSFUL (total time: 3 seconds)

Here's the attempted section of the code alteration for Swing:

String input = fromField.getText();
String[] parts = input.split("/":wink:;
String Num = parts[0];
String Denom = parts[1];
int num = Integer.parseInt(Num);
int denom = Integer.parseInt(Denom);
double answer = num / (double) denom;
String FD = Double.toString(answer);
answerPanel.setText(FD);

However, when testing, all that appears is this:

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 1 at my.fraperdec.FraPerDecUI.convertButtonActionPerformed(FraPerDecUI.java:185) at my.fraperdec.FraPerDecUI.access$200(FraPerDecUI.java:13) at my.fraperdec.FraPerDecUI$3.actionPerformed(FraPerDecUI.java:73) at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022) at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348) at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402) at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252) at java.awt.Component.processMouseEvent(Component.java:6539) at javax.swing.JComponent.processMouseEvent(JComponent.java:3324) at java.awt.Component.processEvent(Component.java:6304) at java.awt.Container.processEvent(Container.java:2239) at java.awt.Component.dispatchEventImpl(Component.java:4889) at java.awt.Container.dispatchEventImpl(Container.java:2297) at java.awt.Component.dispatchEvent(Component.java:4711) at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4904) at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4535) at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4476) at java.awt.Container.dispatchEventImpl(Container.java:2283) at java.awt.Window.dispatchEventImpl(Window.java:2746) at java.awt.Component.dispatchEvent(Component.java:4711) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:760) at java.awt.EventQueue.access$500(EventQueue.java:97) at java.awt.EventQueue$3.run(EventQueue.java:709) at java.awt.EventQueue$3.run(EventQueue.java:703) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:74) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:84) at java.awt.EventQueue$4.run(EventQueue.java:733) at java.awt.EventQueue$4.run(EventQueue.java:731) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:74) at java.awt.EventQueue.dispatchEvent(EventQueue.java:730) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:205) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93) at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

I'm not 100% sure with what is causing the above to appear, (my only guess is there is something wrong with the string split when using Swing) but if anyone can identify the problem, it would be greatly appreciated.

Thank you!
(edited 5 years ago)
Firstly, no String.Split won't be behaving any differently just because the data has been obtained from a JTextField - a String is a String no matter where it has come from, so if this line is indeed the problem then it suggests that the problem is with the actual Text data field inside the JTextField.

The exception is pointing at FraPerDecUI.java at Line 185 inside a method called convertButtonActionPerformed.

Is this exception definitely the same line with your input.Split("/"); ?

Presumably fromField is a JTextField which you're expecting to contain some data.

Are you 100% that this is indeed the correct JTextField ?
Are there any other instances of JTextField which may be getting created and overwriting fromField while the program is running?
Is there anything which might be calling fromField.setText to overwrite the content before your convertButtonActionPerformed method is called?

Try running the program with the debugger and set a breakpoint on String input = fromField.getText(); and check what the value of input happens to be.

Also try setting breakpoints on other lines which modify the text content of fromField - for example, anything which calls fromField.setText.

Depending how complex your code is, the problem might be that something modifying your JTextField is happening in the 'wrong' order - i.e. you're expecting it to contain data but something else in your code changes that data - if you set some breakpoints in your IDE on any lines which might change the value of the text box, then the debugger should make it much easier to see what's going on. (Which IDE are you using by the way? )
(edited 5 years ago)
Reply 2
Fixed! There was an instance where the fromField.setText was called where it seemed to be overwriting it. I moved it within the if statement it was relevant to so now that instance won't run every time the program runs, It now runs as expected.

Thank you for the help!
Original post by x1chloe1x
Fixed! There was an instance where the fromField.setText was called where it seemed to be overwriting it. I moved it within the if statement it was relevant to so now that instance won't run every time the program runs, It now runs as expected.

Thank you for the help!


Well done for fixing the problem! On the question about the IDE/Debugger, do you happen to be using an IDE or are you running Java from the command-line?

The reason I'm asking is that breakpoints are just really useful. You can usually troubleshoot problems like this quite quickly when you've got an IDE and are able to just quickly right-click on a few lines of code to set breakpoints. When the program is running under the debugger, it will pause execution on those lines, allowing you to inspect the content of variables, allow you to execute code line-by-line ("step over" / "step into"), and let you see exactly what the program is doing.

I know that a lot of tutors and lecturers don't really bother with IDEs and don't bother showing people how to use a debugger/breakpoints either, so if you get a chance, then take a look because it will save you loads of time in the future.
Reply 4
Original post by winterscoming
Well done for fixing the problem! On the question about the IDE/Debugger, do you happen to be using an IDE or are you running Java from the command-line?

The reason I'm asking is that breakpoints are just really useful. You can usually troubleshoot problems like this quite quickly when you've got an IDE and are able to just quickly right-click on a few lines of code to set breakpoints. When the program is running under the debugger, it will pause execution on those lines, allowing you to inspect the content of variables, allow you to execute code line-by-line ("step over" / "step into":wink:, and let you see exactly what the program is doing.

I know that a lot of tutors and lecturers don't really bother with IDEs and don't bother showing people how to use a debugger/breakpoints either, so if you get a chance, then take a look because it will save you loads of time in the future.


I was initially recommended to use BlueJ in class, as I started out learning Java in February, but I didn't really get along with that so I'm now using Netbeans IDE 8.2. I reckon I'll look into breakpoints and see how they work for me; the program I'm creating seems to be fairly large.

Thank you again for the help!
Original post by x1chloe1x
I was initially recommended to use BlueJ in class, as I started out learning Java in February, but I didn't really get along with that so I'm now using Netbeans IDE 8.2. I reckon I'll look into breakpoints and see how they work for me; the program I'm creating seems to be fairly large.

Thank you again for the help!

You're welcome! I've seen BlueJ before, and it's not very good. NetBeans is a definite improvement anyway. There isn't a whole lot you need to know for breakpoints, this video is about 4 min and should give you the gist of it - https://www.youtube.com/watch?v=joWldbcp1So

Good luck with the program and learning Java!

Quick Reply

Latest

Trending

Trending