Results 1 to 20 of 48
- 08-22-2010, 04:27 AM #1
Member
- Join Date
- Aug 2010
- Posts
- 32
- Rep Power
- 0
Help needed with string manipulation
This is what i have so far: and im at a loss of what to do next. spent hours and hours so far and am getting over it. please post helpful meaning code with explanations as i dont know much and am new to this world of programming.
Thanks heaps.
String input;
String output;
char current;
System.out.println ("Enter a sentence:");
input = scan.nextLine();
current = input.charAt(0);
if (current == 'a')
output = output + current;
System.out.println ("+output");
}
}Last edited by stringkilla; 08-30-2010 at 08:37 AM.
- 08-22-2010, 04:57 AM #2
What exactly task you feel problematic?
Is there any error?
- 08-22-2010, 05:05 AM #3
Member
- Join Date
- Aug 2010
- Posts
- 32
- Rep Power
- 0
firstly,
output = output + current;
is causing error. i want to add each individual char to output string one at a time.
- 08-22-2010, 06:52 AM #4
When you get errors, please post the full error message.
In this case, the error is because output is not initialized; that is, you did not give it an initial value. Therefore, you cannot concatenate to it.
Try replacing this:
...with this:Java Code:String output;
This assignment looks quite fun actually, best of luck on it.Java Code:String output = "";
- 08-22-2010, 07:20 AM #5
Member
- Join Date
- Aug 2010
- Posts
- 32
- Rep Power
- 0
thanks very much, ill try and get back to you.
- 08-22-2010, 12:43 PM #6
Member
- Join Date
- Aug 2010
- Posts
- 32
- Rep Power
- 0
g'day again,
program is doing what i want it to do thus far. now i just need to implement some more specific code to complete the task.
this is what i have so far: (which compiles and runs correctly)
String input;
String output = "";
char current;
int charactar;
System.out.println ("Enter a sentence:");
input = scan.nextLine();
current = input.charAt(0);
if (current == 'a')
output = output + current;
System.out.println ("Content:"+output);
}
}
after the if statement is performed, can i then loop it back around to do the if statement again for the charAt(1), then charAt(2) and so on? if so please show me.
thanksLast edited by stringkilla; 08-30-2010 at 08:37 AM.
- 08-22-2010, 12:58 PM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,422
- Blog Entries
- 7
- Rep Power
- 17
- 08-22-2010, 01:27 PM #8
Member
- Join Date
- Aug 2010
- Posts
- 32
- Rep Power
- 0
ok so i know how to do while loops sort of but how do i implement:
input.charAt(from 1'st to last character of string) ???
instead of doing input.charAt(0) (1) (2)... and so on
is it something like:
int length;
get the length of string (e.g. 25 characters)
length = input.length()
then going:
while (input.charAt(o) < 25)
or something
help :(
thanks
- 08-22-2010, 02:04 PM #9
Use a for loop with the index starting at 0(the first char) to the last char(index is length of string -1)how do i implement:
input.charAt(from 1'st to last character of string) ???
Use the loop index variable(for example i) as the arg to the charAt(i) method.
If you use a while loop, you will have to do all the index handling your self: Initialize, test and increment that is easily done by a for loop
- 08-22-2010, 02:32 PM #10
Member
- Join Date
- Aug 2010
- Posts
- 32
- Rep Power
- 0
sorry norm but that did not help me. rethink you reply to my previous post taking into account the assignment question and the relevance of your answer. i am asking for the answer. or some small portion of code that will help my program so far. have a look at what ive done. im stuck here. can you help me in the way i want you to and add to this.
thanks.
String input;
String output = "";
char current;
int length;
System.out.println ("Enter a sentence:");
input = scan.nextLine();
length = input.length();
current = input.charAt(0);
for (int length = 0; length <input.length(); length++) // dont know about this but i think im on to something
if (current == 'a')
output = output + current;
System.out.println ("Content:"+output);
be HELPFUL please i do not have your knowledge.Last edited by stringkilla; 08-30-2010 at 06:45 AM.
-
I beg to differ as Norm was being helpful, very helpful in fact, but the problem is that you didn't understand some or all of his answer, and fortunately this can be fixed. We don't like giving "the answer" here but rather helping you find the answer as you will learn so much more this way. In order for us to help you better, what you may wish to do is to tell us exactly what about his answer you don't understand, and perhaps we can either explain it more fully or else show you a decent tutorial that will help you understand us better.
Best of luck at solving your problem! :)
-
Yes, you are on to something here! Have you worked with for loops before? If not, please check out the tutorial on how they are used:Java Code:or (int length = 0; length <input.length(); length++) // dont know about this but i think im on to something
Using for loops
You will want to add an opening and closing brace for this loop and inside use the length int variable, the index of the loop, in your input.charAt(length) method call.
Keep at it and best of luck!
- 08-23-2010, 01:19 AM #13
Member
- Join Date
- Aug 2010
- Posts
- 32
- Rep Power
- 0
ok thanks very much, ill keep going with it and get back to you.
- 08-27-2010, 04:42 AM #14
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
definetely keep posting you are closer than you might think
- 08-27-2010, 11:22 AM #15
Member
- Join Date
- Aug 2010
- Posts
- 32
- Rep Power
- 0
ok so now when running the program i get weird output. please run, type ab or a or aa and see. stumped again. am trying to get it so it will output " a." or "a " or "A ". and so on. but am getting weirdness. any suggestions??? thanks
Last edited by stringkilla; 08-30-2010 at 06:46 AM.
- 08-27-2010, 11:36 AM #16
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,422
- Blog Entries
- 7
- Rep Power
- 17
- 08-27-2010, 11:59 AM #17
Member
- Join Date
- Aug 2010
- Posts
- 32
- Rep Power
- 0
what? this is what i get!?!
Enter a sentence:
aaa
aaaaaa
C:\>
please re-read last post. thanksLast edited by stringkilla; 09-13-2010 at 03:35 AM.
- 08-27-2010, 12:15 PM #18
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,422
- Blog Entries
- 7
- Rep Power
- 17
- 08-27-2010, 12:31 PM #19
Member
- Join Date
- Aug 2010
- Posts
- 32
- Rep Power
- 0
now i get this:
Enter a sentence:
ab
[a]
Enter a sentence:
a
[a]
Enter a sentence:
aa
[a][aa]
Enter a sentence:
abab
[a][aa]
Enter a sentence:
ab
[a]
i want it to output:"ab"Last edited by stringkilla; 09-13-2010 at 03:36 AM.
- 08-27-2010, 12:41 PM #20
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,422
- Blog Entries
- 7
- Rep Power
- 17
Similar Threads
-
Image Manipulation
By sh100 in forum New To JavaReplies: 0Last Post: 11-03-2009, 10:18 AM -
Image manipulation
By spike72 in forum Java 2DReplies: 1Last Post: 08-28-2009, 08:45 PM -
Array manipulation
By Ms.Ranjan in forum New To JavaReplies: 9Last Post: 07-18-2008, 09:10 PM -
String manipulation example (Title case)
By Java Tip in forum Java TipReplies: 0Last Post: 01-29-2008, 09:04 AM -
String Manipulation Task
By hiranya in forum New To JavaReplies: 1Last Post: 11-19-2007, 11:07 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks