Results 1 to 11 of 11
Thread: 2 converting errors
- 03-06-2009, 02:48 AM #1
Member
- Join Date
- May 2008
- Posts
- 7
- Rep Power
- 0
2 converting errors
hey guys,
i need your help. i am writing a torch program.
getting those errors while compiling:
Type mismatch: cannot convert from int to char
Type mismatch: cannot convert from int to boolean
pls. check the lines in my code. i would be thankful if you guys can help me out.
graciasJava Code:package lamp; import java.io.*; public class Lamp { public Lamp(){ LampSwitch = 0; Lamp = 1; Battery = 1; } public void LampOff(){ LampSwitch = 0; } public void LampOn(){ LampSwitch = 1; } public int glowing(){ if(Lamp == 1 && LampSwitch == 1 && Battery == 1){ return 1; } else{ return 0; } } private int Battery; private int Lamp; private int LampSwitch; public static void main(String[] args){ char z = 'a'; //int i = (int) z; Lamp t1; t1 = new Lamp(); System.out.println("e..on a..off x..end\n"); do{ z = System.in.read(); // ERROR 1: Type mismatch: cannot convert from int to char if(z == 'e') t1.LampOn(); if(z == 'a') t1.LampOff(); if(t1.glowing() == 1){ System.out.println("Lamp on."); } else{ System.out.println("Lamp off."); } }while(z!='x'); } }Last edited by Juicer; 03-06-2009 at 01:23 PM.
-
You're not going to get very far trying to get user input using naked System.in. Just don't do it. Instead you might wish to read up on getting input from the user using a Scanner object.
- 03-06-2009, 08:51 AM #3
Senior Member
- Join Date
- Aug 2008
- Posts
- 384
- Rep Power
- 5
Java Code:if(t1.Glowing()){ // Error 2: Type mismatch: cannot convert from int to booleanThe method Glowing() (which should be glowing(), btw) returns an int. This means you can't use t1.Glowing(), since that only works with a boolean.Java Code:public int Glowing(){
Either use
or useJava Code:if (t1.Glowing() == 1)
Java Code:public int Glowing(){ return (Lamp == 1 && LampSwitch == 1 && Battery == 1); }
Btw, please read The Java Code ConventionsI die a little on the inside...
Every time I get shot.
- 03-06-2009, 12:47 PM #4
I do declare...
You need to declare the type of your variables (in the constructor)... int's anybody?Java Code:LampSwitch = 0; Lamp = 1; Battery = 1;
Luck,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 03-06-2009, 12:56 PM #5
Member
- Join Date
- May 2008
- Posts
- 7
- Rep Power
- 0
thanks for your reply guys!
why naked? i have also tried the scanner system, but it didnt work for me.
@Supamagier
true-false system didnt work. only this thing worked for me: if (t1.Glowing() == 1)
why does this work in c++ and not in java?
@ CJSLMAN
they are already in the constructor.
- 03-06-2009, 01:11 PM #6
huh... has your code changed? If so please post. The example that's posted your variable don't have a type defined.@ CJSLMAN
they are already in the constructor
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 03-06-2009, 01:25 PM #7
Member
- Join Date
- May 2008
- Posts
- 7
- Rep Power
- 0
not that much, but you can check it. i have updated my first post.
you said that i m missing some variables in the constructor, but they are there... see:
Java Code:public Lamp(){ LampSwitch = 0; Lamp = 1; Battery = 1; }
- 03-06-2009, 01:53 PM #8
Member
- Join Date
- May 2008
- Posts
- 7
- Rep Power
- 0
i tried now something different.
instead of the line:
i tried this one:Java Code:z = System.in.read();
and now the error is no more, but another error is there:Java Code:z = (char)System.in.read();
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Unhandled exception type IOException
at lamp.Lamp.main(Lamp.java:39)
- 03-06-2009, 02:17 PM #9
Member
- Join Date
- May 2008
- Posts
- 7
- Rep Power
- 0
problem solved... i tried with exceptions and its working. thanks everyone and also to this guy who helped me with exceptions.
i just replaced this line:with this one:Java Code:public static void main(String[] args){
graciasJava Code:public static void main(String[] args) throws Exception{
- 03-06-2009, 02:28 PM #10
huh, no, I never said that you were missing some variables in the constructor.... I said your variables were missing their "type" declaration, but I just saw that they have been declared as class variables, so forget the comment.you said that i m missing some variables in the constructor, but they are there... see:
Luck,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 03-06-2009, 02:58 PM #11
Member
- Join Date
- May 2008
- Posts
- 7
- Rep Power
- 0
Similar Threads
-
Converting an App to Applet
By josephdcoleman in forum New To JavaReplies: 1Last Post: 02-21-2009, 07:07 AM -
Converting asp files to jsp
By vrk in forum Advanced JavaReplies: 5Last Post: 02-11-2009, 09:44 AM -
help with these errors
By oceansdepth in forum New To JavaReplies: 3Last Post: 04-16-2008, 04:55 PM -
Converting URL to URI
By Java Tip in forum Java TipReplies: 0Last Post: 12-26-2007, 10:15 AM -
help with converting to JApplet
By Simmy in forum AWT / SwingReplies: 2Last Post: 08-09-2007, 08:45 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks