indexOf, substring questions


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: indexOf, substring questions

  1. #1
    Join Date
    Jan 2005
    Posts
    45

    indexOf, substring questions

    Hi

    Can someone please explain to me what this does

    public Account(String info) {
    owner = info.substring(0, info.indexOf(","));
    info = info.substring(2 + info.indexOf(","));
    balance = Double.parseDouble(info.substring(0, info.indexOf(",")));
    info = info.substring(2 + info.indexOf(","));
    minBalance = Double.parseDouble(info);
    }

    What will happen if we pass something like

    Account account = new Account("Mike, 2100.0, 1000.0");

    Why do we have 2+... here? Thanks a lot.

  2. #2
    Join Date
    Sep 2004
    Posts
    223
    info.indexOf will return a number where the first occurance of the given character is found.

    ie. info.indexOf(",") will return the charater position of a comma in the string "info"

    so if info was "i like, the wind"

    info.indexOf(",") would return 7.

    therefore, 2 + info.indexOf(",") would equal 9.

    heres a link to all of the different types of indexOf functions:

    http://java.sun.com/j2se/1.4.2/docs/...ng/String.html
    A kram a day keeps the doctor......guessing

  3. #3
    Join Date
    Feb 2004
    Posts
    808

    Re: indexOf, substring questions

    Originally posted by scracker
    Hi

    Can someone please explain to me what this does

    public Account(String info) {
    owner = info.substring(0, info.indexOf(","));
    info = info.substring(2 + info.indexOf(","));
    balance = Double.parseDouble(info.substring(0, info.indexOf(",")));
    info = info.substring(2 + info.indexOf(","));
    minBalance = Double.parseDouble(info);
    }


    it appears to be An Excercise In Making Life Hard Work For Ones-self.

    info.split(", "); would have sufficed for chopping the string up

    What will happen if we pass something like

    Account account = new Account("Mike, 2100.0, 1000.0");
    aside from that being a bit of a strange (dumb) way of passing variables, when a Account(String, double, double) constructor would have sufficed, the string will be split around the commas

    Why do we have 2+... here? Thanks a lot.
    indexOf the comma gets you the index of the comma
    hence, the index + 1 is the space next to the comma
    and index+2 is the start of the next word after the comma

    like i said, info.split(", ") would have been my choice
    The 6th edict:
    "A thing of reference thing can hold either a null thing or a thing to any thing whose thing is assignment compatible with the thing of the thing" - ArchAngel, www.dictionary.com et al.
    JAR tutorial GridBag tutorial Inherited Shapes Inheritance? String.split(); FTP?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center