Welcome to Dream.In.Code
Become a Java Expert!

Join 150,424 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,083 people online right now. Registration is fast and FREE... Join Now!




Help with code

 
Reply to this topicStart new topic

Help with code

dwd3773
17 Mar, 2008 - 01:10 PM
Post #1

D.I.C Head
**

Joined: 9 Feb, 2008
Posts: 103



Thanked: 1 times
My Contributions
I'm not sure how to reference a section of a string. For example if i wanted to use a certain part of the word like the market in the word Supermarket? I would write the code I already I have but I only have the class and the main set up not much else.
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Help With Code
17 Mar, 2008 - 01:18 PM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,660



Thanked: 314 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
If you have the word stored in a String variable type then you can call a function called substring() which can return the characters or word starting at a beginning index or a beginning and ending index. Below is a link to the Java documentation which has some examples of this.

Keep in mind when you look at the examples that a String is an series of characters where if the String is SuperMarket then the "S" is at index 0, "u" is at index 1, "p" is at index 2 etc. This is the index they are talking about.

Java substring() function for String variables

Hope this is what you were looking for. Enjoy! smile.gif
User is offlineProfile CardPM
+Quote Post

dwd3773
RE: Help With Code
17 Mar, 2008 - 01:45 PM
Post #3

D.I.C Head
**

Joined: 9 Feb, 2008
Posts: 103



Thanked: 1 times
My Contributions
Thank you for the tutorial it really helped me out. Here is what I've come up with so far;
CODE

public class Assign7
{
    public static void main(String[] args)
    {
        String s1 = "George ";
        String s2 = "Meghabghab";
        String s3 = s1.concat(s2);
        String s4 = s2.substring(2, 6);
        String s5 = s2.substring(0,2);
        String s6 = s5.concat(s4).concat(s4);
        boolean bool1;
        String s7 = s1.substring(0,1).concat(s2.substring(0,1));



        System.out.println(s3);
        System.out.println(s4);
        System.out.println(s5);
        System.out.println(s6);
        System.out.println(s7);
    }
}


But I've come up with another a problem, I need to test and print t see if s2 and s6 are equal. I have to use a boolean variable called bool1. I'm not really sure what I should use to do this or how to start it.
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Help With Code
17 Mar, 2008 - 01:52 PM
Post #4

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,660



Thanked: 314 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
String variables also have an inherited function called "equals" which takes another string to compare against. It returns a boolean value which you can then store in your bool1. Here is your project modified...

java

public class Assign7
{
public static void main(String[] args)
{
String s1 = "George ";
String s2 = "Meghabghab";
String s3 = s1.concat(s2);
String s4 = s2.substring(2, 6);
String s5 = s2.substring(0,2);
String s6 = s5.concat(s4).concat(s4);

// equals function of a string returns a boolean
boolean bool1 = s2.equals(s6);

// If true, print they are the same.
if (bool1) { System.out.println("Yes s2 and s6 are equal"); }

String s7 = s1.substring(0,1).concat(s2.substring(0,1));



System.out.println(s3);
System.out.println(s4);
System.out.println(s5);
System.out.println(s6);
System.out.println(s7);
}
}


That should explain it all. Enjoy!

"At DIC we be string comparing and equalizing code ninjas!" decap.gif

This post has been edited by Martyr2: 17 Mar, 2008 - 01:52 PM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 08:41PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month