Results 1 to 16 of 16
  1. #1
    Torgero is offline Member
    Join Date
    Oct 2008
    Posts
    33
    Rep Power
    0

    Default 'void' type not allowed here

    I cant seem to find out whats wrong. Anyone who could help me?

    Error:
    KontoTest.java:81: 'void' type not allowed here
    showMessageDialog(null, "Dette tilsvarer " + olesKonto.uttak(Uttak) + " NOK.");

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    What is the return type of the method "olesKonto.uttak(String)"? void maybe? Was it maybe suppossed to be String?

  3. #3
    Torgero is offline Member
    Join Date
    Oct 2008
    Posts
    33
    Rep Power
    0

    Default This is the methods for the class Konto

    import static javax.swing.JOptionPane.*;


    class Konto {
    private long kontonr;
    private String navn;
    private double saldo;

    public Konto(long startKontonr, String startNavn, double startSaldo) {
    kontonr = startKontonr;
    navn = startNavn;
    saldo = startSaldo;
    }

    public long finnKontonr() {
    return kontonr;
    }

    public String finnNavn() {
    return navn;
    }

    public double finnSaldo() {
    return saldo;
    }

    public void innskudd(double belp) {
    saldo = saldo + belp;
    }

    public void uttak(double belp) {
    saldo = saldo - belp;


    }
    }

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by masijade View Post
    What is the return type of the method "olesKonto.uttak(String)"? void maybe? Was it maybe suppossed to be String?
    Looks like you were right on the money. It is a void return type.

    To the original poster, you can't expect a method that returns void to be able to return a String. Either you have to use the method differently (call it without expecting a return type), or rewrite it to return the appropriate object (here a String).

  5. #5
    Torgero is offline Member
    Join Date
    Oct 2008
    Posts
    33
    Rep Power
    0

    Default

    How do I do it? Cant seem to d
    find out.

  6. #6
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Java Code:
    public void someMethod() {
    }
    The method above is a void method.

    Java Code:
    public String someMethod() {
        return "some String";
    }
    This is the same method with a String return type. Note the differences and make the same to your code.

  7. #7
    Torgero is offline Member
    Join Date
    Oct 2008
    Posts
    33
    Rep Power
    0

    Default

    Ok, tried it and this was the new error:

    java:30: missing return statement
    }


    import static javax.swing.JOptionPane.*;


    class Konto {
    private long kontonr;
    private String navn;
    private double saldo;

    public Konto(long startKontonr, String startNavn, double startSaldo) {
    kontonr = startKontonr;
    navn = startNavn;
    saldo = startSaldo;
    }

    public long finnKontonr() {
    return kontonr;
    }

    public String finnNavn() {
    return navn;
    }

    public double finnSaldo() {
    return saldo;
    }

    public String innskudd(double belp) {
    saldo = saldo + belp;
    }

    public String uttak(double belp) {
    saldo = saldo - belp;


    }
    }


    class KontoTest {
    public static void main(String[] args) {


    Konto olesKonto = new Konto(123456676756L, "Ole Olsen", 2300.50);


    long kontonr = olesKonto.finnKontonr();
    String navn = olesKonto.finnNavn();
    double saldo = olesKonto.finnSaldo();

    System.out.println("Fr endring av dataene: \nKontonr: " + kontonr +
    ", navn: " + navn + ", saldo: " + saldo);


    // olesKonto.innskudd(12000);
    // olesKonto.uttak(5000);

    String tekst = showInputDialog("Hvor mye settes inn:");
    double belp = Double.parseDouble(tekst);
    showMessageDialog(null, "Dette tilsvarer " + olesKonto.innskudd(belp) + " NOK.");



    kontonr = olesKonto.finnKontonr();
    navn = olesKonto.finnNavn();
    saldo = olesKonto.finnSaldo();
    System.out.println("Etter endring av dataene: \nKontonr: " + kontonr +
    ", navn: " + navn + ", saldo: " + saldo);


    System.out.println(olesKonto.toString());
    }
    }

  8. #8
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Take a closer look at my two example. I did more than just change the return type, I added a return statement as well, did you do that?

  9. #9
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    6

    Default

    you need a return statement. At the end of the method, you need to use the "return" keyword followed by the String object (or whatever return type your method has). Remember to put this after all necessary code is run.

    The reason this wasn't a problem before is that a void method does not return anything, so a return statement is not explicitly needed. However, if there is a returned parameter, you must explicitly state what you want to return.

    example:

    String foo() { return new String("bar"); }

  10. #10
    Torgero is offline Member
    Join Date
    Oct 2008
    Posts
    33
    Rep Power
    0

    Default

    Ok, I finally made that work. I got this problem and that is how to withdraw money from a account and if the withdraw exceds the money in the account plus 1000 NOK then, you wont be able withdraw.. This is I got so far...

    public double uttak(double belp) {

    if (saldo < 0) // withdraw value is negative
    {

    System.out.println ("Du har ikke nok penger p konto..");

    }
    else
    if (belp > saldo) // withdraw value exceeds balance
    {

    System.out.println ("Du har ikke nok penger p konto..");

    }
    else
    saldo = saldo - belp;

    return saldo;

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    I got this problem
    Can you explain it some more?
    What is the uttak method supposed to do?
    Your if/else code tests for 3 conditions. For one it returns a value.
    What is it supposed to do for the other 2 conditions?

  12. #12
    Torgero is offline Member
    Join Date
    Oct 2008
    Posts
    33
    Rep Power
    0

    Default

    Uttak is the same as withdraw money..

    1. If you have enough money in the account you can withdraw that.

    2. If you have 1 or more you can withdraw 1 + the credit limit which is 1000.. AKA 1001..

    3. If you have - (minus) on the account you cant withdraw at all.

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    What is the uttak method supposed to do or return for each of the three cases?
    It must either return an amount or throw an exception. It's up to the programmer to decide which and then to code it in his program.
    What makes sense for the method to do if there is not enough money?

  14. #14
    Torgero is offline Member
    Join Date
    Oct 2008
    Posts
    33
    Rep Power
    0

    Default

    How can I make this program loop, so that I can withdraw and increase money a lot of times...?

    import static javax.swing.JOptionPane.*;


    class Konto {
    private long kontonr;
    private String navn;
    private double saldo;

    public Konto(long startKontonr, String startNavn, double startSaldo) {
    kontonr = startKontonr;
    navn = startNavn;
    saldo = startSaldo;
    }

    public String toString() {
    return "Kontonr.: " + kontonr + "\nNavn: " + navn + "\nSaldo: " + saldo;
    }

    public long finnKontonr() {
    return kontonr;
    }

    public String finnNavn() {
    return navn;
    }

    public double finnSaldo() {
    return saldo;
    }

    public double innskudd(double belp) {
    saldo = saldo + belp;
    return saldo;


    }

    public double uttak(double belp) {

    if (belp > saldo) // withdraw value exceeds balance
    {

    System.out.println ("Du har ikke nok penger p konto til uttak..");
    return saldo;

    }
    else
    saldo = saldo - belp;
    return saldo;
    }

    }

    class KontoTest {
    public static void main(String[] args) {


    Konto olesKonto = new Konto(123456676756L, "Ole Olsen", 1000);


    long kontonr = olesKonto.finnKontonr();
    String navn = olesKonto.finnNavn();
    double saldo = olesKonto.finnSaldo();

    System.out.println("Fr endring av dataene: \nKontonr: " + kontonr +
    ", navn: " + navn + ", saldo: " + saldo);

    String tekst = showInputDialog("Hvor mye settes inn:");
    double belp = Double.parseDouble(tekst);
    showMessageDialog(null, "Du har n " + olesKonto.innskudd(belp) + " NOK disponibelt p konto.");

    String tekst1 = showInputDialog("Hvor mye taes ut:");
    double belp1 = Double.parseDouble(tekst1);
    showMessageDialog(null, "Du har n " + olesKonto.uttak(belp1) + " NOK disponibelt p konto.");




    System.out.println(olesKonto.toString());
    }
    }

  15. #15
    ojn
    ojn is offline Senior Member
    Join Date
    Aug 2008
    Location
    Stockholm, Sweden
    Posts
    119
    Rep Power
    0

    Default

    Look up while and how that works.

  16. #16
    Torgero is offline Member
    Join Date
    Oct 2008
    Posts
    33
    Rep Power
    0

Similar Threads

  1. Replies: 5
    Last Post: 01-24-2008, 11:53 PM
  2. No duplicates allowed in Sets
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-21-2008, 04:33 PM
  3. is void a type?
    By mary in forum New To Java
    Replies: 3
    Last Post: 08-01-2007, 08:12 PM

Posting Permissions

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