sun
sun

Reputation: 315

How to skip a block in Java?

In the program given I have to make sure that if two consequtive characters are the same. I shouldn't increase the value of the variable (Count)... I have tried "break;", but that skips me out of the "for loop" which is very counter-productive. How can I skip the given part and still continue the "for loop"?

Currently my output for "Hello//world" is 3. It should be 2 (the '/' indicates a ' '(Space)).

Code

import java.util.Scanner;
class CountWordsWithEmergency
{
    public static void main()
    {
        Scanner input = new Scanner(System.in);
        System.out.println("Please input the String");
        String inp = input.nextLine();
        System.out.println("thank you");
        int i = inp.length();
        int count = 1;
        for(int j=0;j<=i-1;j++) //This is the for loop I would like to stay in.
        {
            char check = inp.charAt(j);
            if(check==' ')
            {
                if((inp.charAt(j+1))==check) //This is the condition to prevent increase for
                                             //count variable.
                {
                    count = count; //This does not work and neither does break;
                }
                count++;
            }
        }
        System.out.println("The number of words are : "+count);
    }
}

Upvotes: 4

Views: 5529

Answers (9)

sandeep vanama
sandeep vanama

Reputation: 709

continue is a keyword in java programming used to skip the loop or block of code and reexecutes the loop with new condition.

continue statement is used only in while,do while and for loop.

Upvotes: 1

PSR
PSR

Reputation: 40338

Try this:

if ((inp.charAt(j+1)) != check)  {
    count++;
}

Increment the value of count by checking with !=.

Upvotes: 2

Cyril Deba
Cyril Deba

Reputation: 1210

The following should work.

import java.util.Scanner;
class CountWordsWithEmergency
{
    public static void main()
    {
        Scanner input = new Scanner(System.in);
        System.out.println("Please input the String");
        String inp = input.nextLine();
        System.out.println("thank you");
        int i = inp.length();
        int count = 1;
        for(int j=0;j<=i-1;j++) //This is the for loop I would like to stay in.
        {
            char check = inp.charAt(j);
            if(check==' ')
            {
                if((inp.charAt(j+1))==check) //This is the condition to prevent increase for
                                             //count variable.
                {
                    continue;
                }
                count++;
            }
        }
        System.out.println("The number of words are : "+count);
    }
}

Upvotes: 0

oko
oko

Reputation: 1355

Use "continue;" when you want to break the current iteration.

Upvotes: 1

Miguel Prz
Miguel Prz

Reputation: 13792

The word you are looking for is "continue".

Upvotes: 2

sergelerator
sergelerator

Reputation: 576

You may want to use the continue keyword, or modify the logic a little bit:

import java.util.Scanner;
class CountWordsWithEmergency  
{
  public static void main()
  {
    Scanner input = new Scanner(System.in);
    System.out.println("Please input the String");
    String inp = input.nextLine();
    System.out.println("thank you");
    int i = inp.length();
    int count = 1;
    for(int j=0;j<=i-1;j++) //This is the for loop I would like to stay in.
    {
      char check = inp.charAt(j);
      if(check==' ')
      {
        if((inp.charAt(j+1))!=check)
        {
          count++;
        }
      }
    }
    System.out.println("The number of words are : "+count);
  }
}

Edit:

You may want to use the split method of the String class.

int wordsCount = str.split(' ').length;

Hope it helps :)

Upvotes: 1

benzonico
benzonico

Reputation: 10833

You can use the keyword continue in order to accomplish what you are trying to do.

However you can also inverse your conditional test and use count++ only if it is different (!= instead of == in your if) and do nothing otherwise

Upvotes: 6

JB Nizet
JB Nizet

Reputation: 691943

if ((inp.charAt(j+1)) != check)  {
    count++;
}

Upvotes: 3

Achintya Jha
Achintya Jha

Reputation: 12843

Try using continue where you want to skip an block.

Upvotes: 1

Related Questions