-1

I am trying to terminate my code when "END" is input into the console, however my code wont play fair and I can't seem to see where it is going wrong, btw I haven't learnt how to debug as of yet so this has largely got to do with why i can't figure it out. please do help if you can.

import java.util.*;
class Main 
{
public static void main(String args[])
{
    int j = 0;
    while (j++ <= 3) {    
        // Create Scanner object to take input from command prompt
        Scanner s=new Scanner(System.in); 
        // Take input from the user and store it in st
        String st = "";
        while (!st.equals("END")) {
            {
                st=s.nextLine();
                // Initialize the variable count to 0
                int count=0;
                // Convert String st to char array
                char[] c=st.toCharArray();
                // Loop till end of string
                for(int i=0;i<st.length();i++)

                // If character at index 'i' is not a space, then increment count
                    if(c[i]!=' ') count++;
                // Print no.of spaces
                System.out.printf("[%4d] spaces in ", +(st.length()-count));
                // Print no.of spaces
                System.out.println('"' + st + '"');

            }
            j++;
        }
    }
}
Jonny
  • 19
  • 1
  • 7

3 Answers3

3

Since you have while (j++ <= 3) { ... } your program will end if you enter "END" two times.

Santosh
  • 874
  • 11
  • 21
0

Because you have two while loops nested. You input "END" in the second while loop and after that the second loop ends that's correct. But as you see, after it ends it will start the first loop which is while (j++ <= 3) { and in this while loop it waits for an input from user 3 times, which corresponds to Scanner s=new Scanner(System.in);, therefore it is normal for your program not to exit. If you want your program to finish after some input you may want to use System.exit(-1); command. I've added code according to your comments.

import java.util.*;

class Main {

public static void main(String args[]) {
int j = 0;
while (j++ <= 3) {
  // Create Scanner object to take input from command prompt
  Scanner s = new Scanner(System.in);
  // Take input from the user and store it in st
  String st = s.nextLine();
    if (!st.equals("END")) {
    // Initialize the variable count to 0
    int count = 0;
    // Convert String st to char array
    char[] c = st.toCharArray();
    // Loop till end of string
    for (int i = 0; i < st.length(); i++)

    // If character at index 'i' is not a space, then increment count
    {
      if (c[i] != ' ') {
        count++;
      }
    }
    // Print no.of spaces
    System.out.printf("[%4d] spaces in ", +(st.length() - count));
    // Print no.of spaces
    System.out.println('"' + st + '"');
  } else {
    System.exit(-1);
  }
}
}
}
Oguz Ozcan
  • 1,497
  • 13
  • 21
  • He also has an extra `j++`, so I think that it'll only be twice - I haven't stepped through it, though. – EJoshuaS - Stand with Ukraine Dec 04 '17 at 05:22
  • hello, do you know how and where i would integrate the System.exit(-1); command. when entering "END"? – Jonny Dec 04 '17 at 05:29
  • I don't know your reasoning for having `while (j++ <= 3) { ` this while loop, but according to your second while loop which is `while (!st.equals("END")) {` you may want to add `System.exit(-1);` command right after end of that. Because that while loop is telling me that you may want to finish your loop after user enters "END" and that time is when the loop finishes. But again I don't know why you have `while (j++ <= 3) { ` – Oguz Ozcan Dec 04 '17 at 05:35
  • I only used while (j++ <= 3) { so that i could get my java program to repeat 4 times. as I want to input and output strings for the first 3 times and the non the fourth run, I can input "END" and terminate my program – Jonny Dec 04 '17 at 05:40
  • I've added a sample code I think that will work for you. You don't need a second while loop for that, you can go with an if case. – Oguz Ozcan Dec 04 '17 at 05:45
  • still, it is not outputting what i want. When I for example input "END" on the first try, I do not want it to output " [ 0] spaces in "END" ", I want it to terminate the program straight away. could you help – Jonny Dec 04 '17 at 05:51
  • when i input it into charon to submit it, it gives me this error: -#### << Expected answer was >> ----------------------------------------- [ 3] spaces in "Line 1 test test" [ 0] spaces in "Line-2" [ 8] spaces in "Line 3 a b c d e f g" ------------------------------------------------------------------------- -#### << Your answer [Excluding lines containing a #] >> ---------------- Exception in thread "main" java.util.NoSuchElementException: No line found at java.util.Scanner.nextLine(Scanner.java:1540) at Main.main(Main.java:11) [ 3] spaces in "Line 1 test test" – Jonny Dec 04 '17 at 05:58
  • This error is about the code you write, not about the "END" part, if you try this with end from cmd it will work. – Oguz Ozcan Dec 04 '17 at 06:02
0

I have just add an if condition without using loop.

public static void main(String args[]) {
    int j = 0;
    while (j++ <= 3) {
        // Create Scanner object to take input from command prompt
        Scanner s = new Scanner(System.in);
        // Take input from the user and store it in st
        String st = "";

        st = s.nextLine();

        if (st.equalsIgnoreCase("END")) {
            j = 5;
        }
        // Initialize the variable count to 0
        int count = 0;
        // Convert String st to char array
        char[] c = st.toCharArray();
        // Loop till end of string
        for (int i = 0; i < st.length(); i++) // If character at index 'i' is not a space, then increment count
        {
            if (c[i] != ' ') {
                count++;
            }
        }
        // Print no.of spaces
        System.out.printf("[%4d] spaces in ", +(st.length() - count));
        // Print no.of spaces
        System.out.println('"' + st + '"');
        j++;
    }
}

What I do basically when I get the input end I just change the value of j to 5 so that the first loop will be terminated. and stop taking inpu.

Do you want this type of solution or you want you need to take input 3 times?