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 6 of 6

Thread: question about indexes

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default question about indexes

    So I was wondering if it is possible to stores indexes as variables. I have a question where the user inputs a string such as the fox [who is red] must have left earlier.
    The problem is to return only what is in the [] including the brackets and the second question is to get the length of that part. Does anyone know how to do this? I was figuring if I could store the indexOf [ and the lastIndexOf ] stored those values as variables and then used them in a substring method (a,b) but I don't know how to do that or if it's even possible. Thanks


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: question about indexes

    Of course you know how to store values in variables. Start writing the code and hopefully your "block" about storing a value will disappear as you write. Come back with your code when you need help.

  3. #3
    Junior Member
    Join Date
    Sep 2013
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: question about indexes

    import java.util.Scanner;

    public class Help
    {
    public static void main(String [] args)
    {
    Scanner in = new Scanner(System.in);
    String abc;
    int ind1, ind2;

    System.out.println("Enter a sentence with a substring in [] brackets: ");
    abc = in.nextLine();

    ind1 = int abc.indexOf([);



    }
    }
    I'm not sure how to set up the variable with the proper syntax

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: question about indexes

    Please post your code in code tags.

    Sometimes you try something and it doesn't work, so you try something else. Sometimes you read the documentation and increase the chances that the first thing you try will work.

    Refer to the String API, and note that the method String.indexOf() returns an int. Knowing that, you can use correct syntax, like:
    int leftBraceIndex = userString.indexOf( '[' );

  5. #5
    Junior Member
    Join Date
    Sep 2013
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: question about indexes

    Thanks

  6. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: question about indexes

    You're welcome.

Similar Threads

  1. Returning an array of indexes in my program.
    By Spanky_10 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: April 16th, 2013, 03:21 PM
  2. Creating Objects at specified indexes
    By dougie1809 in forum Object Oriented Programming
    Replies: 4
    Last Post: March 1st, 2013, 08:20 PM
  3. How to find indexes of ALL occurrences of object in ArrayList
    By nafty in forum Collections and Generics
    Replies: 5
    Last Post: December 23rd, 2012, 02:02 AM
  4. Assgining values to array indexes
    By chronoz13 in forum Collections and Generics
    Replies: 3
    Last Post: December 28th, 2009, 11:09 PM