-1

Why is this returning false for 'a' when it exists in the string I pass as input (input is "the quick brown fox jumps over the lazy dog")

    import java.util.*;
    import java.io.*;
    public class Main 
    {
        public static void main(String args[])throws IOException
        {
            Scanner sc=new Scanner(System.in);
            String s=sc.next();
            int len=s.length();
            int i;
            char c;
            boolean arr[] = new boolean[26];
            for(i=0;i<len;i++)
            {
                c = s.charAt(i);
                if(Character.isLetter(c))
                {
                    continue;
                }
                else
                {
                    System.out.println("Invalid Input");
                    System.exit(1);
                }
            }
            for(i=0;i<len;i++)
            {
                if(s.charAt(i)==' ')
                {
                    s.replace(" ","");
                }
            }
            boolean success = true;
            for(c = 'a';c <= 'z'; c++) 
           {
               //System.out.println(String.valueOf(c));
               if(!s.contains(String.valueOf(c))) 
               {
                   System.out.println(String.valueOf(c));
                   success = false;
                   break;
               }
           }
           if(success)
           {
                System.out.println("Special");
           }
           else System.out.println("Not Special");
       }
   }  
Brian Tompsett - 汤莱恩
  • 5,195
  • 62
  • 50
  • 120
  • 1
    It is clear that `the` does not contain `a`, hence you get `success` as *false*. What would you expect? Your `s` equals to `the`. There are other issues, like `s.replace(" ","");`, but it seems you just expect the scanner to read a whole line, not just the first non-whitespace token, right? – Wiktor Stribiżew Sep 12 '16 at 07:46
  • Try a debugger or some well-placed `System.out.println()` statements. – Ole V.V. Sep 12 '16 at 07:58
  • 1
    This could be a dupe: http://stackoverflow.com/questions/22458575/whats-the-difference-between-next-and-nextline-methods-from-scanner-class (since OP will then understand his issue after reading the answers) – Tom Sep 12 '16 at 08:01
  • Actually, there are more issues here than just `next()`. – Wiktor Stribiżew Sep 12 '16 at 08:02
  • Your question title needs some work. Try to describe your problem in a generic way instead of simply copying your complete and specific description, e.g. "Cannot find character in stream when using java.util.Scanner". Think about which class of problems you're looking at and what someone with a similar problem might search for. – valid Sep 12 '16 at 08:38

2 Answers2

0

The String s=sc.next(); line initializes s with just the. As the does not contain a, you get success as false.

If you plan to check the whole line, you should change String s=sc.next(); to String s=sc.nextLine();.

However, your first for loop will stop the code execution once a space is found. You need to remove it. Also, the second for loop seems to do excessive and meaningless work since s.replace(" ","") will not modify s (strings are immutable in Java). Just use s = s.replace(" ", "") to remove spaces from the input.

See this Java demo printing Special.

Wiktor Stribiżew
  • 484,719
  • 26
  • 302
  • 397
0

It's only reading in the word 'the', which doesn't contain an 'a'. You need to read in the whole string to check the whole thing. If you want the whole line, don't use 'sc.next()', use 'sc.nextLine()'

SuperHanz98
  • 1,126
  • 1
  • 10
  • 21
  • He needs to change it from checking if it's a letter in that next for loop as well actually because spaces aren't letters – SuperHanz98 Sep 12 '16 at 08:05