943,683 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 536
  • Java RSS
Nov 11th, 2008
0

creating a method for texts

Expand Post »
I need to creat a method something like... when the user inputs

test1
test2 haha

test3
end


and display that should contain exact same one as above except "end":
test1
test2 haha

test3

so the "end" is the keyword to finish the inputing...

Java Syntax (Toggle Plain Text)
  1. String s = new String("");
  2. String s1 = new String("");
  3. System.out.println("enter String : ");
  4. Scanner keyboard = new Scanner(System.in);
  5.  
  6. while(!s.equals("end")){
  7. s = keyboard.nextLine();
  8. s1 += s + "\n";
  9.  
  10. }
  11. System.out.println("you've typed: \n");
  12. System.out.println(s1);

oh I've just figured out. however, i see see the "end". how do i remove it?
Last edited by enuff4life; Nov 11th, 2008 at 4:53 pm.
Similar Threads
Reputation Points: 31
Solved Threads: 0
Newbie Poster
enuff4life is offline Offline
18 posts
since Nov 2007
Nov 11th, 2008
0

Re: creating a method for texts

Java Syntax (Toggle Plain Text)
  1. String s = new String("");
  2. String s1 = new String("");
  3. System.out.println("enter String : ");
  4. Scanner keyboard = new Scanner(System.in);
  5.  
  6. while(!s.equals("end")){
  7. s = keyboard.next();
  8. s1 += s+" ";
  9. // or s1 += s+"/n";
  10.  
  11. }
  12.  
  13.  
  14. System.out.println("you've typed: \n");
  15. System.out.println(s1);
Reputation Points: 12
Solved Threads: 3
Junior Poster in Training
Chaster is offline Offline
68 posts
since Jun 2007
Nov 12th, 2008
0

Re: creating a method for texts

Click to Expand / Collapse  Quote originally posted by Chaster ...
Java Syntax (Toggle Plain Text)
  1. String s = new String("");
  2. String s1 = new String("");
  3. System.out.println("enter String : ");
  4. Scanner keyboard = new Scanner(System.in);
  5.  
  6. while(!s.equals("end")){
  7. s = keyboard.next();
  8. s1 += s+" ";
  9. // or s1 += s+"/n";
  10.  
  11. }
  12.  
  13.  
  14. System.out.println("you've typed: \n");
  15. System.out.println(s1);
That is not what enuff4life has asked. Try this:

Java Syntax (Toggle Plain Text)
  1. String s = new String("");
  2. String s1 = new String("");
  3. System.out.println("enter String : ");
  4. Scanner keyboard = new Scanner(System.in);
  5.  
  6. while(!s.equals("end")){
  7. s = keyboard.nextLine();
  8.  
  9. if (!s.equals("end")) {
  10. s1 += s + "\n";
  11. }
  12. }
  13. System.out.println("you've typed: \n");
  14. System.out.println(s1);

Or keep the while-loop as it is and use the methods:
String.indexOf()
and
String.subString()

Java Syntax (Toggle Plain Text)
  1. int index = s1.indexOf("end");
  2. System.out.println(s1.subString(0,index));

Link for the String class.
Sponsor
Featured Poster
Reputation Points: 1014
Solved Threads: 446
Nearly a Senior Poster
javaAddict is offline Offline
3,258 posts
since Dec 2007
Nov 12th, 2008
0

Re: creating a method for texts

Click to Expand / Collapse  Quote originally posted by javaAddict ...
That is not what enuff4life has asked. Try this:

Java Syntax (Toggle Plain Text)
  1. String s = new String("");
  2. String s1 = new String("");
  3. System.out.println("enter String : ");
  4. Scanner keyboard = new Scanner(System.in);
  5.  
  6. while(!s.equals("end")){
  7. s = keyboard.nextLine();
  8.  
  9. if (!s.equals("end")) {
  10. s1 += s + "\n";
  11. }
  12. }
  13. System.out.println("you've typed: \n");
  14. System.out.println(s1);
or this way:

Java Syntax (Toggle Plain Text)
  1. String s = new String("");
  2. String s1 = new String("");
  3. System.out.println("enter String : ");
  4. Scanner keyboard = new Scanner(System.in);
  5. s = keyboard.nextLine();
  6. while(!s.equals("end")){
  7. s1 += s + "\n";
  8. s = keyboard.nextLine();
  9. }
  10. System.out.println("you've typed: \n");
  11. System.out.println(s1);
Reputation Points: 919
Solved Threads: 354
Nearly a Posting Maven
stultuske is offline Offline
2,487 posts
since Jan 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: A method that returns a collection
Next Thread in Java Forum Timeline: exception handling for wrong input.





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC