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?
if(!args.equals(null))always returntrue. Are you sure that's you want?