Page 1 of 2 12 LastLast
Results 1 to 20 of 31
Like Tree2Likes

Thread: Comparing Strings Application

  1. #1
    broo7198 is offline Member
    Join Date
    Aug 2011
    Location
    West Virginia
    Posts
    38
    Rep Power
    0

    Default 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();
    
        }
    }

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,776
    Blog Entries
    7
    Rep Power
    21

    Default

    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,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    Have a look at what you are checking:

    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
    }
    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.

  4. #4
    broo7198 is offline Member
    Join Date
    Aug 2011
    Location
    West Virginia
    Posts
    38
    Rep Power
    0

    Default

    Quote Originally Posted by pbrockway2 View Post
    Have a look at what you are checking:

    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
    }
    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.
    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 08:59 PM.

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

    Default

    Java Code:
    		new StringApplication().GetInput();
    
            new StringApplication().checkStringContains();
    Those are two completey different objects you are calling those methods on, each with their own mainString and secondaryString attributes.
    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.

  6. #6
    broo7198 is offline Member
    Join Date
    Aug 2011
    Location
    West Virginia
    Posts
    38
    Rep Power
    0

    Default

    I dont understand what you mean by assign "new StringApplication().GetInput();" to a variable?
    Last edited by broo7198; 08-09-2011 at 09:17 PM.

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

    Default

    Java Code:
    new StringApplication();
    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.

  8. #8
    broo7198 is offline Member
    Join Date
    Aug 2011
    Location
    West Virginia
    Posts
    38
    Rep Power
    0

    Default

    something like this?

    Java Code:
    public static void main(String[] args) {
    
    			string = new StringApplication();
    
    			string.GetInput;
    			string.checkStringContains;
    
    
    
    
            new StringApplication().checkStringContains();
    
        }
    }

  9. #9
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    is the error coming because I am trying to check two "null" values?
    Yes. More particularly you are calling the indexOf() method of mainString, but that variable is null so there are no methods to call.

    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.

  10. #10
    broo7198 is offline Member
    Join Date
    Aug 2011
    Location
    West Virginia
    Posts
    38
    Rep Power
    0

    Default

    Quote Originally Posted by pbrockway2 View Post
    Yes. More particularly you are calling the indexOf() method of mainString, but that variable is null so there are no methods to call.

    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.
    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 :/

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default

    Perhaps since there have been some changes made, you should post the current version of the program and the errors you are getting.

  12. #12
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    The problem arises because in getInput() you declare another pair of variables. In particular you say

    Java Code:
    public void GetInput() {
    
        [color=red]String[/color] mainString = JOptionPane.showInputDialog("Enter a String");
    
        // etc
    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.

  13. #13
    broo7198 is offline Member
    Join Date
    Aug 2011
    Location
    West Virginia
    Posts
    38
    Rep Power
    0

    Default

    Quote Originally Posted by pbrockway2 View Post
    The problem arises because in getInput() you declare another pair of variables. In particular you say

    Java Code:
    public void GetInput() {
    
        [color=red]String[/color] mainString = JOptionPane.showInputDialog("Enter a String");
    
        // etc
    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.

    Here is my complete updated code, perhaps it will shed some new light on the issue:

    Java 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();
    
        }
    }
    the problem is with calling the checkStringContains and GetInput... and the variables involved. But I am just massively confused


    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

  14. #14
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    perhaps it will shed some new light on the issue:
    Looks like a quite different problem (ie nothing to do with dereferencing the null variable.)

    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 11:47 PM.

  15. #15
    broo7198 is offline Member
    Join Date
    Aug 2011
    Location
    West Virginia
    Posts
    38
    Rep Power
    0

    Default

    Quote Originally Posted by pbrockway2 View Post
    Looks like a quite different problem (ie nothing to do with dereferencing the null variable.)

    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.
    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 11:59 PM.

  16. #16
    broo7198 is offline Member
    Join Date
    Aug 2011
    Location
    West Virginia
    Posts
    38
    Rep Power
    0

    Default

    I feel like the solution is painfully easy and I just am overlooking it or cannot think of it! Any other suggestions?

  17. #17
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default

    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?

  18. #18
    broo7198 is offline Member
    Join Date
    Aug 2011
    Location
    West Virginia
    Posts
    38
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    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?
    so something like this?

    Java Code:
     public static void main(String[] args) {
    
    
    		 stringApp = new StringApplication();
    
    		 stringApp.GetInput();
    		 stringApp.checkStringContains();

  19. #19
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default

    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

  20. #20
    broo7198 is offline Member
    Join Date
    Aug 2011
    Location
    West Virginia
    Posts
    38
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    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:
    I believe it would need to be listed under the class.. but what type of variable is stringApp to be?

Page 1 of 2 12 LastLast

Similar Threads

  1. Comparing Strings
    By Adomini in forum New To Java
    Replies: 3
    Last Post: 02-17-2011, 01:20 AM
  2. need in help in comparing Strings
    By jaq in forum New To Java
    Replies: 1
    Last Post: 11-25-2009, 02:06 PM
  3. comparing strings
    By diggitydoggz in forum New To Java
    Replies: 7
    Last Post: 12-23-2008, 05:40 AM
  4. Comparing Strings
    By souFrag in forum Advanced Java
    Replies: 5
    Last Post: 05-21-2008, 10:03 AM
  5. Comparing Strings
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-03-2007, 10:44 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
  •