0

I get no compiler errors, but I get this when I run the program and trying to run case 1, the method lesFraFil():

Exception in thread "main" java.lang.NumberFormatException: For input string: ";
"
        at java.lang.NumberFormatException.forInputString(Unknown Source)
        at java.lang.Integer.parseInt(Unknown Source)
        at java.lang.Integer.parseInt(Unknown Source)
        at Hybelhus.lesFraFil(Oblig4.java:63)
        at Hybelhus.oversikt(Oblig4.java:134)
        at Hybelhus.meny(Oblig4.java:107)
        at Oblig4.main(Oblig4.java:23)

I have tried asking all my classmates, but none of them were able to help me.

import easyIO.*;
class Oblig4{
public static void main(String[] args) {

    int[] antallHybler = new int[18];

    for (int i = 0; i < args.length; i++) {
        antallHybler[i] = Integer.parseInt(args[i]);
    }

    Hybelhus hh = new Hybelhus(antallHybler);
    hh.meny();
}
}class Hybelhus{


    Out skjerm = new Out();
    In lesFil = new In("Hybeldata.txt");
    In tast = new In();
    Out skrivTilFil = new Out("Hybeldata.txt", true);

    Hybel[][] hybler = new Hybel[3][6];


    void lesFraFil(){

    int maaned = lesFil.inInt(";");
    int aar = lesFil.inInt(";");
    int totFortjeneste = lesFil.inInt(";");
    int totAntallMåneder = lesFil.inInt(";"); 
    int månedsleieVanligHybel = lesFil.inInt(";"); 
    int månedsleieToppEtasjeHybel = lesFil.inInt(";");

    skjerm.outln(maaned + ", " +  aar + ", "  + totFortjeneste + ", " + totAntallMåneder + ", " +  månedsleieVanligHybel + ", " + månedsleieToppEtasjeHybel);

    while(!lesFil.endOfFile()){

        for(int i = 0; i < hybler.length; i++){
            for(int j = 0; j < hybler[i].length; j++){

                String tekst = lesFil.inLine();
                if(lesFil == null){
                    continue;
                } 
                String[] enArray = tekst.split("; ");

                hybler[i][j] = new Hybel();

                hybler[i][j].etasje = Integer.parseInt(enArray[0])-1;
                hybler[i][j].rom = enArray[1].charAt(0);
                hybler[i][j].leietager.saldo = Integer.parseInt(enArray[2]);                    
                hybler[i][j].leietager = new Student(enArray[3]);
            } 
        }       
    }   
}
    Etasjer[] etasje = new Etasjer[3];
    Hybelhus(int[] antallHybler) {
        for(int i = 0; i < etasje.length; i++){
            etasje[i] = new Etasjer(antallHybler[i]);
        }
    }

    void SkrivUt() {
        for(int i = 0; i < etasje.length; i++){
            System.out.println("hei");
        }
    }

    void meny() {

        int aksjon = 0;

        while (aksjon != 8) {


        skjerm.outln("\nMENY");
        skjerm.outln("1. Skriv oversikt");
        skjerm.outln("2. Registrer ny leietaker");
        skjerm.outln("3. Registrer betaling fra leietaker");
        skjerm.outln("4. Registrer frivillig utflytting");
        skjerm.outln("5. Månedskjøring av husleie");
        skjerm.outln("6. Kast ut leietakere");
        skjerm.outln("7. Øk husleien");
        skjerm.outln("8. Avslutt");     

        aksjon = tast.inInt();

        switch (aksjon) {
        case 1: oversikt(); break;
        case 2: regLeietaker(); break;
        case 3: regBetaling(); break;
        case 4: regUtflytting(); break;
        case 5: kjorHusleie(); break;
        case 6: kastUt(); break;
        case 7: okHusleie(); break;
        case 8:; avslutt(); break;
        default: System.out.println ("\nDu må taste inn et av de åtte valgene over"); 
        break;
            }
        }
    }
        void oversikt() {

            final int BREDDE1 = 10;
            final int BREDDE2 = 35;
            final int BREDDE3 = 25;

            skjerm.out("Hybel", BREDDE1);
            skjerm.out("Leietager", BREDDE2);
            skjerm.out("Saldo", BREDDE3);

            skjerm.outln("\n----------------------------------------------------\n");

            lesFraFil();

        }

        void regLeietaker(){
        }
        void regBetaling() {
        }

        void regUtflytting(){
        }

        void kjorHusleie() {
        }

        void kastUt(){
        }

        void okHusleie() {
        }

        void avslutt() {
        }

}

class Etasjer{
    Hybel[] hybelNavn;

    Etasjer(int antallHybler){
        hybelNavn = new Hybel[antallHybler];

        for(int i = 0; i < hybelNavn.length; i++){
            char c = (char) i;
            c += 'A';
            hybelNavn[i] = new Hybel();
        }

    }

}

class Hybel{
    int etasje;
    char rom;
    Student leietager;

    Hybel() {   
    }
}

class Student{
    int saldo;
    String studentNavn;
    Student(String studentNavn){
        this.studentNavn = studentNavn;
    }

}
4
  • 1
    Which line is at Hybelhus.lesFraFil(Oblig4.java:63)? (Oblig4.java, line 63) Commented Oct 24, 2013 at 15:08
  • 1
    What does your input data look like? It might be better to split on ";" (no space) and then trim the strings before calling Integer.parseInt. Commented Oct 24, 2013 at 15:08
  • Can you please indicate with a comment the line at which the exception occurs. Commented Oct 24, 2013 at 15:08
  • Simply Debug your program adding a break on NumberFormatException's It should stop the first time it comes across this exception. Commented Oct 24, 2013 at 15:27

2 Answers 2

1

I don't understand what this means lesFil.inInt(";");, but to me this method obviously parses a string to an int and returns an int (from my best guess by the name of the method and since you say your program does not show any compile errors).

And since ";" is not a number, it throws a NumberFormatException

Sign up to request clarification or add additional context in comments.

1 Comment

inInt() is a method in his class In which because the constructor takes a text file I guess the ";" string passed in is a seperator character. It may be in here the error is happening... as there is nothing at line 63 ;-)
0

You need to use a debugger.. the line of code that is throwing this exception is line 63 of Oblig4.java. Because of formatting, I'm not sure which line this is. So look at your source code and goto line 63 and see what you are doing there..

I'm guessing it

 hybler[i][j].etasje = Integer.parseInt(enArray[0])-1;

What you can do is :

String temp = enArray[0];
System.out.println(temp);
int tempInt = Integer.parseInt(temp)-1;
System.out.println(tempInt);
hybler[i][j].etasje = tempInt;

And you'll see what's going on... (if you don't know how to use a debugger!)

Good luck!

Solve your problem ???

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.