0

For a class that I have, I have to take arguments, which will be files with different urls on each line, and run through the lines to count the amount of times each url has different schemes and different domains. If there is no argument, than I have to scan in directly what is typed. I have gotten the following code, which includes everything except the printlns at the end.

import java.util.Scanner;
public class P1 {
  public static void main(String[] args){
    int fileLinesCt=0, totalLinesCt=0;
    int httpCt=0, httpsCt=0, ftpCt=0, otherSchemeCt=0;
    int eduCt=0, orgCt=0, comCt=0, otherDomainCt=0;
    String url, scheme, schemeSP, domain = null;
    Scanner scan = null;

    if(!args.equals(null)){
        for (int j=0; j<args.length; j++){
            scan = new Scanner(args[j]);
            fileLinesCt = 0;
            while (!"end".equals(scan.nextLine())){
                url = scan.nextLine();
                String[] parts = url.split("//://");
                scheme = parts[0];
                schemeSP = parts[1];
                if ("http".equals(scheme))
                    httpCt++;
                if ("https".equals(scheme))
                    httpsCt++;
                if ("ftp".equals(scheme))
                    ftpCt++;
                else otherSchemeCt++;
            for (int i=0; i<schemeSP.length(); i++){
                if (schemeSP.charAt(j) == '.')
                    domain = schemeSP.substring(j,'/');
                if (schemeSP.charAt(j) == '/')
                    break;
            }
            if ("edu".equals(domain))
                eduCt++;
            if ("org".equals(domain))
                orgCt++;
            if ("com".equals(domain))
                comCt++;
            else otherDomainCt++;
            fileLinesCt++;
            totalLinesCt++;
            if (fileLinesCt == 1)
                System.out.println(">> Got " + fileLinesCt + " line from " + scan);
            else System.out.println(">> Got " + fileLinesCt + " lines from " + scan);
            }
        }
    }
    else {
        Scanner scanner = new Scanner(System.in);
        fileLinesCt = 0;
        while (!scanner.next().equals("end")){
            url = scanner.next();
            String[] parts = url.split("//://");
            scheme = parts[0];
            schemeSP = parts[1];
            if ("http".equals(scheme))
                httpCt++;
            if ("https".equals(scheme))
                httpsCt++;
            if ("ftp".equals(scheme))
                ftpCt++;
            else otherSchemeCt++;
            for (int j=0; j<schemeSP.length(); j++){
                if (schemeSP.charAt(j) == '.')
                    domain = schemeSP.substring(j,'/');
                if (schemeSP.charAt(j) == '/')
                    break;
            }
            if ("edu".equals(domain))
                eduCt++;
            if ("org".equals(domain))
                orgCt++;
            if ("com".equals(domain))
                comCt++;
            else otherDomainCt++;
            fileLinesCt++;
            totalLinesCt++;
            if (fileLinesCt == 1)
                System.out.println(">> Got " + fileLinesCt + " line from " + scan);
            else System.out.println(">> Got " + fileLinesCt + " lines from " + scan);
        }
    }

When running the code, I get an error that says

"Program failed when listing file(s) on command line.
Exception in thread "main" java.util.NoSuchElementException: No line found" This occurs at line 15 when it tries to scan the next line.

What am I doing wrong that would stop it from scanning the next line?

1
  • 2
    The statement if(!args.equals(null)) always return true. Are you sure that's you want? Commented Sep 18, 2013 at 19:42

4 Answers 4

2

Thats because you are calling scan.nextLine twice

while (!"end".equals(scan.nextLine())){
    url = scan.nextLine();

You should do this instead:

while ( !("end".equals((url = scan.nextLine()))) ) {
    ...
}
Sign up to request clarification or add additional context in comments.

1 Comment

I tried this and it seems that this didn't fix the issue. With this changed do you have any idea why this may still be happening?
0

You are skipping a line at the start of each while-run.

scan.nextLine()

That advances the scanner past the line that is being returned, so

  while (!"end".equals(scan.nextLine())){
            url = scan.nextLine();

means url contains the second line. Which also means that it might skip over the end-condition, leading to it trying to read a line that doesn't exist.

scan.hasNextLine() is the way to check for the end of the file that you are looking for.

Comments

0

args is never null (I believe), try args.length != 0 instead.

Comments

0

The statement below

if(!args.equals(null)){

Could be switched by

if(args.length > 0){

Once using .equals in the String array args doesn't say whether is contains elements or not.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.