0

I need to сheck that the code does not mix indents whitespaces and tabs.

The code is mixed with spaces and tabs, if both conditions are met:

  1. There are lines containing a space up to the first non-whitespace character (or the end of the line)
  2. There are lines containing a tab character up to the first non-whitespace character (or the end of the line)

Here's what I've tried to do

import java.io.*;
import java.util.*;

public class Main {
    public static void main(String[] args) throws IOException {
        FileInputStream fis = new FileInputStream("input.txt");
        Scanner sc = new Scanner(fis);
        boolean flag1 = false;
        boolean flag2 = false;
        while (sc.hasNextLine()) {
            String str = sc.nextLine();
            for(int i = 1; i < str.length(); i++){
                if((str.charAt(0) == ' ') && (str.charAt(i) != ' ')) {
                    flag1 = true;
                    break;
                }
            }
            System.out.println(flag1);
            for(int i = 0; i < str.length(); i++){
                int ind = str.indexOf("\t");
                int ind2 = -1;
                if(str.indexOf("\t") != -1){
                    if((str.charAt(i) != ' ') && (str.charAt(i) != '\t')) {
                        ind2 = str.indexOf(str.charAt(i));
                        if (ind < ind2) {
                            flag2 = true;
                        }
                    break;
                    }
                }
            }
            if (flag1 && flag2){
                break;
            }
        }
        FileOutputStream fos = new FileOutputStream("output.txt");
        PrintWriter pw = new PrintWriter(fos);
        if(flag1 == true && flag2 == true){
            System.out.println("NO");
        } else {
            System.out.println("YES");
        }
    }
}

But it doesn't work right when the input is

  // whitespace
        // tab 

Example of input:

public class Main 
{
  public static void main(String[]  args) 
  {
  }}

Output

YES

Could you explain how can I fix this?

EntErPrise
  • 55
  • 4
  • Did you try using regular expressions? – Piotr P. Karwasz Apr 03 '21 at 08:58
  • *Curious:* What is the purpose of `fos` and `pw`? – Andreas Apr 03 '21 at 13:24
  • *"Could you explain how can I fix this?"* Start by identifying where the logic is wrong. You can't fix it unless you know where the problem is. [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/5221149) – Andreas Apr 03 '21 at 13:33
  • That code is a bit of a mess. Lots of things that don't make much sense. But rather than pointing out all of the things that are wrong, I'm going to suggest that you learn the [Rubber Duck Debugging](https://rubberduckdebugging.com/) technique. Seriously. Then apply it ... carefully ... to this code. – Stephen C Apr 04 '21 at 04:27
  • The other thing is that the criteria you have stated for mixed indentation don't seem correct to me. – Stephen C Apr 04 '21 at 04:32

2 Answers2

1

Your code seems overly complex. Re-phrasing the assignment:

Examine all leading white-spaces, i.e. examine all indentations. If you find both space and tab characters, the input has mixed indentations.

try (Scanner sc = new Scanner(new FileInputStream("input.txt"))) {
    boolean foundSpace = false, foundTab = false;
    while ((! foundSpace || ! foundTab) && sc.hasNextLine()) {
        String line = sc.nextLine();
        for (int i = 0; i < line.length(); i++) {
            char ch = line.charAt(i);
            if (ch == ' ')
                foundSpace = true;
            else if (ch == '\t')
                foundTab = true;
            else
                break; // end of leading white-spaces
        }
    }
    System.out.println(foundSpace && foundTab ? "NO" : "YES");
}
Andreas
  • 154,647
  • 11
  • 152
  • 247
0

As Piotr suggests, the best solution is using regex (more efficent than loops I think) and here is how I did it,

import java.io.*;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    public static void main(String[] args) throws IOException {
        FileInputStream fis = new FileInputStream("input.txt");
        Scanner sc = new Scanner(fis);
        boolean flag1 = false;
        boolean flag2 = false;
        while (sc.hasNextLine()) {
            String str = sc.nextLine();
            
            // matches spaces
            Pattern pattern1 = Pattern.compile("^\\s*[ ]+\\s*[^\\s]+");
            Matcher matcher1 = pattern1.matcher(str);
            flag1 = matcher1.find();
            
            // matches tabs
            Pattern pattern2 = Pattern.compile("^\\s*[\t]+\\s*[^\\s]+");
            Matcher matcher2 = pattern2.matcher(str);
            flag2 = matcher2.find();
            
            if (flag1 && flag2){
                break;
            }
        }
        FileOutputStream fos = new FileOutputStream("output.txt");
        PrintWriter pw = new PrintWriter(fos);
        if(flag1 == true && flag2 == true){
            System.out.println("NO");
        } else {
            System.out.println("YES");
        }
    }
}
Ibraheem Alyan
  • 426
  • 1
  • 5
  • 14