Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: lastIndexOff

  1. #1
    Member
    Join Date
    Sep 2012
    Location
    The Netherlands
    Posts
    84
    My Mood
    Inspired
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default lastIndexOff

    Hi everyone,

    I almost have my program complete, at the moment I am working on the last part of this program.

    I have in my program an option for the user to select a output file through a GUI screen.
    Now I would like the program to look if there is a certain extension behind the file. (.xlsx)
    If yes: continue.
    else: put .xlsx behid it.

    Now I was thinking of using lastIndexOf() as I have used indexOf somewhere else with succes.
    But I can't get the lastIndexOf to start working, becasue of the folowing error:

    outputFileName1 cannot be resolved to a type

    outputFileName1 = defined as a String
    String outputFileName1 = "";

    Now I looked it up and the input needs to be a string.
    string.lastIndexOf(".xlsx")

    Now this string.lastIndexOf(".xlsx") works.
    But not when I put it inside an if loop.

    Is it not usable in a if loop or something?

  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: lastIndexOff

    outputFileName1.lastIndexOf(".xlsx") should be fine in a loop or not. If you have a compiler message and can't see how to fix it, post the whole mesage and the code to which it refers.

    ---

    I suspect you might find endsWith() a better method to use. It could be the case that .xlsx occurs, but not at the end like foo.xlsx.tar.gz and in that case you may not want to merely continue.

  3. #3
    Member
    Join Date
    Sep 2012
    Location
    The Netherlands
    Posts
    84
    My Mood
    Inspired
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default Re: lastIndexOff

    I figured it out.
    lastIndexOf returns an int in my code.
    So now I changed something and now it does work. :-))

    Now I am happy, finished program one completly.