0

but when I run the program using the command line. I get a run time error of "java.lang.NoClassDefFoundError". All I did was copy the code from Netbeans and paste it on a notepad file and then tried running it by command prompt. I am not sure what I am/ did wrong. Any feedback is greatly appreciated it! Here is my code BTW

package reader;



import java.util.*;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintStream;

import java.util.Scanner;


public class Reader{

public static final Scanner in = new Scanner( System.in);
static final int adult = 0;  // The index representation numbers for the total and count arrays;
static final int child = 1;
static final int adultMale = 2;
static final int adultFemale = 3;
static final int childMale = 4;
static final int childFemale = 5;
static final int people = 6;
static final int family = 7;


public static void main(String[] arg){

    if(arg.length > 2){ die("Too many arguments");}
    else {System.out.println("Good");}

String inFileName;

    if(arg.length > 0){ inFileName = arg[0];}
    else {
    inFileName = "population.txt";}

Scanner fin = openFile(inFileName);
int[] count = new int[8];  // adults,children,male adult, female adult, male child , female child, people, family
int[] total = new int[8]; //adults,children,male adult, female adult, male child , female child, people, family

for(  ;  fin.hasNextLine();  ){
     String line = fin.nextLine();
    String error = check(line);

    if(error != null){die(error);}
    else{ gather(line, count, total);}

}//loop

for(int i = 0; i< count.length; i++){ System.out.print(count[i] + " ");}
System.out.println();
for(int i = 0; i< total.length; i++){ System.out.print(total[i] + " ");}
System.out.println();
System.out.println((float)count[family]/count[people]);

fin.close();

String outFileName;
if( arg.length > 1){ outFileName = arg[1];}
else{outFileName = "output.txt";}

PrintStream fout = outFile(outFileName);

showCensus(fout,count,total);

}//main


public static void die(String message){

System.err.println("Error: " + message);
System.exit(1);

}//die 

public static Scanner openFile(String fileName){

Scanner inputFile = null;
try{
inputFile = new Scanner(new File(fileName));
}
catch(FileNotFoundException e){ die("File not found: " + fileName);
}

return inputFile;

}// OpenFIle

public static PrintStream outFile(String fileName){

    Scanner temp = null;
    try{
    temp = new Scanner(new File(fileName));
    } catch(FileNotFoundException ei){
        PrintStream result = null;
        try{
        result = new PrintStream( new File(fileName));
        }catch(FileNotFoundException eo){die("Can't open " + fileName);}
        return result;
    }
    die("The file " + fileName + " already exists!");
    return null;
}

public static String check(String line){
    int change = 0;

    String sex;
    int age;
    Scanner sin = new Scanner(line);
    if(!sin.hasNext()){return null;}
    if(sin.next().equalsIgnoreCase("Comment")){return null;}
    Scanner sin2 = new Scanner(line);
    while(sin2.hasNext()){
       change++;
       if(change % 2 == 0){} 
       else{ 
           sex = sin2.next();
           if(!sex.equals("M")&& !sex.equals("F")){return "Gender must be 'M' or 'F', not " + sex;}
           if(!sin2.hasNext()){return "No data after " + sex ;}
           if(!sin2.hasNextInt()){return "age must be a number not " + sin2.next();}
           age = sin2.nextInt();
           //System.out.print(sex + " " + age + " ");
       }

    }

    System.out.println();

    return null;

}


public static void gather(String line, int[] count, int[] total){

   int change = 0; 
   Scanner sin = new Scanner(line);
   if(!sin.hasNext()){return ;}
   if(sin.next().equalsIgnoreCase("Comment")){return;}
   Scanner sin2 = new Scanner(line);
   while(sin2.hasNext()){
       change++;
       if(change % 2 == 0){}
       else{
           String sex = sin2.next();
           int age = sin2.nextInt();
           if(sex.equals("M") && age > 17){
               count[adultMale]++; 
               count[adult]++; 
               count[people]++;
               total[adultMale]+= age;
               total[adult]+= age;
               total[people]+= age;}
           else if(sex.equals("M") && age <= 17){
               count[child]++; 
               count[people]++; 
               count[childMale]++;
               total[child]+= age;
               total[people]+= age;
               total[childMale]+= age;}
           else if(sex.equals("F") && age > 17 ){
               count[adult]++; 
               count[adultFemale]++; 
               count[people]++;
               total[adult]+= age;
               total[adultFemale]+= age;
               total[people]+= age;}
           else if(sex.equals("F") && age <= 17){
               count[childFemale]++; 
               count[child]++; 
               count[people]++;
               total[childFemale]+= age;
               total[child]+= age;
               total[people]+= age;}
       }
   }// while

count[family]++;







}

public static void showCensus(PrintStream out, int[] count, int[] total){

    out.println("The Family Statistics 2013 Report");
    out.println();
    out.println("People: " + count[people] + " Average Age: " + (float)total[people]/count[people]);
    out.println("   Adults: " + count[adult] + " Average Age: " + (float)total[adult]/count[adult]);
    out.println("       Males: " + count[adultMale] + " Average Age: " + (float)total[adultMale]/count[adultMale]);
    out.println("       Females: " + count[adultFemale] + " Average Age: " + (float)total[adultFemale]/count[adultFemale]);
    out.println("   Children: " + count[child] + " Average Age: " + (float)total[child]/count[child]);
    out.println("       Males: " + count[childMale] + " Average Age: " + (float)total[childMale]/count[childMale]);
    out.println("       Female: " + count[childFemale] + " Average Age: " + (float)total[childFemale]/count[childFemale]);
    out.println("Families: " + count[family] + " Average Family Size " + (float)count[family]/count[people]);



}





}//Reader
8
  • Can you tell us what are your command line arguments? Commented Apr 26, 2013 at 19:47
  • did you compile your class? i mean u do have a .class file and not only a .java? Commented Apr 26, 2013 at 19:47
  • How to Compile and Run Java Code from a Command Line Commented Apr 26, 2013 at 19:49
  • There is no command line arguments. And yes I know how to compile and run a program using the command line Commented Apr 26, 2013 at 19:50
  • The file name itself is an argument, or is it not? Commented Apr 26, 2013 at 19:53

1 Answer 1

1

Your class Reader is defined in the reader package. You need to give the JVM the proper class path. Create a folder called reader and place your class there. Then use the -classpath flag when call java.

c:\>javac reader\Reader.java
c:\>java -classpath . reader.Reader
Sign up to request clarification or add additional context in comments.

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.