java - Output accumulates each iteration instead of resetting -


the code runs first time run again using while loop , lets first time entered aa , becomes cc runs again enter aa again come out cccc again comes out cccccc don't want need not keep data string each time loops.

import java.util.*; public class secretcypher {      public static void main(string args[]) {          scanner kb = new scanner(system.in);         stringbuffer e = new stringbuffer();         system.out.println("welcome secret cypher!");              char loop = 'y';             while(loop == 'y' || loop == 'y') {                 system.out.println("");                 system.out.println("enter cypher in upper case.");                 string s = kb.nextline();                   char[] cs = s.tochararray();                 (int = 0; < cs.length; i++) {                     e.append((char)('a' + (cs[i] - 'a' + 2) % 26));                 }                 if(s == s.tolowercase()) {                     system.out.println("remember use upper case letters!");                     system.exit(0);//also bored of using break , works in code.                 }                 system.out.println(e.tostring());                   system.out.println("do want enter cypher? > ");                 string again = kb.nextline();                 if(again.charat(0) == 'n') {                     system.out.println("hope come again!");                     break;                 }             }     } } 

you're reusing same string buffer. if keep putting things same buffer without clearing it, you're going extraneous stuff previous iterations.

simply declare stringbuffer inside while loop created on each iteration.

anyway, should learn use debugger, instead of asking here debug. if anything, using debugger can offer extremely valuable insight troubles having here.


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -