Results 1 to 16 of 16
Thread: 'void' type not allowed here
- 10-01-2008, 09:39 PM #1
Member
- Join Date
- Oct 2008
- Posts
- 33
- Rep Power
- 0
- 10-01-2008, 09:46 PM #2
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
What is the return type of the method "olesKonto.uttak(String)"? void maybe? Was it maybe suppossed to be String?
- 10-01-2008, 09:56 PM #3
Member
- Join Date
- Oct 2008
- Posts
- 33
- Rep Power
- 0
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 beløp) {
saldo = saldo + beløp;
}
public void uttak(double beløp) {
saldo = saldo - beløp;
}
}
-
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).
- 10-02-2008, 05:32 PM #5
Member
- Join Date
- Oct 2008
- Posts
- 33
- Rep Power
- 0
How do I do it? Cant seem to d
find out.
- 10-02-2008, 05:56 PM #6
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
The method above is a void method.Java Code:public void someMethod() { }
This is the same method with a String return type. Note the differences and make the same to your code.Java Code:public String someMethod() { return "some String"; }
- 10-02-2008, 08:01 PM #7
Member
- Join Date
- Oct 2008
- Posts
- 33
- Rep Power
- 0
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 beløp) {
saldo = saldo + beløp;
}
public String uttak(double beløp) {
saldo = saldo - beløp;
}
}
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("Før endring av dataene: \nKontonr: " + kontonr +
", navn: " + navn + ", saldo: " + saldo);
// olesKonto.innskudd(12000);
// olesKonto.uttak(5000);
String tekst = showInputDialog("Hvor mye settes inn:");
double beløp = Double.parseDouble(tekst);
showMessageDialog(null, "Dette tilsvarer " + olesKonto.innskudd(beløp) + " 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());
}
}
- 10-02-2008, 08:15 PM #8
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
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?
- 10-02-2008, 08:17 PM #9
Senior Member
- Join Date
- Sep 2008
- Posts
- 564
- Rep Power
- 5
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-03-2008, 07:04 PM #10
Member
- Join Date
- Oct 2008
- Posts
- 33
- Rep Power
- 0
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 beløp) {
if (saldo < 0) // withdraw value is negative
{
System.out.println ("Du har ikke nok penger på konto..");
}
else
if (beløp > saldo) // withdraw value exceeds balance
{
System.out.println ("Du har ikke nok penger på konto..");
}
else
saldo = saldo - beløp;
return saldo;
- 10-03-2008, 08:16 PM #11
Can you explain it some more?I got this problem
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?
- 10-03-2008, 08:22 PM #12
Member
- Join Date
- Oct 2008
- Posts
- 33
- Rep Power
- 0
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.
- 10-03-2008, 08:34 PM #13
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?
- 10-04-2008, 10:52 PM #14
Member
- Join Date
- Oct 2008
- Posts
- 33
- Rep Power
- 0
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 beløp) {
saldo = saldo + beløp;
return saldo;
}
public double uttak(double beløp) {
if (beløp > saldo) // withdraw value exceeds balance
{
System.out.println ("Du har ikke nok penger på konto til uttak..");
return saldo;
}
else
saldo = saldo - beløp;
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("Før endring av dataene: \nKontonr: " + kontonr +
", navn: " + navn + ", saldo: " + saldo);
String tekst = showInputDialog("Hvor mye settes inn:");
double beløp = Double.parseDouble(tekst);
showMessageDialog(null, "Du har nå " + olesKonto.innskudd(beløp) + " NOK disponibelt på konto.");
String tekst1 = showInputDialog("Hvor mye taes ut:");
double beløp1 = Double.parseDouble(tekst1);
showMessageDialog(null, "Du har nå " + olesKonto.uttak(beløp1) + " NOK disponibelt på konto.");
System.out.println(olesKonto.toString());
}
}
- 10-04-2008, 10:57 PM #15
Senior Member
- Join Date
- Aug 2008
- Location
- Stockholm, Sweden
- Posts
- 119
- Rep Power
- 0
Look up while and how that works.
- 10-04-2008, 11:08 PM #16
Member
- Join Date
- Oct 2008
- Posts
- 33
- Rep Power
- 0
Similar Threads
-
The different of static void,protected,and void in methods?
By Winarto in forum New To JavaReplies: 5Last Post: 01-24-2008, 11:53 PM -
No duplicates allowed in Sets
By Java Tip in forum Java TipReplies: 0Last Post: 01-21-2008, 04:33 PM -
is void a type?
By mary in forum New To JavaReplies: 3Last Post: 08-01-2007, 08:12 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks