-11

The code runs properly the first time then run it again using the while loop and lets say the first time I entered AA and it becomes CC then it runs again I enter AA again it will come out with CCCC do it again it comes out with CCCCCC I don't want that I need it to not keep the data from the string each time it 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 to Secret Cypher!");

            char loop = 'Y';
            while(loop == 'Y' || loop == 'y') {
                System.out.println("");
                System.out.println("Enter your cypher in upper case.");
                String s = kb.nextLine();


                char[] cs = s.toCharArray();
                for (int i = 0; i < cs.length; i++) {
                    e.append((char)('A' + (cs[i] - 'A' + 2) % 26));
                }
                if(s == s.toLowerCase()) {
                    System.out.println("Remember to use upper case letters!");
                    System.exit(0);//Also I was bored of using break and this works any where in the code.
                }
                System.out.println(e.toString());


                System.out.println("Do you want to enter another cypher? > ");
                String again = kb.nextLine();
                if(again.charAt(0) == 'N') {
                    System.out.println("Hope you come back again!");
                    break;
                }
            }
    }
}
Raedwald
  • 46,613
  • 43
  • 151
  • 237
Jordan
  • 119
  • 1
  • 1
  • 12
  • 3
    Don't repost. Edit and request that it be reopened, – nanofarad Aug 15 '13 at 15:01
  • I know but that had to many negative votes and people wont bother looking at. – Jordan Aug 15 '13 at 15:02
  • 1
    That doesn't mean that you can bend the rules and repost. If anything, all you're doing is encouraging the system to perform a question-ban on your IP address. – nanofarad Aug 15 '13 at 15:03
  • Looks like this one will have "too many negative votes" as well. – Charles Caldwell Aug 15 '13 at 15:03
  • wow really are you doing this on purpose? – Jordan Aug 15 '13 at 15:05
  • 1
    possible duplicate of [Java - I need help removing the data from the string before](http://stackoverflow.com/questions/18255051/java-i-need-help-removing-the-data-from-the-string-before). – nanofarad Aug 15 '13 at 15:05
  • 3
    No, we're voting down as per the guideline for the downvote, "This question does not show any research effort; it is unclear or not useful". This, unfortunately, fits the bill, due to lack of prior attempts to fix it or debug it using a debugger. – nanofarad Aug 15 '13 at 15:06
  • See also http://stackoverflow.com/questions/5192512/how-can-i-clear-or-empty-a-stringbuilder – Raedwald Jan 28 '15 at 20:00

1 Answers1

3

You're reusing the same string buffer. If you keep putting things into the same buffer without clearing it, you're obviously going to get extraneous stuff from previous iterations.

Simply declare the StringBuffer inside the while loop so that it is created on each iteration.

Anyway, you should learn to use your debugger, instead of asking here for us to debug. If anything, using the debugger can offer extremely valuable insight into the troubles that you are having here.

nanofarad
  • 40,330
  • 4
  • 86
  • 117
  • @JordanCoaten Is it related to this in any way? If you can write it well and show prior research, go ahead and ask. – nanofarad Aug 15 '13 at 15:07
  • Yeah that's what I was going to do this is the problem - You need to create a program that will calculate how much free space is still left in a shipping container (if any at all) . Example, if you tell the program the container has 3 maxi boxes, 7 midi boxes and 3 mini boxes it must say CAN'T SHIP YET – NOT HALF FULL YET. – Jordan Aug 15 '13 at 15:12
  • @JordanCoaten Is that homework? Make sure to show what you've tried, how it failed, and if you debugged at all(I ***strongly*** recommend you to use the debugger before asking) – nanofarad Aug 15 '13 at 15:15
  • Ok I will look at that and I didn't want the answer because I enjoy programming and I like to do it myself, I want to become a programmer when i'm older. – Jordan Aug 15 '13 at 15:17
  • @JordanCoaten Yes, programming is a fun hobby, especially as a student. – nanofarad Aug 15 '13 at 15:18
  • Can you send me a link to 'debugger' – Jordan Aug 15 '13 at 15:19
  • @JordanCoaten Are you using an IDE such as [Eclipse](http://eclipse.org)? It should include its own debugger within. – nanofarad Aug 15 '13 at 15:19
  • I've had 4 weeks from programming so I've got a bit rusty but my teacher said im the best in the class my target is a A* in computer science. – Jordan Aug 15 '13 at 15:20
  • Yeah im using eclipse I saw debugger before thinking about it but I thought it might be something different. – Jordan Aug 15 '13 at 15:21
  • @JordanCoaten OK, I'm experiencing the same with my studies right now as well. Anyway, I recommend you take a quick tutorial on the debugger as it will help greatly. – nanofarad Aug 15 '13 at 15:21
  • Ok, do you go to university for computer science? – Jordan Aug 15 '13 at 15:22
  • Or are you in six form doing A-levels? – Jordan Aug 15 '13 at 15:22
  • @JordanCoaten Somewhat off-topic, but I'm a high school student that leads the Computer Science club. Unfortunately I do not know what "six form" and "a-levels" are as we do not have such specification of levels and classes in the USA. – nanofarad Aug 15 '13 at 15:23
  • Oh ok, my teacher said most people in America are better at programming, how long have you been programming? – Jordan Aug 15 '13 at 15:26
  • @JordanCoaten Started a while ago, got serious in the beginning of this year. – nanofarad Aug 15 '13 at 15:28
  • 1
    Oh ok, well thats where the money's at. – Jordan Aug 15 '13 at 15:33