Results 1 to 7 of 7

Thread: String in java

  1. #1
    mockingbird is offline Member
    Join Date
    May 2013
    Posts
    3
    Rep Power
    0

    Default String in java

    Hello.
    I have a task and I need programming a small program like this;

    The program will count in a string text area how many used the same word.
    Example text

    " A dog run out of home.When dog come back home he saw that there is another dog in home."

    In this text how many time word "dog" is used I have to count.But I dont know how can I make this program.I just think that I have to define a variable as this text value.And more I dont know what to do.

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

    Default Re: String in java

    Read the API documentation for the String class and pay special attention to the indexOf( ... ) methods.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Kagiso is offline Member
    Join Date
    Nov 2012
    Location
    Johannesburg, South Africa
    Posts
    92
    Rep Power
    0

    Default Re: String in java

    Just one word for you... regular expressions aka "Regex"

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

    Default Re: String in java

    Quote Originally Posted by Kagiso View Post
    Just one word for you... regular expressions aka "Regex"
    Regular languages (expressions) can't count. They're not much help here ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    mockingbird is offline Member
    Join Date
    May 2013
    Posts
    3
    Rep Power
    0

    Default Re: String in java

    Quote Originally Posted by JosAH View Post
    Regular languages (expressions) can't count. They're not much help here ...

    kind regards,

    Jos
    Finally I did thanks both of u.But Kagiso said true this Regex is works well on it.

  6. #6
    teja is offline Member
    Join Date
    May 2013
    Posts
    3
    Rep Power
    0

    Default Re: String in java

    This is my code to implement stack using arrays!!But i am getting an exception array out of bounds for push() method...help me wid dis!!
    import java.io.*;
    import java.util.*;
    public class Stack {
    int item,top=-1;
    int a[]=new int[item];
    public void push()
    {


    top++;
    System.out.println("enter the item");
    Scanner sc1=new Scanner(System.in);
    item=sc1.nextInt();
    a[top]=item;
    System.out.println("push operation performed");
    }
    public int pop()
    {

    item=a[top];
    top--;
    return item;
    }
    public void display()
    {

    for(int i=top;i>=0;i--)
    {
    System.out.println(i);
    }
    }

    public static void main(String args[])throws IOException
    {


    int ch;
    String che;
    Stack s1=new Stack();
    Stack s2=new Stack();
    Stack s3=new Stack();
    do{
    System.out.println("1.push 2.pop 3.display \n Enter your choice");
    Scanner sc2=new Scanner(System.in);
    ch=sc2.nextInt();
    switch(ch)
    {
    case 1:
    s1.push();
    break;
    case 2:
    s2.pop();
    break;
    case 3:
    s3.display();
    break;
    }
    System.out.println("continue y or n");
    Scanner sc3=new Scanner(System.in);
    che=sc3.next();
    sc3.close();
    }
    while(che=="y"||che=="Y");
    }

    }

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

    Default Re: String in java

    teja:
    Continue in your own thread here.
    Take note of what I said in your other thread hijack here.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Replies: 12
    Last Post: 09-23-2011, 02:25 PM
  2. Replies: 3
    Last Post: 05-02-2011, 08:51 AM
  3. Replies: 4
    Last Post: 01-01-2011, 11:52 AM
  4. Replies: 2
    Last Post: 09-18-2008, 08:30 AM
  5. Replies: 0
    Last Post: 11-20-2007, 05:59 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
  •