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...

String s = new String("");
		String s1 = new String("");
		System.out.println("enter String : ");
		Scanner keyboard = new Scanner(System.in);
		
		while(!s.equals("end")){
			s = keyboard.nextLine();
			s1 += s + "\n"; 
		
		}
		System.out.println("you've typed: \n");
		System.out.println(s1);

oh I've just figured out. however, i see see the "end". how do i remove it?

Recommended Answers

All 3 Replies

String s = new String("");
		String s1 = new String("");
		System.out.println("enter String : ");
		Scanner keyboard = new Scanner(System.in);
		
		while(!s.equals("end")){
			s = keyboard.next();
			s1 += s+" ";
                       // or s1 += s+"/n";
		
		}
		

		System.out.println("you've typed: \n");
		System.out.println(s1);
String s = new String("");
		String s1 = new String("");
		System.out.println("enter String : ");
		Scanner keyboard = new Scanner(System.in);
		
		while(!s.equals("end")){
			s = keyboard.next();
			s1 += s+" ";
                       // or s1 += s+"/n";
		
		}
		

		System.out.println("you've typed: \n");
		System.out.println(s1);

That is not what enuff4life has asked. Try this:

String s = new String("");
		String s1 = new String("");
		System.out.println("enter String : ");
		Scanner keyboard = new Scanner(System.in);
		
		while(!s.equals("end")){
			s = keyboard.nextLine();

                       if (!s.equals("end")) {
                             s1 += s + "\n"; 
                       }
		}
		System.out.println("you've typed: \n");
		System.out.println(s1);

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

int index = s1.indexOf("end");
System.out.println(s1.subString(0,index));

Link for the String class.

That is not what enuff4life has asked. Try this:

String s = new String("");
		String s1 = new String("");
		System.out.println("enter String : ");
		Scanner keyboard = new Scanner(System.in);
		
		while(!s.equals("end")){
			s = keyboard.nextLine();

                       if (!s.equals("end")) {
                             s1 += s + "\n"; 
                       }
		}
		System.out.println("you've typed: \n");
		System.out.println(s1);

or this way:

String s = new String("");
		String s1 = new String("");
		System.out.println("enter String : ");
		Scanner keyboard = new Scanner(System.in);
		s = keyboard.nextLine();
		while(!s.equals("end")){
			s1 += s + "\n"; 
                        s = keyboard.nextLine();
		}
		System.out.println("you've typed: \n");
		System.out.println(s1);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.