Results 1 to 11 of 11
  1. #1
    Juicer is offline Member
    Join Date
    May 2008
    Posts
    7
    Rep Power
    0

    Default 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.

    Java 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');
    		}
    	}
    gracias
    Last edited by Juicer; 03-06-2009 at 01:23 PM.

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

    Default

    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.

  3. #3
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    7

    Default

    Java Code:
    if(t1.Glowing()){ // Error 2:         Type mismatch: cannot convert from int to boolean
    Java Code:
    public int Glowing(){
    The 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.

    Either use
    Java Code:
    if (t1.Glowing() == 1)
    or use
    Java Code:
    public int Glowing(){
        return (Lamp == 1 && LampSwitch == 1 && Battery == 1);	
    }

    Btw, please read The Java Code Conventions
    I die a little on the inside...
    Every time I get shot.

  4. #4
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default I do declare...

    Java Code:
    LampSwitch = 0;
    Lamp = 1;
    Battery = 1;
    You need to declare the type of your variables (in the constructor)... int's anybody?

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  5. #5
    Juicer is offline Member
    Join Date
    May 2008
    Posts
    7
    Rep Power
    0

    Default

    thanks for your reply guys!

    Quote Originally Posted by Fubarable View Post
    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.
    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.

  6. #6
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default

    @ CJSLMAN
    they are already in the constructor
    huh... has your code changed? If so please post. The example that's posted your variable don't have a type defined.

    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  7. #7
    Juicer is offline Member
    Join Date
    May 2008
    Posts
    7
    Rep Power
    0

    Default

    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; 
    		}

  8. #8
    Juicer is offline Member
    Join Date
    May 2008
    Posts
    7
    Rep Power
    0

    Default

    i tried now something different.
    instead of the line:

    Java Code:
    z = System.in.read();
    i tried this one:
    Java Code:
    z = (char)System.in.read();
    and now the error is no more, but another error is there:
    Exception in thread "main" java.lang.Error: Unresolved compilation problem:
    Unhandled exception type IOException

    at lamp.Lamp.main(Lamp.java:39)

  9. #9
    Juicer is offline Member
    Join Date
    May 2008
    Posts
    7
    Rep Power
    0

    Default

    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:
    Java Code:
    public static void main(String[] args){
    with this one:
    Java Code:
    public static void main(String[] args) throws Exception{
    gracias

  10. #10
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default

    you said that i m missing some variables in the constructor, but they are there... see:
    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.

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  11. #11
    Juicer is offline Member
    Join Date
    May 2008
    Posts
    7
    Rep Power
    0

Similar Threads

  1. Converting an App to Applet
    By josephdcoleman in forum New To Java
    Replies: 1
    Last Post: 02-21-2009, 07:07 AM
  2. Converting asp files to jsp
    By vrk in forum Advanced Java
    Replies: 5
    Last Post: 02-11-2009, 09:44 AM
  3. help with these errors
    By oceansdepth in forum New To Java
    Replies: 3
    Last Post: 04-16-2008, 04:55 PM
  4. Converting URL to URI
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-26-2007, 10:15 AM
  5. help with converting to JApplet
    By Simmy in forum AWT / Swing
    Replies: 2
    Last Post: 08-09-2007, 08:45 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
  •