-1

I have this assignment where I need to populate a 2d array using info from a text file. I am trying to make every thing even using the primekey variable to replace my poscon that I am using temporarily. What is stopping me from doing that is that primekey wont increment right because of another

(MAIN PROBLEM) variable that is stuck, which is the emailcheck variable. I'm pretty sure it has to do with my for loop syntax but I can't quite figure it out.

       ...

try{
    Scanner check = new Scanner(file);
    Scanner checkNext = new Scanner(file);       

    System.out.println("Success File load");
    String data=check.next();
    System.out.println("data.next() works");

    int emailcheck=0;
    int primekey=0;

    while(check.hasNext()){
        posCon++;
        //check for @ symbol
        for(int i=0;i<data.length();i++){
            if(data.charAt(i)=='@'){
                emailcheck=emailcheck+1;
            }
        }

        //populates position array
        if(data.equalsIgnoreCase("staff")||
                data.equalsIgnoreCase("freshman")||
                data.equalsIgnoreCase("sohmore")||
                data.equalsIgnoreCase("junior")||
                data.equalsIgnoreCase("senior")||
                data.equalsIgnoreCase("adjunct")||
                data.equalsIgnoreCase("professor"))
        {
            db[0][posCon]=data;
            sort=1;
            data=check.next();

        }
        //id
        else if(sort==1){
            db[1][posCon]=data;
            sort=2;
            data=check.next();
        }
        //firstname
        else if(sort==2){
            db[2][posCon]=data;
            sort=3;
            data=check.next();
        }
        //lastname
        else if(sort==3){
            db[3][posCon]=data;
            sort=4;
            data=check.next();
        }
        //department
        else if(sort==4){
            db[4][posCon]=data;
            sort=5;
            data=check.next();
        }
        //email
        else if(sort==5 && emailcheck==1){
            db[5][posCon]=data;
            sort=6;
            emailcheck=0;
        }
        else if(sort==5 && emailcheck==0){
            db[5][posCon]="not here";
            sort=6;
        }
        //room
        else if(sort==6){
            db[6][posCon]=data;
            sort=0;
            data=check.next();
            emailcheck=0;
            primekey=primekey+1;
            System.out.println(primekey);
        }
        else{
            sort=0;
            data=check.next();
            emailcheck=0;
        }

    }
}catch(FileNotFoundException e) {
    e.printStackTrace();
}
}//End Constructor

here is the data from the text file

Staff 77778 Julie Chang Registrar
Adjunct 19778 Mike Thompson CS mtxxx@gmail.com GITC2400
Staff 30041 Anne Mathews Security
Junior 98444 Serene Murray Math smyyy@gmail.com
Freshman 98772 Bob Mathew CS bmyyy@gmail.com
Professor 19010 Joan Berry Math jbxxx@gmail.com GITC2315C
Professor 19871 Aparna Khat CS akxxx@gmail.com GITC1400
Adjunct 18821 Hari Mentor Physics hmxxx@gmail.com CK231
Staff 20112 Jim George Plant
Junior 68339 Tom Harry CS thyyy@gmail.com
Senior 78883 Vince Charles IT vcyyy@gmail.com
Freshman 87777 Susan Han EE shyyy@gmail.com
Senior 88888 Janki Khat IE jkyyy@gmail.com
Staff 5555 Aparna Sen Plant
Senior 66663 Jill Kaley it jk@jk.com
Staff 77777 Joe Batra plumbing
Staff 33333 Jim Natale Plumbing
Eypros
  • 5,370
  • 6
  • 42
  • 75
user2872194
  • 147
  • 2
  • 12
  • 2
    I advise you to indent the code properly. The problem might get clearer then. – aioobe Dec 04 '14 at 08:51
  • Your `primekey` is only incremented in case `sort == 6` but where do you set this var? – Eypros Dec 04 '14 at 08:59
  • What should emailcheck contain? When should it be 0 and when 1? – Eran Dec 04 '14 at 08:59
  • i dont get what you're trying to achieve/expected output. – Ker p pag Dec 04 '14 at 09:00
  • Classic problem for debugger. Please use it. – Maroun Dec 04 '14 at 09:01
  • sorry I wasn't clear. my overall goal is to have the array populate while keeping it organized. The only overall issue I am having while ignoring the primekey issue is the emailcheck variable. Emailcheck is supposed to equal 1 or 0 depending if the @ character is located in the word or not. the problem is that it is always set to 1. – user2872194 Dec 04 '14 at 09:04

1 Answers1

1

You have the data = check.next() call in the wrong place it should just after the start of the loop with the check.hasNext() test in it, and not elsewhere. You only need one Scanner instance the second one you declare is redundant.

Matthew V Carey
  • 196
  • 1
  • 10