Reputation: 1
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?
Upvotes: 0
Views: 161
Reputation: 3396
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.
Upvotes: 0
Reputation: 6479
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()))) ) {
...
}
Upvotes: 2
Reputation: 3825
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.
Upvotes: 0