I want to know how I can make a program that takes command line arguments work without a command line argument.
It's the last else if
-statement that I need help with. How can I accomplish what I'm trying to do here?
P.S I did not find an answer to this when reading the replies on the post this is a "possible duplicate" of.
Here's my code:
import java.util.Scanner;
import java.io.File;
import java.util.ArrayList;
class LesInformasjon{
public static void main(String[]args) throws Exception{
Scanner fil = new Scanner(new File("informasjon.txt"));
ArrayList<Bil> biler = new ArrayList<>();
while(fil.hasNextLine()){
String line = fil.nextLine();
String ord[] = line.split(" ");
String bilType = ord[0];
String kjennemerke = ord[1];
Bil bil = null;
//Tester typen bil, lager bil og setter inn i ArrayList
if(bilType.equals("EL")){
double batteriKapasitet = Double.parseDouble(ord[2]);
bil = new Elbil(kjennemerke, bilType, batteriKapasitet);
}else if(bilType.equals("LASTEBIL")){
double utslipp = Double.parseDouble(ord[2]);
double nyttevekt = Double.parseDouble(ord[3]);
bil = new Lastebil(kjennemerke,bilType, utslipp, nyttevekt);
}else if(bilType.equals("PERSONBIL")){
double utslipp = Double.parseDouble(ord[2]);
int antGodkjenteSeter = Integer.parseInt(ord[3]);
bil = new Personbil(kjennemerke, bilType, utslipp, antGodkjenteSeter);
}
biler.add(bil);
}
if(args[0].equals("EL")){
for(Bil bil : biler){
if(bil instanceof Elbil){
//if(bil.bilType.equals("EL")){
System.out.println(bil);
System.out.println(" ");
}
}
//System.out.println("Print Elbiler");
}else if(args[0].equals("FOSSIL")){
for(Bil bil : biler){
if(bil instanceof Fossilbil){
//if(bil.bilType.equals("LASTEBIL") || bil.bilType.equals("PERSONBIL")){
System.out.println(bil);
System.out.println(" ");
}
}
}else if(args.length == 0){ //tried else if(args[0] == null as well
for(Bil bil : biler){
System.out.println(bil);
System.out.println(" ");
}
}
}
}
If you need the other classes, I can give you those. However, they're not needed to answer the question.