Results 1 to 10 of 10
  1. #1
    gbonecapone is offline Member
    Join Date
    May 2013
    Posts
    68
    Blog Entries
    1
    Rep Power
    0

    Default Searching in an array

    here is the contents of my array

    "12 C4-END MILL-4 LTG/6 FLUTE 1.25"
    is it possible to search the string for just the "1.25" at the end WITHOUT knowing the length or value of the number? can i put a symbol infront of the 1.25 like * and copy just the text that comes after the * and save it to a variable?
    The thing is I have set the array to a specific line in a text file but i need to retrieve the 1.25, cutter diameter, from the text. just wondering if theres a simple built in method to do so thanks.

  2. #2
    gbonecapone is offline Member
    Join Date
    May 2013
    Posts
    68
    Blog Entries
    1
    Rep Power
    0

    Default Re: Searching in an array

    or maybe by the position of where the value starts in the string? i can set all the values in the text file to say the 40th character in their line.

  3. #3
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,839
    Rep Power
    8

    Default Re: Searching in an array

    That's not an array. It's a String. Did you mean something else?

    You should provide an SSCCE that demonstrates exactly what you're doing in as few lines as possible.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  4. #4
    gbonecapone is offline Member
    Join Date
    May 2013
    Posts
    68
    Blog Entries
    1
    Rep Power
    0

    Default Re: Searching in an array

    thats the contents of my array after i already received it from my text file.


    Java Code:
     public class readfile {
        private Scanner x;
        String records[] = new String[49];
            public void openFile(){
                try{
                x = new Scanner(new File("N:/USERS-Engineers/Gsudicky/program look/Inquiry2.txt"));
                        }
                catch (Exception w){
                    System.out.println("could not find file");
                
                }
           
            }
         public String readFile(int b){
              int p=1;
             while(x.hasNextLine()){
                
             records[p]= x.nextLine();
                
                 System.out.println(records[p]);
    
                     p=p+1;      
             }
          
             return records[b];
         }
         public void closeFile(){
         x.close();}
        }
    records[b] prints returns that string for other purposes but i need to retrieve the value 1.25 out of it and set it to a variable. thanks for looking

  5. #5
    gbonecapone is offline Member
    Join Date
    May 2013
    Posts
    68
    Blog Entries
    1
    Rep Power
    0

    Default Re: Searching in an array

    sorry yes String records[];

  6. #6
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,839
    Rep Power
    8

    Default Re: Searching in an array

    I'm still not totally sure what your question is, whether it's about searching through a String or an array. An SSCCE would help clear that up.

    If you're trying to look through a String, you might want to check out the String and Pattern classes in the API: Java Platform SE 7
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

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

    Default Re: Searching in an array

    You could search for the last space character in the String; have a look at the lastIndexOf( ... ) method.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    gbonecapone is offline Member
    Join Date
    May 2013
    Posts
    68
    Blog Entries
    1
    Rep Power
    0

    Default Re: Searching in an array

    ok i got the lastIndexOf method to return me 39. how can i save everything after that as a string. is there another method for that? thanks for your help.

  9. #9
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,338
    Rep Power
    5

    Default Re: Searching in an array

    If k is your index and str is your String:

    String substr = str.substring(k); // all characters starting at k.

    I suggest you read the JDK API for String and StringBuilder. Just knowing what those classes can do may save you some time in the future. And if you are going to parse alphanumeric input, you may want to look at java.util.regex.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  10. #10
    gbonecapone is offline Member
    Join Date
    May 2013
    Posts
    68
    Blog Entries
    1
    Rep Power
    0

Similar Threads

  1. help with Array searching
    By kamikazii in forum New To Java
    Replies: 2
    Last Post: 01-30-2013, 10:02 AM
  2. Replies: 3
    Last Post: 11-25-2011, 12:38 AM
  3. Searching through Array of Objects
    By coopc in forum New To Java
    Replies: 6
    Last Post: 04-26-2011, 03:32 PM
  4. Replies: 3
    Last Post: 03-15-2011, 12:24 PM
  5. Searching In a String Array - Problem
    By DillMan in forum New To Java
    Replies: 4
    Last Post: 12-07-2008, 09:12 PM

Tags for this Thread

Posting Permissions

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