Page 1 of 2 12 LastLast
Results 1 to 20 of 25
  1. #1
    </3java is offline Member
    Join Date
    Feb 2011
    Posts
    15
    Rep Power
    0

    Default Need help with Strings

    This is pretty urgent, but I basically need a few small methods to be made and have been working for several hours just trying to see where to start. Id rather keep it private, so if anyone can help me id be more than happy to donate money via paypal to you.

    Thanks.

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    How about just asking the question and we will push you in the right direction and you can save yourself some money.

  3. #3
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Wink

    Quote Originally Posted by </3java View Post
    Id rather keep it private,
    Keep it in your safe and make sure that you have the keys in right place.

    Quote Originally Posted by </3java View Post
    so if anyone can help me id be more than happy to donate money via paypal to you.
    There is a way to save your money. Show us what you have done so far in your "secret" mission. Tell us where have you stuck up and we would be more than happy to help you out via this forum.

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  4. #4
    </3java is offline Member
    Join Date
    Feb 2011
    Posts
    15
    Rep Power
    0

    Default

    Alright, that would be cool.

    So basically, our assignment is to produce four mini methods (I wouldnt call them mini, but some might).

    So ill start with the first one. The method itself is called "isLike". Here is what the method does:

    Returns true if all characters in input1 are found in input2.

    So basically, I believe this is what it is asking:

    input1 = "hello"
    input2 = "oellh igsajigagasgag"

    It will return "true".

    Here is my code so far:

    Java Code:
    public class YO {
    
    	public static boolean isLike(String input1, String input2) {
    		boolean found = true;
    		
    		// check every character in input1
    		for (int i=0;i<input1.length(); i++) {
    			if (input2.indexOf(input1.charAt(i))<0){
    				found = false;
    			}
    		}
    		return (found); // outputs true or false
    	}
    
    	
    	
    	// main
    	public static void main(String args[]) {
    		// Test your methods HERE.	
    
    		
    	}
    
    }
    Under main I tried doing like "string input1 = "hey" ", but it was saying that input1 is never used. Is that code above right? What goes in main to test it? Thanks a lot.

    I tried this:

    java.util.Scanner console ; // Java console Scanner
    console = new java.util.Scanner(System.in);

    isLike("hey","hello");

  5. #5
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Wink

    The method that compares two Strings is a static method here, so you can call it directly from main. And as you see it returns a boolean, so you need to store the result in a boolean and confirm whether your method is doing the job or not.

    Java Code:
    boolean x = isLike("hello", "hello world");
    Then print the boolean and see what was the result of your comparison.

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  6. #6
    javaforum$ is offline Member
    Join Date
    Feb 2011
    Posts
    7
    Rep Power
    0

    Default Use 'break;' to save computation

    public static boolean isLike(String input1, String input2) {
    boolean found = true;

    // check every character in input1
    for (int i=0;i<input1.length(); i++) {
    if (input2.indexOf(input1.charAt(i))<0){
    found = false;
    break;
    }
    }
    return (found); // outputs true or false
    }

    This saves the computation time, rather than continue the loop even when first char of input1 doesn't available in input2.

  7. #7
    </3java is offline Member
    Join Date
    Feb 2011
    Posts
    15
    Rep Power
    0

    Default

    Perfect, just tried my code with different inputs for input1/input2 and it looks like it works.

    Im gonna try the second part for like 15 min and if I dont get it ill ask for some help. Thanks so far :)

    @javaforums$

    I dont know what a break is yet, I dont think this assignment right now is aimed for optimizing computation time, but I will look back at breaks later on.
    Last edited by </3java; 02-17-2011 at 05:38 AM.

  8. #8
    </3java is offline Member
    Join Date
    Feb 2011
    Posts
    15
    Rep Power
    0

    Default

    Alright, time to call in for some help :)

    our next method is called "isInteger". Before I go on, our teacher said that there already is like a system method that comes with java that can do this, but he wants us to write our own method.

    Basically, what we are trying to do is say if the input is an integer or not. Here are the exceptions and what-not:

    Java Code:
    Examples of "good" integer strings:
    "12345"
    00012"         allow leading zeros
    "  34   "       allow leading and trailing spaces
    
    Examples of "bad" integer strings:	
    "34*$D3456(9"   bad symbols
    "21.4"          bad symbols
    "87 619"        string should be one integer
    --

    So my thought pattern would be to create an if loop and have else if's to not accept bad symbols.

  9. #9
    bkim33 is offline Member
    Join Date
    Feb 2011
    Posts
    18
    Rep Power
    0

    Default

    hmm tried yours and it works if the strings are the same but it stil outputs true if the strings are different

  10. #10
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Are you allowed to use exceptions? It is probably the simplest method to use Integer.parseInt and exceptions.

  11. #11
    </3java is offline Member
    Join Date
    Feb 2011
    Posts
    15
    Rep Power
    0

    Default

    Quote Originally Posted by bkim33 View Post
    hmm tried yours and it works if the strings are the same but it stil outputs true if the strings are different
    You must be doing something wrong, I have tried it when the strings are different and it outputs true and false.

  12. #12
    </3java is offline Member
    Join Date
    Feb 2011
    Posts
    15
    Rep Power
    0

    Default

    I dont think he is allowing that, because he said there is already a method that does this for you, so I am guessing if its just that and exceptions, then we gotta write it out ourselves.

  13. #13
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    There was a thread about this earlier today. But the simplest solution would be to loop through the input and check to see if that char at the position is 0-9. if it finds something that isn't, return false immediately.

  14. #14
    </3java is offline Member
    Join Date
    Feb 2011
    Posts
    15
    Rep Power
    0

    Default

    What about spaces and stuff? Ill try that and we can include exceptions later, thanks.

  15. #15
    bkim33 is offline Member
    Join Date
    Feb 2011
    Posts
    18
    Rep Power
    0

    Default

    write a helper method that clears leading spaces then in the main call out isInteger(clearLeadingSpaces(" 23u2"))

  16. #16
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    There is a built in method for stirngs called trim which removed leading and ending whitespace.

  17. #17
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Wink

    Quote Originally Posted by sunde887 View Post
    There is a built in method for stirngs called trim which removed leading and ending whitespace.
    This would be the right thing to do before iterating over the input. Here is the API doc for String.trim()

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  18. #18
    </3java is offline Member
    Join Date
    Feb 2011
    Posts
    15
    Rep Power
    0

    Default

    We got a little method to remove leading spaces in class.

    Someone in the class and I have been teaming up, we got the isInteger to work except for a few exceptions: if there is a decimal (which we are considering to be an integer, idk why) and for trailing spaces (if input = "45 ").

  19. #19
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    decimals count because 42.56 is a number

  20. #20
    </3java is offline Member
    Join Date
    Feb 2011
    Posts
    15
    Rep Power
    0

    Default

    I realize its a number, but this method is called isInteger. An integer is a whole number, thats why its weird. Still, we gotta figure out a way to accept it.

    WAIT WAIT WAIT, I lied, im such an idiot. decimals are not included, haha.

Page 1 of 2 12 LastLast

Similar Threads

  1. Strings
    By zoala001 in forum Java Applets
    Replies: 9
    Last Post: 01-02-2011, 10:26 AM
  2. Strings and fonts
    By TheGoodGuy in forum New To Java
    Replies: 12
    Last Post: 09-05-2010, 02:01 AM
  3. It is possible in Strings..?
    By mlibot in forum New To Java
    Replies: 1
    Last Post: 03-12-2010, 05:30 AM
  4. Strings and delimeters
    By Manfizy in forum New To Java
    Replies: 12
    Last Post: 06-09-2009, 07:51 AM
  5. Reading in strings
    By thekermo in forum New To Java
    Replies: 2
    Last Post: 10-19-2008, 05:24 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •