Results 1 to 20 of 31
Thread: Comparing Strings Application
- 08-09-2011, 09:55 AM #1
Member
- Join Date
- Aug 2011
- Location
- West Virginia
- Posts
- 38
- Rep Power
- 0
Comparing Strings Application
Hello everyone. I have been working on this code for a bit and I can't figure out what I am doing wrong. I am guessing that I have the right idea and am just not implementing it correctly. If anyone would care to help out it would be much appreciated.
The assignment is:
Write an application that that prompts a user to enter two strings, and check whether the first string entered is a substring of the second string entered. Remember you can check whether a string is a substring of another string by using the indexOf() method in the String class.
Here is my code thus far:
Java Code:import javax.swing.*; import javax.swing.JOptionPane; //Main class public class StringApplication { // Variables String mainString; String secondaryString; int idex1; // JOptionPane Input class public void GetInput() { String mainString = JOptionPane.showInputDialog("Enter a String"); String secondaryString = JOptionPane.showInputDialog("Enter Another String"); } // use indexOf method to compare strings public void checkStringContains() { int index1 = mainString.indexOf(secondaryString); if (index1 != -1) System.out.println("The string contains the substring " + secondaryString); else System.out.println("The string does not contain the substring " + secondaryString); } // main method public static void main(String[] args) { new StringApplication().checkStringContains(); } }
- 08-09-2011, 10:06 AM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,407
- Blog Entries
- 7
- Rep Power
- 17
Your mainString and secondaryString are local variables in the GetInput() method so other methods can't 'see' them. Make them member variables instead (as you already did) and get rid of the local definitions.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 08-09-2011, 10:08 AM #3
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Have a look at what you are checking:
Where did you think you gave mainString a value? Look at that code and see why it didn't happen. In particular, notice that you have two mainString variables.Java Code:// use indexOf method to compare strings public void checkStringContains() { [color=green]System.out.println("About to check \"" + mainString + "\" with \"" + secondaryString + "\"");[/color] int index1 = mainString.indexOf(secondaryString); //etc }
- 08-09-2011, 07:55 PM #4
Member
- Join Date
- Aug 2011
- Location
- West Virginia
- Posts
- 38
- Rep Power
- 0
I can see that the problem probably lies with my input method, but I am unsure what is wrong with it? It is a runtime error "Exception in thread "main" java.lang.NullPointerException at lines 26 and 32
is the error coming because I am trying to check two "null" values?
Here is my latest code:
Java Code:import javax.swing.*; import javax.swing.JOptionPane; //Main class public class StringApplication { // Variables private String mainString; private String secondaryString; private int idex1; // JOptionPane Input class public void GetInput() { mainString = JOptionPane.showInputDialog("Enter a String"); secondaryString = JOptionPane.showInputDialog("Enter Another String"); } // use indexOf method to compare strings public void checkStringContains() { System.out.println("About to check \"" + mainString + "\" with \"" + secondaryString + "\""); int index1 = mainString.indexOf(secondaryString); if (index1 != -1) System.out.println("The string contains the substring " + secondaryString); else System.out.println("The string does not contain the substring " + secondaryString); } // main method public static void main(String[] args) { new StringApplication().GetInput(); new StringApplication().checkStringContains(); } }Last edited by broo7198; 08-09-2011 at 07:59 PM.
- 08-09-2011, 08:06 PM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Those are two completey different objects you are calling those methods on, each with their own mainString and secondaryString attributes.Java Code:new StringApplication().GetInput(); new StringApplication().checkStringContains();
So the first one populates its mainString and secondaryString, and the second on prints its ones (which are still null).
Assign the first "new String...etc" to a variable, then call the two methods on that variable.
- 08-09-2011, 08:14 PM #6
Member
- Join Date
- Aug 2011
- Location
- West Virginia
- Posts
- 38
- Rep Power
- 0
I dont understand what you mean by assign "new StringApplication().GetInput();" to a variable?
Last edited by broo7198; 08-09-2011 at 08:17 PM.
- 08-09-2011, 08:21 PM #7
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
The above is telling Java to create a new instance of StringApplication.Java Code:new StringApplication();
Instead of just calling the getInput() on that, you assign it to a StringApplication variable, and call it (and the other method) on the variable.
- 08-09-2011, 08:32 PM #8
Member
- Join Date
- Aug 2011
- Location
- West Virginia
- Posts
- 38
- Rep Power
- 0
something like this?
Java Code:public static void main(String[] args) { string = new StringApplication(); string.GetInput; string.checkStringContains; new StringApplication().checkStringContains(); } }
- 08-09-2011, 09:43 PM #9
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Yes. More particularly you are calling the indexOf() method of mainString, but that variable is null so there are no methods to call.is the error coming because I am trying to check two "null" values?
Sorry to repeat myself but where - on what line - did you think you gave mainString a nonnull value? The point is to see why that didn't happen.
Your program has two mainString variables. That is, there are two lines that begin "String mainString = " which declare the variable. This is confusing at best. You can use the variable all over the place but declare it just once.
- 08-09-2011, 09:50 PM #10
Member
- Join Date
- Aug 2011
- Location
- West Virginia
- Posts
- 38
- Rep Power
- 0
Sorry, I am just a little confused with this assignment and I am still relatively new to java in general, so speak slowly

Anyway, I am declaring the mainString and secondaryString variables on lines 4 and 5... and I do not see where else I am declaring separate mainString and partner variables. Within the GetInput() on lines 8 and 9 is where I thought I was getting input :/
- 08-09-2011, 10:04 PM #11
Perhaps since there have been some changes made, you should post the current version of the program and the errors you are getting.
- 08-09-2011, 10:08 PM #12
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
The problem arises because in getInput() you declare another pair of variables. In particular you say
It's that little word "String" that does the damage. (and its partner on the next line). What it does is declare a different mainString variable to which you assign a value. The rest of the class will use the other mainString variable declared earlier which remains null.Java Code:public void GetInput() { [color=red]String[/color] mainString = JOptionPane.showInputDialog("Enter a String"); // etc
- 08-09-2011, 10:28 PM #13
Member
- Join Date
- Aug 2011
- Location
- West Virginia
- Posts
- 38
- Rep Power
- 0
Here is my complete updated code, perhaps it will shed some new light on the issue:
the problem is with calling the checkStringContains and GetInput... and the variables involved. But I am just massively confusedJava Code:import javax.swing.*; import javax.swing.JOptionPane; //Main class public class StringApplication { // Variables private String mainString; private String secondaryString; private int idex1; private String string; // JOptionPane Input class public void GetInput() { mainString = JOptionPane.showInputDialog("Enter a String"); secondaryString = JOptionPane.showInputDialog("Enter Another String"); } // use indexOf method to compare strings public void checkStringContains() { System.out.println("About to check \"" + mainString + "\" with \"" + secondaryString + "\""); int index1 = mainString.indexOf(secondaryString); if (index1 != -1) System.out.println("The string contains the substring " + secondaryString); else System.out.println("The string does not contain the substring " + secondaryString); } // main method public static void main(String[] args) { string = new StringApplication(); string.GetInput(); string.checkStringContains(); new StringApplication().checkStringContains(); } }
here are the compilation errors:
C:\Users\broo7198\Documents\StringApplication.java :39: non-static variable string cannot be referenced from a static context
string = new StringApplication();
^
C:\Users\broo7198\Documents\StringApplication.java :39: incompatible types
found : StringApplication
required: java.lang.String
string = new StringApplication();
^
C:\Users\broo7198\Documents\StringApplication.java :41: non-static variable string cannot be referenced from a static context
string.GetInput();
^
C:\Users\broo7198\Documents\StringApplication.java :41: cannot find symbol
symbol : method GetInput()
location: class java.lang.String
string.GetInput();
^
C:\Users\broo7198\Documents\StringApplication.java :42: non-static variable string cannot be referenced from a static context
string.checkStringContains();
^
C:\Users\broo7198\Documents\StringApplication.java :42: cannot find symbol
symbol : method checkStringContains()
location: class java.lang.String
string.checkStringContains();
^
6 errors
Tool completed with exit code 1
- 08-09-2011, 10:41 PM #14
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Looks like a quite different problem (ie nothing to do with dereferencing the null variable.)perhaps it will shed some new light on the issue:
So what is the first line with the error - "string = new StringApplication();" - supposed to do? Why is it there?
[Edit] In fact, since you declare string to be a String (near the top of the class), none of the first three lines of main() make any sense. Try removing them.Last edited by pbrockway2; 08-09-2011 at 10:47 PM.
- 08-09-2011, 10:48 PM #15
Member
- Join Date
- Aug 2011
- Location
- West Virginia
- Posts
- 38
- Rep Power
- 0
well it was my attempt at trying to figure out what Tolls was getting at earlier
The above is telling Java to create a new instance of StringApplication.
Instead of just calling the getInput() on that, you assign it to a StringApplication variable, and call it (and the other method) on the variable.
After I remove those 3 unecessary lines I get no Input boxes and no output .. so I need to figure out how to initialize the input boxes, and then have that data output .Last edited by broo7198; 08-09-2011 at 10:59 PM.
- 08-09-2011, 11:28 PM #16
Member
- Join Date
- Aug 2011
- Location
- West Virginia
- Posts
- 38
- Rep Power
- 0
I feel like the solution is painfully easy and I just am overlooking it or cannot think of it! Any other suggestions?
- 08-09-2011, 11:45 PM #17
One confusion with reading your code is to have a variable named string that is not a String. It is a StringApplication. If you were to rename that variable to stringApp then you might not define it as a String.
One other observation: you create two StringApplication objects in the main() method. Why not just create one and use it for all the method calls?
Does the new variable: stringApp (was string) need to be known outside of the main() method?
- 08-09-2011, 11:48 PM #18
Member
- Join Date
- Aug 2011
- Location
- West Virginia
- Posts
- 38
- Rep Power
- 0
- 08-09-2011, 11:54 PM #19
Also consider this:
Does the new variable: stringApp (was string) need to be known outside of the main() method?
If not you can define it in the main() method vs as a class variable.
If it is a class variable, main can not see it without an instance of the class. You'll get:
non-static variable string cannot be referenced from a static context
- 08-09-2011, 11:57 PM #20
Member
- Join Date
- Aug 2011
- Location
- West Virginia
- Posts
- 38
- Rep Power
- 0
Similar Threads
-
Comparing Strings
By Adomini in forum New To JavaReplies: 3Last Post: 02-17-2011, 12:20 AM -
need in help in comparing Strings
By jaq in forum New To JavaReplies: 1Last Post: 11-25-2009, 01:06 PM -
comparing strings
By diggitydoggz in forum New To JavaReplies: 7Last Post: 12-23-2008, 04:40 AM -
Comparing Strings
By souFrag in forum Advanced JavaReplies: 5Last Post: 05-21-2008, 09:03 AM -
Comparing Strings
By Java Tip in forum Java TipReplies: 0Last Post: 12-03-2007, 09:44 AM


2Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks