Page 2 of 2 FirstFirst 12
Results 21 to 28 of 28
Like Tree1Likes

Thread: Boolean help

  1. #21
    alphasil is offline Member
    Join Date
    Jan 2012
    Posts
    33
    Rep Power
    0

    Default Re: Boolean help

    Quote Originally Posted by KevinWorkman View Post
    You really have to think about what you're doing here. What exactly are you printing out? What exactly is aluno? Hint: it's different from Aluno.

    Break this up into smaller pieces. How do you get an instance from an array? Hint: you already did that in an earlier section of the code. How do you get a value contained by an instance? How do you print out a value?
    Hi ppl;

    Agaain thank you for your help
    I think i have managed to get this work like this

    [CODE=java]
    public void listaAlunos()
    {
    for (Aluno alun : alunos)
    {
    System.out.println(alun.getNome());
    }
    }
    [CODE]

    Is that correct like this?


    Now a liitle problem

    Why with this code, respecting the class Turma and Aluno

    [CODE=java]
    public Aluno getAluno(int numAluno)
    {
    for(int i=0; i<alunos.size(); i++)
    {
    Aluno t= alunos.get(i);
    if(t.getID() == numAluno)
    {
    t.getNome();
    }
    }
    return null;
    }
    [CODE]

    If i put 1 as parameter...i have a name "G", but when i use this method i have null as return
    Last edited by alphasil; 10-26-2012 at 03:32 PM.

  2. #22
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,839
    Rep Power
    8

    Default Re: Boolean help

    Again, you have to think about what you're doing in every line. What are you returning? Why would you expect it to return anything other than null?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #23
    alphasil is offline Member
    Join Date
    Jan 2012
    Posts
    33
    Rep Power
    0

    Default Re: Boolean help

    Hi

    You are patient with me, thanks

    So what i'm expecting, if if(t.getID() == numAluno), is to give me the name of student.
    If the id from classe Aluno is the same as (numAluno), i expect his name. i have this inside the loop, outside the loop i have return null,

    it's because return null outside?

  4. #24
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,839
    Rep Power
    8

    Default Re: Boolean help

    When are you returning the name? I see a place where you get the value, but you don't store it in a variable or return it or anything.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #25
    alphasil is offline Member
    Join Date
    Jan 2012
    Posts
    33
    Rep Power
    0

    Default Re: Boolean help

    Ok, made some progress but stuck in final return.

    Java Code:
    public Aluno getAluno(int numAluno)
        {
           for(int i=0; i<alunos.size(); i++)
           {
               Aluno t= alunos.get(i);
               if(t.getID() == numAluno)
               {
                   aluno= t.getNome();
                   System.out.println(aluno);
               }
               else
               {
                 return null;  
               }
            }
            return ??? //What should be the return??
        }
    Thanks

  6. #26
    SJF
    SJF is offline Senior Member
    Join Date
    Oct 2012
    Posts
    108
    Rep Power
    0

    Default Re: Boolean help

    Java Code:
        public ThisClass doSomething(){                    //doSomething() is method name ... ThisClass is return type.
            ThisClass retVal;
            // Doing something to my return value
            return retVal;                                       // retVal is a ThisClass object.  It must be or we'll get a type mismatch error.
        }
    So... you should be returning....

  7. #27
    alphasil is offline Member
    Join Date
    Jan 2012
    Posts
    33
    Rep Power
    0

    Default Re: Boolean help

    Quote Originally Posted by SJF View Post
    Java Code:
        public ThisClass doSomething(){                    //doSomething() is method name ... ThisClass is return type.
            ThisClass retVal;
            // Doing something to my return value
            return retVal;                                       // retVal is a ThisClass object.  It must be or we'll get a type mismatch error.
        }
    So... you should be returning....

    Hi

    Thanks

    I should be returning my Aluno "t" variable but gives me an error, because the Aluno t is inside the loop, i f i put outside it says
    variabel "t" not initialized

    [CODE=java]
    public Aluno getAluno(int numAluno)
    {
    Aluno t;
    for(int i=0; i<alunos.size(); i++)
    {
    t= alunos.get(i); //Trying to give to "t" variable value of alun0
    if(t.getID() == numAluno)
    {
    aluno= t.getNome();
    }
    else
    {
    return null;
    }
    }
    return t;
    }
    [CODE]

    Wrong again....:(

  8. #28
    SJF
    SJF is offline Senior Member
    Join Date
    Oct 2012
    Posts
    108
    Rep Power
    0

    Default Re: Boolean help

    If alunos.size() is 0, t never gets initialized. Try:
    Java Code:
        Aluno t = null;
    outside your loop. It will return null then if the alunos(list?) is empty.


    (close your [code] with [/code ] )
    Last edited by SJF; 10-28-2012 at 12:01 AM.

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Boolean help, again.
    By Zigster in forum New To Java
    Replies: 3
    Last Post: 04-25-2012, 10:20 PM
  2. Boolean
    By Twixmaster123 in forum New To Java
    Replies: 1
    Last Post: 04-13-2012, 08:20 AM
  3. Boolean help
    By FadedAura in forum New To Java
    Replies: 2
    Last Post: 11-20-2011, 01:56 PM
  4. Replies: 3
    Last Post: 09-13-2011, 12:01 AM
  5. boolean error help when no boolean is given
    By drewtrcy in forum New To Java
    Replies: 18
    Last Post: 05-05-2011, 09:04 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
  •