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: Substring function

  1. #1
    Junior Member
    Join Date
    Nov 2009
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Substring function

    Ok so i need to make a function that using the same things as the substing method.
    Here the problem:
    The substring command prompts the user for two integer indexes. It then replaces the result string with the set of characters in the result string that includes the character at the starting index up to the end index.

    Constraint: We cant use the substring method in the Java String Class and we have to use a for loop to do this.

    I have this so far.
    public String substring(String result)
    {
    Scanner scan = new Scanner(System.in);
    System.out.println("Enter inital> ");
    int inital = scan.nextInt();
    System.out.println("Enter end> ");
    int end= scan.nextInt();
    String[] c= new String[result.length()];///creates an array 
    String newresult="";//makes a new string to hold the new result 
    char letter = 0;
    for(int i = 0; i<c.length; i++)//
    	{
    		letter=result.charAt(i);//gets the index of the chara
    		c[i]=String.valueOf(letter);//get the chara of the index
    		if(letter>=inital && letter<=end)//if any of the chara in the array equals the chara that is to replaced then
    		{
    				newresult+=c[i];//changes the chara
    		}
     
    	}
    	return newresult;
    }
    It compiles but im not getting anything....
    Last edited by helloworld922; November 12th, 2009 at 10:09 AM.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Substring function

    You don't have a main method. I don't know how you're going to get the initial string, but I'd imagine maybe reading it in from the user.

    public static void main(String[] args)
    {
    	Scanner scan = new Scanner(System.in);
    	System.out.println("Enter a string: ");
    	System.out.println(substring(scan.nextLine()));
    }

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,327
    Thanks
    181
    Thanked 828 Times in 771 Posts
    Blog Entries
    5

    Default Re: Substring function

    		if(letter>=inital && letter<=end)//if any of the chara in the array equals the chara that is to replaced then
    		{
    				newresult+=c[i];//changes the chara
    		}

    In the above code, you are comparing a character to an integer, rather than an int to an int (the result is a comparison of the char ascii value to the int). You should rather do the comparison to 'i' rather than letter. An easier way overall is to create a char array of the length of the substring, then fill in the array by iterating through the parameter string starting at the initial and ending at the end, returning a new string created with the character array

Similar Threads

  1. First string is a substring of another
    By mgutierrez19 in forum What's Wrong With My Code?
    Replies: 13
    Last Post: October 21st, 2009, 10:35 PM
  2. Substring program, not working
    By Newoor in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 18th, 2009, 12:46 PM
  3. Function of difference between two numbers
    By uplink600 in forum Java Theory & Questions
    Replies: 2
    Last Post: May 13th, 2009, 05:57 AM
  4. Traverse Function for Knights tour
    By budder8818 in forum Java Theory & Questions
    Replies: 0
    Last Post: February 4th, 2009, 02:49 PM
  5. Replies: 5
    Last Post: January 30th, 2009, 08:31 PM