New Posts  All Forums:Forum Nav:

Need help with Java

post #1 of 4
Thread Starter 
I keep getting this error while trying to use a scanner with my tempConverter program. CPKH-DATA\\474404$\\My Documents\\JCreator LE\\MyProjects\empConverter\\src\empConverter.java:23: incompatible types
found : java.lang.String
required: int
int fahrenheitTemp = scan.nextLine();
.................................................. ....^


Heres the code;

import java.util.Scanner;


public class tempConverter
{


........public static void main(String[] args)
.........{
............final int BASE = 32;
............final int CONVERSION_FACTOR = 5 / 9;

............Scanner scan = new Scanner(System.in);

............int fahrenheitTemp = scan.nextLine();
............double celsiusTemp;

............celsiusTemp = (fahrenheitTemp - BASE) * CONVERSION_FACTOR;

............System.out.println("Fahrenheit Temperature: " + fahrenheitTemp);
............System.out.println("Celsius Equivalent: " + celsiusTemp);
.......}
}

Figured it out. It needed to be 5.0/9.0 not 5/9
Edited by RileyD - 5/26/11 at 5:26am
    
CPUMotherboardGraphicsRAM
AMD Phenom II x4 955 Gigabyte GA-890GPA-UD3H XFX 5850 G.SKILL Ripjaws Series 6GB 
Hard DriveOSMonitorKeyboard
WD Black 1TB, WD Blue 500gb and Samsung 2TB Windows 7 Ultimate 64 bit. Asus VH232H x3 Filco 87 key MX cherry blue & Model M 
PowerCaseMouseMouse Pad
OCZ 700 Corsair Obsidian 800D Steelseries Xai Steelseries Qck 
  hide details  
Reply
    
CPUMotherboardGraphicsRAM
AMD Phenom II x4 955 Gigabyte GA-890GPA-UD3H XFX 5850 G.SKILL Ripjaws Series 6GB 
Hard DriveOSMonitorKeyboard
WD Black 1TB, WD Blue 500gb and Samsung 2TB Windows 7 Ultimate 64 bit. Asus VH232H x3 Filco 87 key MX cherry blue & Model M 
PowerCaseMouseMouse Pad
OCZ 700 Corsair Obsidian 800D Steelseries Xai Steelseries Qck 
  hide details  
Reply
post #2 of 4
Quote:
incompatible types
found : java.lang.String
required: int
int fahrenheitTemp = scan.nextLine();
Didn't you get the error?
Let me rewrite one line from your program
Quote:
int fahrenheitTemp=Integer.parseInt(scan.nextLine());
Everytime you get input from program, it is usually a STRING. you cannot do implicit conversion by doing INT x= Y, where Y equal STRING.

To do proper conversion you have to use function to convert the string into integer by using Integer.parseInt function.

I would suggests you learn debugging to see the value of the variable.

PS:Was writing a scathing reply for your question, but i take pity on you
Outdated
(13 items)
 
  
CPUMotherboardGraphicsRAM
E8200 2.6 Ghz Asrock g31M-S R2 ATi 3870 2GB Kingston Value RAM 
Hard DriveOptical DriveOSMonitor
2TB WD Green+ 640GB WD Blue None Win 7 Ultimate 21.5 Dell 
KeyboardPowerCaseMouse
HP Wireless Keyboard 550W Cooler Master None Logitech cheap mouse 
Mouse Pad
none 
  hide details  
Reply
Outdated
(13 items)
 
  
CPUMotherboardGraphicsRAM
E8200 2.6 Ghz Asrock g31M-S R2 ATi 3870 2GB Kingston Value RAM 
Hard DriveOptical DriveOSMonitor
2TB WD Green+ 640GB WD Blue None Win 7 Ultimate 21.5 Dell 
KeyboardPowerCaseMouse
HP Wireless Keyboard 550W Cooler Master None Logitech cheap mouse 
Mouse Pad
none 
  hide details  
Reply
post #3 of 4
Make sure when you are using classes you are not familiar with that you understand the return types of its methods. You'll learn to love JavaDocs over time don't worry

http://download.oracle.com/javase/1....api/index.html
post #4 of 4
............final int CONVERSION_FACTOR = 5 / 9; <--- this is 0, make it a double.
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming