2. Challenge - No Output


#1

Hey guys,

I just completed the code for the second Challenge and tried to run it, but I don’t get any output at all.
I compared my idea of a solution to some that are working and it’s almost identical to some. Even if I change it so it is identical, I don’t get any output.
Why could that be? I’m running Mountain Lion, could that be it?
Any help would be really appreciated.

Thank you :slight_smile:

Here is my code:

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[])
{

    @autoreleasepool {
        
    // Read in the Name File
        NSString *nameString = [NSString stringWithContentsOfFile:@"usrshare/dict/propernames" encoding:NSUTF8StringEncoding error:NULL];
    // Read in the Word File
        NSString *wordString = [NSString stringWithContentsOfFile:@"usrshare/dict/words" encoding:NSUTF8StringEncoding error:NULL];
        
    // Break them into arrays of string
        NSArray *names = [nameString componentsSeparatedByString:@"\n"];
        NSArray *words = [wordString componentsSeparatedByString:@"\n"];
        
    // Compare both Files
        for (NSString *n in names) {
            for (NSString *w in words) {
                if ([n caseInsensitiveCompare:w] == NSOrderedSame) {
                    NSLog(@"%@ and %@ are both names and words", n, w);
                    break;
                }
            }
            
            
        }
    
    }
    return 0;
}

#2

Hello my friend,

I looked your code, it seems exactly the same to my code except the part “usrshare”. It must be “usr/share”.

I am also putting my code here, but as i said before they looked the same to me. If you make the correction, it will work properly.

One more thing. When you use for in or for loop or if sentence, if you have one line statement no need “{ }”. Also no need break here. I think.

Please Look at my code:

//
//  main.m
//  NSMutableArrayChallenge2
//

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{

    @autoreleasepool {
        
        
        //stringWithContentsOfFile:encoding:error:
        // Returns a string created by reading data from the file at a given path interpreted using a given encoding.
        
        // Read in a file as a huge string (ignoring the possibility of error)
        NSString *nameString = [NSString stringWithContentsOfFile:@"/usr/share/dict/propernames" encoding:NSUTF8StringEncoding error:NULL];
        
        NSString *regWordsString = [NSString stringWithContentsOfFile:@"/usr/share/dict/words" encoding:NSUTF8StringEncoding error:NULL];
        
        // Break into an array of strings
        NSArray *names = [nameString componentsSeparatedByString:@"\n"];
        
        NSArray *regularWords = [regWordsString componentsSeparatedByString:@"\n"];
        
//        for (NSString *str in regularWords)
//            NSLog(@"%@", str);
        
        for (NSString *name in names)
            for (NSString *regWord in regularWords)
                if ([name caseInsensitiveCompare:regWord] == NSOrderedSame)
                    NSLog(@"%@ and %@ are equal", name, regWord);
        
    }
    return 0;
}

#3

Thanks so much, that did the trick!

All the best,
Lukas


#4

You’re welcome.


#5

I’m having a similar problem with having no output using the code below. Mine also looks terribly similar to some of the prior posted solutions, but I’m not getting anything. Anybody see what I’m missing here?

[code]#import <Foundation/Foundation.h>

int main(int argc, const char * argv[])
{

@autoreleasepool {
    
    // Read in each file as a huge string (ignoring the possibility fo an error)
    NSString *wordsString = [NSString stringWithContentsOfFile:@"usr/share/dict/words"
                                               encoding:NSUTF8StringEncoding
                                                  error:NULL];
    NSString *properNamesString = [NSString stringWithContentsOfFile:@"usr/share/dict/propernames"
                                                     encoding:NSUTF8StringEncoding
                                                        error:NULL];
    
    // Break each string into an array of strings
    NSArray *words = [wordsString componentsSeparatedByString:@"\n"];
    NSArray *properNames = [properNamesString componentsSeparatedByString:@"\n"];
    
    // Go through the words array one string at a time
    for (NSString *w in words){
        
        // Go through the properNames array one string at a time
        for (NSString *p in properNames){
            
            //See if the selected word equals the selected properName
            if ([w caseInsensitiveCompare:p] == NSOrderedSame){
                NSLog(@"%@ is both a word and a proper noun.", p);
                break; // Okay to exit back to main loop since we have a match.
            }
        }
        
    }
    
    
}
return 0;

}

[/code]


#6

I figured out my problem. There was a backslash missing from my import of the words and proper names files.

See the updated code below.

[code]#import <Foundation/Foundation.h>

int main(int argc, const char * argv[])
{

@autoreleasepool {
    
    // Read in each file as a huge string (ignoring the possibility fo an error)
    NSString *wordsString = [NSString stringWithContentsOfFile:@"/usr/share/dict/words"
                                               encoding:NSUTF8StringEncoding
                                                  error:NULL];
    NSString *properNamesString = [NSString stringWithContentsOfFile:@"/usr/share/dict/propernames"
                                                     encoding:NSUTF8StringEncoding
                                                        error:NULL];
    
    // Break each string into an array of strings
    NSArray *words = [wordsString componentsSeparatedByString:@"\n"];
    NSArray *properNames = [properNamesString componentsSeparatedByString:@"\n"];
    
    // Go through the words array one string at a time
    for (NSString *w in words){
        
        // Go through the properNames array one string at a time
        for (NSString *p in properNames){
            
            //See if the selected word equals the selected properName
            if ([w caseInsensitiveCompare:p] == NSOrderedSame){
                NSLog(@"%@ is both a word and a proper noun.", p);
                break; // Okay to exit back to main loop since we have a match.
            }
        }
        
    }
    
    
}
return 0;

}

[/code]


#7

Gee,
You forgot to place /. Please notice it :
NSString *properNameString = [NSString stringWithContentsOfFile:@"/usr/share/dict/propernames" encoding:NSUTF8StringEncoding error:NULL];


#8

[quote=“dangeresque”]I figured out my problem. There was a backslash missing from my import of the words and proper names files.
[/quote]
Been there. Just done that too :slight_smile: I had no output and could not figure it out.
I knew something was wrong when I tried to print out the first element of the first array, just to check if it had been reading the file.
I got NULL instead.
Thanks for sharing that.