Jump to content

Java Code Help

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
10 replies to this topic

#1
Badgers

Badgers

    Newbie

  • Members
  • Pip
  • 3 posts
I am trying to write a Java program that converts a temperature a user inputs from Fahrenheit to Celsius or vice versa. The user is asked to enter a temperature in this format:

System.out.println("Enter temp (ex. 70 F or 23 C): ")

From there I read the line and take in the temperature number and the "C" or the "F" to determine which function it should be converted in(f2c() or c2f() ).

I am having problems creating a string that will read the letter to determine Fahrenheit or Celsius.

Can anyone please help me fix this? I would greatly appreciate it.

My code is no where near complete. I know I need 3 functions and a do while loop to complete the statement.
Here is my bad code:

Quote

//Converter.java
import java.util.Scanner;

public class Converter {

public static void main(String[] args) {

Scanner reader = new Scanner(System.in);
int x;
double convertedTemp;
double enteredTemp;
do
System.out.println("Enter temp (ex. 70 F or 23 C): ");
enteredTemp = reader.nextDouble();
String d = input.nextInt();
while (input.hasNext())
} if (Sting d = "C")
System.out.println("The Celsius temperature you entered will equal " + convertedTemp + " degrees Farenheit.");
else if (String d = "F")
System.out.println("The Farenheit temperature you entered will equal " + convertedTemp + " degrees Celsius.");

public f2c(String[] args) {
convertedTemp = (5.0/9) * (enteredTemp - 32);
System.out.println("The Farenheit temperature you entered will equal " + convertedTemp + " degrees Celsius.");

}
public f2c(String[] args) {
convertedTemp = (9.0/5)* enteredTemp + 32;

}
}


I will donate money via PayPal for whoever sends me the corrected code.

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Why are you using reader and input for getting info?
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
Badgers

Badgers

    Newbie

  • Members
  • Pip
  • 3 posts

WingedPanther said:

Why are you using reader and input for getting info?

Because I don't know what I'm doing.

#4
Badgers

Badgers

    Newbie

  • Members
  • Pip
  • 3 posts
Here was the original program I wrote:

//ConverterTemp.java
import java.util.Scanner;

public class ConverterTemp {

public static void main(String[] args) {

Scanner reader = new Scanner(System.in);

double convertedTemp;
double enteredTemp;
double tempType;

System.out.println("Enter temp (ex. 70 F or 23 C): ");
enteredTemp = reader.nextDouble();
tempType = reader.nextInt();

if(tempType == 1)
{ convertedTemp = (5.0/9) * (enteredTemp - 32);
System.out.println("The Farenheit temperature you entered will equal " + convertedTemp + " degrees Celsius.");
}

if(tempType == 2)
{
convertedTemp = (9.0/5)* enteredTemp + 32;
System.out.println("The Celsius temperature you entered will equal " + convertedTemp + " degrees Farenheit.");
}
}
}

I simply read the line as temp and then a 1 or 2 (i.e. 212 1 would be 212 F).

I am now having a problems adding in the while/do and three total functions(where f2c and c2f return the value) and reading the 2nd input as a char into a string.

#5
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
You're reading an integer instead of a char/string
String d = input.nextInt();
When you type "C" or "F" here, the scanner will just wait until you finally give an integer.

#6
atomicpineapple

atomicpineapple

    Newbie

  • Members
  • Pip
  • 5 posts
Hi,
Here's my code. I did it using ConsoleReader class instead of Scanner. Hope that's okay. It works great. Here are the two files;
tempconverter:

Quote

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

ConsoleReader console = new ConsoleReader(System.in);
double convertedTemp;
String stringtemp= "";
double enteredTemp2 = 0;
String enteredTemp;
System.out.println("Enter temp (ex. 70 F or 23 C): ");
enteredTemp = console.readLine();
int len = enteredTemp.length();
int i = 0;
while (i < len)
{
if (enteredTemp.charAt(i)== 'F')
{
convertedTemp = (5.0/9) * (enteredTemp2 - 32);
System.out.println("The Farenheit temperature you entered will equal " + convertedTemp + " degrees Celsius.");
}

else if (enteredTemp.charAt(i)== 'C')
{
convertedTemp = (enteredTemp2*(9.0/5)) + 32;
System.out.println("The Celsius temperature you entered will equal " + convertedTemp + " degrees Farenheit.");
}
else
{
if ((enteredTemp.substring(i,i+1).equals(" ")))
{
}
else
{
stringtemp = stringtemp + enteredTemp.charAt(i);
enteredTemp2 = Integer.parseInt(stringtemp);
}
}
i++;
}
}
}
And the ConsoleReader file you will need:

Quote

import java.io.InputStreamReader;
import java.io.InputStream;
import java.io.BufferedReader;
import java.io.IOException;

public class ConsoleReader {
private BufferedReader reader;

public ConsoleReader(InputStream inStream) {
reader = new BufferedReader(new InputStreamReader(inStream));
}

public String readLine() {
String inputLine = "";

try {
inputLine = reader.readLine();
}
catch(IOException e) {
System.out.println(e);
System.exit(1);
}

return inputLine;

}

public int readInt() {
String inputString = readLine();
int n = Integer.parseInt(inputString);
return n;
}

public double readDouble() {
String inputString = readLine();
double x = Double.parseDouble(inputString);
return x;
}

}

If this works for you let me know and I can PM you my paypal info

#7
Root23

Root23

    Programmer

  • Members
  • PipPipPipPip
  • 144 posts
I was going to try and do this real quick, using the scanline like you did.. and now I have a question for anyone who can answer it..

My code was this:

import java.util.Scanner;


public class Converter {


	public static void main(String[] args) {

		//initialize variable

		String strTemp;

		double fahrenheit; 

		double celsius;

		double convertedF; 

		double convertedC;

		//print out message for user input

		System.out.println("Enter a temperature to convert. Example: 10F, or 10C");

		//initialize new instance of Scanner

		Scanner scan = new Scanner(System.in);

		//scans line of entered text

		strTemp = scan.nextLine();

		if(strTemp.toUpperCase().endsWith("F")){

			strTemp.replace("\\D", "");

			fahrenheit = Double.parseDouble(strTemp);

			convertedF = (5.0/9.0)*(fahrenheit-32);

			System.out.println("Your converted temp is " + convertedF);

		} else if(strTemp.toUpperCase().endsWith("C")){

				strTemp.replace("\\D","");

				celsius = Double.parseDouble(strTemp);

				convertedC = (9.0/5.0)*celsius+32;

				System.out.println("Your converted temp is " + convertedC);

		}

	}

}

If the user enter a a temp with F at the end, it works great. If it is a C it does not work though. What did I do wrong? The line it doesn't seem to like is 'celsius = Double.parseDouble(strTemp);' Which shouldn't be a problem if the C is being removed.. I don't see why it wouldn't be. Ideas as to what the problem is?
Posted Image

#8
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts

Quote

if the C is being removed
There it is. IF it's removed. Which it isn't.

The reason the Fahrenheit works, isn't because the F is removed, but in java an f behind a number indicates it's a float number. which can be stored in a double.
My question is: why do you replace '\d' ? What is \d?

Note that the replace function returns a string, so you must catch it. Even if the replace replaced something it wouldn't have changed strTemp.

strTemp = strTemp.replace("F", "");
And replace 'C' for the celcius part

#9
Root23

Root23

    Programmer

  • Members
  • PipPipPipPip
  • 144 posts
I used \\D because I read that it would replace all 'non-digits'. I'll try it the way you mentioned though.
Posted Image

#10
pradap

pradap

    Newbie

  • Members
  • Pip
  • 1 posts
i thought this will be correct...

public class tempconverter {public static void main(String[] args) {
ConsoleReader console = new ConsoleReader(System.in);
double convertedTemp;
String stringtemp= "";
double enteredTemp2 = 0;
String enteredTemp;
System.out.println("Enter temp (ex. 70 F or 23 C): ");
enteredTemp = console.readLine();
int len = enteredTemp.length();
int i = 0;
while (i < len)
{
if (enteredTemp.charAt(i)== 'F')
{
convertedTemp = (5.0/9) * (enteredTemp2 - 32);
System.out.println("The Farenheit temperature you entered will equal " + convertedTemp + " degrees Celsius.");
}

else if (enteredTemp.charAt(i)== 'C')
{
convertedTemp = (enteredTemp2*(9.0/5)) + 32;
System.out.println("The Celsius temperature you entered will equal " + convertedTemp + " degrees Farenheit.");
}
else
{
if ((enteredTemp.substring(i,i+1).equals(" ")))
{
}
else
{
stringtemp = stringtemp + enteredTemp.charAt(i);
enteredTemp2 = Integer.parseInt(stringtemp);
}
}
i++;
}
}
}

Edited by dargueta, 18 October 2010 - 08:54 PM.
Removed spam-ish links


#11
bobiecayao

bobiecayao

    Newbie

  • Members
  • Pip
  • 1 posts
Its kinda hard easy, i think we can help about this problem

If you are looking for a document generation system on Java, take a look at this Java document generation site. It has basic info on all the vendors. It makes for a great starting point."