java - How do I make my for loop print my LinkedList Queue properly -
i writing program encode messages using repeating sequence. believe got encoding process correctly. using tostring statement before printing loop gives me correct values, when try , run loop @ end not print every element in list. example when enter "hello" "igo" instead of expected "igopt". thing can think of somehow encoded list somehow shrinking run loop, created new linkedlist print not affect encoded list, i'm not sure why isn't printing correctly.
import java.util.*; public class encode { public static void main(string[] args) { encode enc = new encode(); system.out.println("please enter string decode"); scanner scan = new scanner(system.in); string str = scan.nextline(); system.out.println("encoded: " + enc.encode(str)); } public string encode(string toencode) { queue key = new linkedlist(); // creates key , adds key values queue clone = new linkedlist(); key.add(1); key.add(2); key.add(3); key.add(4); key.add(5); linkedlist encoded = new linkedlist(); // creates list encoded linkedlist print = new linkedlist(); print = encoded; // values int ascii; // ascii value of character int keyvalue; // int value of key add ascii , encode // characters char result; // sum of int values of character , key string s = ""; // initializes string used return value (int = 0; < toencode.length(); i++) { clone = key; char mychar = toencode.charat(i); // gets character @ ascii = (int) mychar; // converts character @ int value if (ascii == 32) { // ignores spaces continue; } else { // if character not space keyvalue = (int) clone.remove(); result = (char) (ascii + keyvalue); encoded.add(result); clone.add(keyvalue); } } system.out.println(encoded.tostring()); // testing see if encoded // *remove after // prints elements of encoded list (int j = 0; j < encoded.size(); j++) { s = s + print.remove(); // gets first value , concatenates // string } return s; } }
the thing can think of somehow encoded list somehow shrinking run loop, created new linkedlist print not affect encoded list, i'm not sure why isn't printing correctly.
that's what's happening.
your linkedlist print
not protecting @ all, it's reference same list, , removals on affecting original list.
to make different list same elements, create using
linkedlist print = new linkedlist(encoded);
Comments
Post a Comment