Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-09-2012, 05:13 PM   PM User | #1
YA123
New to the CF scene

 
Join Date: Sep 2012
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
YA123 is an unknown quantity at this point
Help with basic java project

Enter a long string: My name is YA123
Enter a substring: name
Length of your string:14
Length of your substring:4
Starting position of your substring in string: 4
String before your substring: My
String after your substring: is YA123
Enter a position between 0 and 13: 5
The character at position : a
Enter a replacement string: username
Your new string is: My username is YA123

I need help with this code which probably looks very basic for some of you, but I'm struggling right now.

I have up to length of the substring, as shown in the following code.
Code:
	Scanner keyboard = new Scanner(System.in);
		System.out.print("Enter a long string: ");
		String longSt = keyboard.nextLine();
		System.out.println("Enter a substring: ");
		String substring = keyboard.nextLine();
		int longlength = longSt.length();
		System.out.println("Length of your string: " + longlength);
		int sublength = substring.length();
		System.out.println("Length of your substring: " + sublength);
Can someone please help me, I'm stuck because i don't know how to program finding characters of strings that the user creates.
YA123 is offline   Reply With Quote
Old 09-09-2012, 07:41 PM   PM User | #2
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
You are more likely to obtain a response if you post in the correct forum - this is nothing to do with JavaScript (or DOM and JSON scripting). I assume it's Java.
__________________
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
Validate your HTML and CSS
AndrewGSW is offline   Reply With Quote
Old 09-09-2012, 07:59 PM   PM User | #3
YA123
New to the CF scene

 
Join Date: Sep 2012
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
YA123 is an unknown quantity at this point
Douh! I'm sorry thank you very much.
YA123 is offline   Reply With Quote
Old 09-10-2012, 03:05 PM   PM User | #4
VIPStephan
The fat guy next door


 
VIPStephan's Avatar
 
Join Date: Jan 2006
Location: Halle (Saale), Germany
Posts: 7,614
Thanks: 5
Thanked 865 Times in 842 Posts
VIPStephan is a jewel in the roughVIPStephan is a jewel in the roughVIPStephan is a jewel in the rough
Refrain from re-posting a thread, rather, ask a moderator for it to be moved accordingly (which I did by myself now).
__________________
Don’t click this link!
VIPStephan is offline   Reply With Quote
Old 09-10-2012, 05:33 PM   PM User | #5
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,661
Thanks: 4
Thanked 2,452 Times in 2,421 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Simply use indexOf on string. It accepts a string as well:
PHP Code:
        System.out.println("Starting position of your substring in string: " longSt.indexOf(substring)); 
Note in this example you have here, the starting position is 3, not four. But that differs from the starting index versus the position within the string. Arrays are 0 based in java, so that would indicate that the character array for this string starts its substring in position 3, which is the fourth character of the string.

Once you get the index you can use that as a part of the substring method to determine start and end positions (with length's involved).
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Tags
basic, java, project

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:16 PM.


Advertisement
Log in to turn off these ads.