Results 1 to 6 of 6

Thread: Find data in a string

  1. #1
    Join Date
    Dec 2009
    Posts
    178

    Find data in a string

    Hello,
    I am in a big problem with my code, here is what I am trying to do. I am trying to find a text in a string.
    here is my code:
    Code:
    Public void rcprmt(String word){
        for(int i =0; i <NB; i + +){
           if (stprd[i].rtdes().regionMatches(true,0word,0word.length())){
             System.out.System.out.println(word);
           }
        }
      }
    the string is: "This product from our store"
    and for testing
    Code:
    mag.rcprmt("Product");
    the output is an error. Can anyone help me please. Thanks in advance.

  2. #2
    Join Date
    Dec 2009
    Posts
    263

    Re: Find data in a string

    Hello,
    I am new to this and even I was trying something like this , I have code with me that is somewhat similar to this. If you need you can find it helpful.
    Here is the code:
    Code:
    Public Product(n String,double cpdouble pi, double num, String D) {
        name = n;
        PrixAchat = cp;
        PrixVente = pi;
        NbreExplStock = num;
        des = D;
      }
    / / method that returns me the des
    Public String returnDescription(){
    return des;
    }
     
    / / Method that adds a product in my picture
       Public void atpr(String name,double PrixAchat, double PrixVente, double num, String Desc) {
    if (NB! = StockProduit.length){
     StockProduit[NB] = new Product(name, PrixAchat, PrixVente, num, Desc);
    NB + +;
    }else{
    System.out.System.out.println("Error, size of the array too small");
       }
    }
     
     
    / / Method to search by keyword
      Public void rcprt(String word){
        for(int i =0; i <NB; i + +){
           if (StockProduit[i].returnDescription().regionMatches(true,0word,0word.length())){
             System.out.System.out.println(word);
           }
        }
      }
     
    / / and test
    
        Mag mag new Mag();
        mag.atpr("Eraser",3,5.2,3,"This product from our store");
        mag.rcprt("Product");

  3. #3
    Join Date
    Nov 2009
    Posts
    518

    Re: Find data in a string

    Hello,
    This is normal, you check whether "proceeds" is the beginning of "Product". Change "product" to "On", and I think you will return true in your method. In your place I would rather use String.match (RegExp). Or you can make a StringTokenizer to check for each token (each word) with equal "product." It's up to you to see. I think till now you know what exactly you have to do.

  4. #4
    Join Date
    Nov 2009
    Posts
    330

    Re: Find data in a string

    Hello,
    I have a part of code with me, just have a look at this
    Code:
    Public class Product {
     
      private String nm;
      private double pract;
      private double prvnt;
      private double nbxest;
      private String desc;
     
      Public Product(String Ndouble PAdouble PV) {
        nm = N;
        pract = PA;
        prvnt = PV;
        nbxest = 0;
        desc = "No desc";
      }
       Public void Shown(){
        System.out.System.out.println("Product Name: + Name + "\ n" +
                           "Purchase Price" + + pract "\ n" +
                           "Price" + + prvnt "\ n" +
                           "Quantity of copies in stock: + + nbxest "\ n" +
                           "Product Description: + Description);
      }
       Public Product(No String,double CAPdouble POI, double Number, String D) {
        nm = No;
        pract = CAP;
        prvnt = POI;
        nbxest = Number;
        desc = D;
      }

  5. #5
    Join Date
    Dec 2009
    Posts
    178

    Re: Find data in a string

    Hello,
    This is a part of my code , which I am trying to do, but it is not working.
    Code:
    Public class TestMag {
      Public static void hand(String[] args) {
        Mag mag new Mag();
        mag.ajprd("Pencil",6,8.3);
        mag.ajprd("Pen",7.0,8.94,2,"the mark of this pen is exceptional" );
        mag.ajprd("Eraser",3,5.2,4,"This product from our store");
    mag.rchprd("Eraser");
        mag.rchrcpmt("from");
      }
    }

  6. #6
    Join Date
    Nov 2009
    Posts
    356

    Re: Find data in a string

    Hello,
    Try the following code.
    Code:
    Public void rechercheParMot(String word){
      boolean fd = false; 
        for(int i =0; i <NB; i + +){
          String [] chstr = StockProduit[i].returnDescription().split(word);
          if (chstr.length > 1){
                 fd = true;
           }
        } if(fd) System.out.System.out.println(word);
      }

Similar Threads

  1. How to find a substring in a string?
    By Jason Voorhees in forum Software Development
    Replies: 3
    Last Post: 08-09-2012, 11:52 AM
  2. How to find string that doesn't start with empty space?
    By MACE in forum Software Development
    Replies: 4
    Last Post: 02-03-2010, 06:21 PM
  3. How to input variable in string.find method in C++
    By Caelaan in forum Software Development
    Replies: 5
    Last Post: 29-01-2010, 10:50 PM
  4. Find and Replace in JavaScript String
    By Wilbur in forum Software Development
    Replies: 5
    Last Post: 15-12-2009, 02:23 PM
  5. Find any file containing a string?
    By Calab in forum Vista Help
    Replies: 15
    Last Post: 06-11-2008, 03:56 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,711,651,162.66739 seconds with 17 queries