Results 1 to 9 of 9
  1. #1
    steinarsen is offline Member
    Join Date
    Nov 2012
    Posts
    6
    Rep Power
    0

    Default Unwanted Null output.

    Hi!

    Im just getting started with java and im having some troubles.
    I want to print out some grades, but im just getting null as an output and i cant seem to figure out the problem.

    Thanks in advance!

    Java Code:
    import java.util.Random;
    import java.util.Arrays;
    import java.util.ArrayList;
    import java.math.BigInteger;
    import javax.swing.*;
    import java.awt.*;
    
    
    public class Studenter {
        private int antallStudenter = 20;//Setter antall studenter
        private int maksPoeng = 200;//Maks oppnåelig poeng
        private int[] studenter = new int [antallStudenter];//Oppretter arryaer som programmet setter data inn i
        private int[] poeng = new int[antallStudenter];
        private int[] poengKlone = new int[antallStudenter];
        private int[] prosent = new int[antallStudenter];
        private int[] bestaat = new int[antallStudenter];
        private String[] karakter = new String[antallStudenter];
        private static final Random rPoeng = new Random();//random genererer poeng.
        
            private int bestaa = 1;        
            
            
            private int A = 1;
            private int B = 1;
            private int C = 1;
            private int D = 1;
            private int E = 1;
            private int F = 1;
            private ArrayList<String> liste = new ArrayList<String>();
            private boolean brukt = false; 
    
        
                 
    		
    		
        
        public void DelUt()//"Deler" ut all infoen
        {
            for(int i = 0; i<antallStudenter; i++)
            {
                studenter[i] = studentNummer(i);
                poeng[i] = tildelPoeng(i);
                prosent[i] = tildelProsent(i);
                poengKlone[i] = poeng[i];
                bestaat[i] = bestodStudent(i);
                
            }
        
        }
        
        public int studentNummer(int i)//Opretter studentnummer
                {
                    int studentNummer = 1 + i;
                    return studentNummer;
                }
                    
        public int tildelPoeng(int i)//Oppretter poeng
        {
            int studentPoeng = rPoeng.nextInt(maksPoeng + 1);
            return studentPoeng;
        }
        
        public int tildelProsent(int i)//Regener ut prosent
        {
            int tildelProsent = ((poeng[i]*100)/maksPoeng);
            return tildelProsent;
        }
        
               public int bestodStudent(int i)
        {
            int studentNummer;
            if(poeng[i]>30)
            {
            studentNummer = studenter[i]; 
            bestaa++;
            }else{
                studentNummer = 0;
            }
            return studentNummer;
        }
               
               public void karStudenter()
        {
            int a;
            Arrays.sort(poengKlone); //Sorterer klonen av poeng så vi slipper å bytte om på karakterene til studentene
            Arrays.sort(bestaat); //Sorterer adressene til hvem som har passert
            double counter = 0.0;
            for(int i = 0; i<antallStudenter; i++)
            {
                a = searchClone(i);  //Finner riktig plassering i "points" for denne studenten så riktig student beholder poengsummen og karakteren den først fikk
                if(bestaat[i]>=30)
                {
                     counter++;
                    if((counter<=(bestaa*10)/100))
                    {
                        karakter[a] = "E";
                        E++;
                    }else if(counter<=((bestaa*35)/100) && counter >((bestaa*10)/100))
                    {
                        karakter[a] = "D";
                        D++;
                    }else if(counter<=((bestaa*65)/100) && counter>((bestaa*35)/100))
                    {
                        karakter[a] = "C";
                        C++;
                    }else if(counter<=((bestaa*90)/100) && counter >((bestaa*65)/100))
                    {
                        karakter[a] = "B";
                        B++;
                    }else if(counter <=bestaa && counter >(bestaa*90)/100)
                    {
                        karakter[a] = "A";
                        A++;
                    }
                }else if(bestaat[i]<30||bestaat[i]==-1){
                    karakter[a] = "F";
                    F++;
                }
            }
        }
    
               public void addToList(BigInteger z) //Legger til adressen som er blitt brukt
        {
            String s = z.toString();
            liste.add(s);
        }
    
            public void checkList(BigInteger z) //Sjekker lista for å finne hvem som er blitt brukt og hvem som skal brukes neste
        {
            String s = z.toString();
            if(liste.contains(s))
            {
                brukt = true;
            }else{
                brukt = false;
            }
        }
            
        public int searchClone(int i) //Finner riktig plassering i "points" for denne studenten så riktig student beholder poengsummen og karakteren den først fikk
        {
            int x = poengKlone[i];
            int location = 0;
                for(int j = 0; j<antallStudenter; j++)
                {
                    checkList(BigInteger.valueOf(j));
                        if(poeng[j]==x&&brukt==false)
                    {
                        location=j; //Sender tilbake hvilken adresse neste adressen som ikke er brukt for å ungå karakteren "null"
                    } 
                }
            addToList(BigInteger.valueOf(location)); //Legger til at denne adressen er brukt og ikke skal bli brukt igjen
            return location;
        }
           
        public void utSkrift()//Skriver ut hele greia:)
        {
            System.out.printf("%s%9s%9s%9s\n", "StudentNr", "Poeng", "Prosent", "Karakter");
            for(int counter = 0; counter<antallStudenter; counter++)
            {
             System.out.printf("\t%d%9d%9d%9s\n", studenter[counter], poeng[counter], prosent[counter], karakter[counter]);   
            }
        }   
    }
    Last edited by steinarsen; 11-07-2012 at 06:03 PM.

  2. #2
    steinarsen is offline Member
    Join Date
    Nov 2012
    Posts
    6
    Rep Power
    0

    Default Re: Unwanted Null output.

    And please tell me what the Java BB code is :P

    And tell me what you need to know if it makes finding the problem easier:D

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default Re: Unwanted Null output.

    What output do you actually see?
    How is the above code called?

    You need to sort out some of your indenting, but you found the code tags at least...which is something of a rarity around here.
    :)
    Please do not ask for code as refusal often offends.

  4. #4
    steinarsen is offline Member
    Join Date
    Nov 2012
    Posts
    6
    Rep Power
    0

    Default Re: Unwanted Null output.

    This is displayed in the output:
    Unwanted Null output.-untitled-1.jpg

    Here is how i call the code.

    Java Code:
    package obligatorisk.innlevering.pkg2;
    
    
    public class ObligatoriskInnlevering2 {
    
    
        public static void main(String[] args) {
            Studenter Stud = new Studenter();
            Stud.DelUt();
            Stud.utSkrift();
            Stud.karStudenter();
        }
    }
    Last edited by steinarsen; 11-07-2012 at 06:23 PM.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default Re: Unwanted Null output.

    Your calls are in the wrong order.
    The 'karakter' array is not populated before the utSkrift() call.
    You have it populated afterwards.
    Please do not ask for code as refusal often offends.

  6. #6
    steinarsen is offline Member
    Join Date
    Nov 2012
    Posts
    6
    Rep Power
    0

    Default Re: Unwanted Null output.

    ahh! I changed the order and atleast i get an output now!

    Only problem is that everyone gets an F...

  7. #7
    steinarsen is offline Member
    Join Date
    Nov 2012
    Posts
    6
    Rep Power
    0

    Default Re: Unwanted Null output.

    can anyone see why i only get F`s? I cant figure this shit out and its killing me slowly...

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default Re: Unwanted Null output.

    You'll need to do some debugging.
    Put some println() statements in there printing out what various values are, specifically around the values being put into the bestaat array, since that's what defines what grade is used.
    Please do not ask for code as refusal often offends.

  9. #9
    steinarsen is offline Member
    Join Date
    Nov 2012
    Posts
    6
    Rep Power
    0

    Default Re: Unwanted Null output.

    Quote Originally Posted by Tolls View Post
    You'll need to do some debugging.
    Put some println() statements in there printing out what various values are, specifically around the values being put into the bestaat array, since that's what defines what grade is used.
    Hey! I got it working yesterday already!

    Turns out i had to change

    Java Code:
    public int studentNummer(int i)//Studentnummer
                {
                    int studentNummer = 1 + i;//Her er det viktig at studentnummeret starter på over 30. Om vi starter på for ekesempel 1, får alle studentene F som karakter. Dette er en svakhet i koden.
                    return studentNummer;
                }
    to this

    Java Code:
    public int studentNummer(int i)//Studentnummer
                {
                    int studentNummer = 30 + i;//Her er det viktig at studentnummeret starter på over 30. Om vi starter på for ekesempel 1, får alle studentene F som karakter. Dette er en svakhet i koden.
                    return studentNummer;
                }
    The studenNummer has to be over 30 for it to work.
    This is a weakness in the code, but i see no other way of making it work.

Similar Threads

  1. char array output Null...Help please
    By toppcon in forum New To Java
    Replies: 25
    Last Post: 05-09-2011, 02:16 AM
  2. output giving null, methods not reading properly
    By csuever in forum New To Java
    Replies: 22
    Last Post: 04-01-2011, 02:13 AM
  3. String output showing "null"
    By hayden06f4i in forum New To Java
    Replies: 6
    Last Post: 11-05-2010, 10:21 AM
  4. unwanted bug. JMenu
    By ocean in forum New To Java
    Replies: 7
    Last Post: 11-20-2009, 09:29 PM
  5. Program can run but output all null
    By matt_well in forum New To Java
    Replies: 15
    Last Post: 07-24-2008, 08:48 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •