0

I am trying to make a program that lets the user go down the list of 3 ways to edit string (replace, eliminate, insert) and they work great. The problem is once I do the first operation it then asks if they want to do the next two at the same time instead of waiting for input on the following if statement.

the code I have is

    StringBuilder s = new StringBuilder("");
    boolean q = true;

    System.out.print("Enter a string :");
    String c = userinput.nextLine();
    s.append(c);

    while (q = true) {

        System.out.print("would you like to replace a character? (Y/N)");
        String replace1 = userinput.nextLine();
        if (replace1.equals("Y")) {
            System.out.print("Which char would you like to replace?");
            int r = userinput.nextInt();
            System.out.print("What char would you like to replace it with?");
            String j = userinput.next();
            s.deleteCharAt(r);
            s.insert(r, j);
            System.out.println("updated string is: " + s);
        }

        System.out.println("would you like to eliminate a character? (Y/N)");
        String replace2 = userinput.nextLine();
        if (replace2.equals("Y")) {
            System.out.print("Which char would you like to eliminate?");
            int r = userinput.nextInt();
            s.deleteCharAt(r);
            System.out.println("updated string is: " + s);
        }
        System.out.println("would you like to insert a character? (Y/N)");
        String replace3 = userinput.nextLine();
        if (replace3.equals("Y")) {
            System.out.print("Which char would you like to insert?");
            String i = userinput.next();
            System.out.print("Where would you like to insert it?");
            int r = userinput.nextInt();
            s.insert(r, i);
            System.out.println("updated string is: " + s);
        }
        System.out.println("would you like to edit more? (Y/N)");
        String quit = userinput.nextLine();
        if (quit.equals("N")) {
            System.out.println("you have exited the string editor");
            System.exit(0);
        }

    }

}
}
  • Enter a string :sam would you like to replace a character? (Y/N)Y
  • Which char would you like to replace?0
  • What char would you like to replace it with?B
  • updated string is: Bam would you like to eliminate a character? (Y/N)
  • would you like to insert a character? (Y/N)

it doesn't let me choose to eliminate any characters because it skips past to the last operation

Dave Newton
  • 152,765
  • 23
  • 240
  • 286

1 Answers1

0

While loop condition was wrong. '=' is used to assign a value and '==' is used for comparision. I tried changing userinput.nextLine() to userinput.next() and it all works fine.

Also you could try using equalsIgnoreCase instead of equals method

    Scanner userinput = new Scanner(System.in);
    StringBuilder s = new StringBuilder("");
    boolean q = true;

    System.out.print("Enter a string :");
    String c = userinput.next();
    s.append(c);

    while (q == true) {

        System.out.print("would you like to replace a character? (Y/N)");
        String replace1 = userinput.next();
        if (replace1.equals("Y")) {
            System.out.print("Which char would you like to replace?");
            int r = userinput.nextInt();
            System.out.print("What char would you like to replace it with?");
            String j = userinput.next();
            s.deleteCharAt(r);
            s.insert(r, j);
            System.out.println("updated string is: " + s);
        }

        System.out.println("would you like to eliminate a character? (Y/N)");
        String replace2 = userinput.next();
        if (replace2.equals("Y")) {
            System.out.print("Which char would you like to eliminate?");
            int r = userinput.nextInt();
            s.deleteCharAt(r);
            System.out.println("updated string is: " + s);
        }
        System.out.println("would you like to insert a character? (Y/N)");
        String replace3 = userinput.next();
        if (replace3.equals("Y")) {
            System.out.print("Which char would you like to insert?");
            String i = userinput.next();
            System.out.print("Where would you like to insert it?");
            int r = userinput.nextInt();
            s.insert(r, i);
            System.out.println("updated string is: " + s);
        }
        System.out.println("would you like to edit more? (Y/N)");
        String quit = userinput.next();
        if (quit.equals("N")) {
            System.out.println("you have exited the string editor");
            System.exit(0);
        }

    }
Arv
  • 254
  • 2
  • 6