Re: Error in IF-Else Code
Ah a but a simple solution, you cannot have two if statements right next to each other, what you have to do for the second if is add a else behind it like:
else if (String) usrInpt = "F" {
System.out.println("Working on this");
}
else {
System.out.println("ERROR");
If you do that the program should work perfectly, also a suggestion, I'm assuming you didn't start with python so beware of your indents in your code cause when you get to more complex stuff, if its not aligned your code will be pretty unreadable not knowing what goes were even if they're curly braces.
Re: Error in IF-Else Code
Quote:
Originally Posted by
thatguywhoprograms
Ah a but a simple solution, you cannot have two if statements right next to each other, what you have to do for the second if is add a else behind it like:
else if (String) usrInpt = "F" {
System.out.println("Working on this");
}
else {
System.out.println("ERROR");
If you do that the program should work perfectly, also a suggestion, I'm assuming you didn't start with python so beware of your indents in your code cause when you get to more complex stuff, if its not aligned your code will be pretty unreadable not knowing what goes were even if they're curly braces.
What do you mean you can not have two if statements right next to each other?
What's problem in here?
To the OP: The if statement syntax : If StatementStatement ControlJava Tutorial See this.
Re: Error in IF-Else Code
Quote:
if (String) usrInpt = "C"
This is an assignment (=) statement not an equality(==) test.
However, You should use the equals() method to compare Strings. The == operator is for primitives.
Re: Error in IF-Else Code
I done what you said but now I'm getting errors on the if and the else if statements here is my revised code. hat did I do wrong?
Code :
if (String) usrInpt = "C" {
Scanner temp = new Scanner(System.in);
System.out.println("Temp: ");
String celcius = scan.next();
double n = Double.parseDouble(celcius);
double sum = ((n * 1.8) + 32);
}
else if (String) usrInpt = "F" {
System.out.println("Working on this");
}
else {
System.out.println("ERROR");
}
Re: Error in IF-Else Code
Quote:
if (String) usrInpt = "C" {
This is an assignment (=) statement not an equality(==) test.
However, You should use the equals() method to compare Strings. The == operator is for primitives.
Quote:
I'm getting errors
Copy and post here the full text of the error messages.
Re: Error in IF-Else Code
Do you mind giving me an example on how to use the equals() method
Here is the full code
Code :
import java.util.Scanner;
import java.io.*;
public class converter {
private static final boolean String = false;
private static double sum;
public static void main(String args[])
{
Scanner scan = new Scanner(System.in);
String usrInput;
System.out.print("C or F: ");
String usrInpt = scan.next();
if (String) usrInpt = "C" {
Scanner temp = new Scanner(System.in);
System.out.println("Temp: ");
String celcius = scan.next();
double n = Double.parseDouble(celcius);
double sum = ((n * 1.8) + 32);
}
else if (String) usrInpt = "F" {
System.out.println("Working on this");
}
else {
System.out.println("ERROR");
}
System.out.println((double) sum);
}
}
Here is the errors
Code :
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
Syntax error, insert ";" to complete Statement
Syntax error on token(s), misplaced construct(s)
at converter.main(converter.java:17)
Re: Error in IF-Else Code
Which statement is at line 17?
The compiler is saying that the statement on line 17 needs and ending ;
Quote:
how to use the equals() method
You need to understand how to read the API doc that describes how to use a class's methods.
Have you read the API doc for the String class? Do you have any questions about its description of the equals() method?
Locate String in lower left and click the link for the doc
Java Platform SE 6
You should also read about how to code an if statement:
The if-then and if-then-else Statements (The Java™ Tutorials > Learning the Java Language > Language Basics)
Re: Error in IF-Else Code
Quote:
Originally Posted by
Norm
Sorry for replying so late. At the time of my last post I was away from my computer and was working on my phone through a VNC connection to my computer. Anyways Line 17 is
Code :
if (String) usrInpt = "C" {
And I am looking at the docs now, hopefully I can debug this code.
Re: Error in IF-Else Code
It's not a bug. Its a syntax error.
Re: Error in IF-Else Code
Isn't a bug usually a programmer error and since this is my error it would be considered a bug?
Re: Error in IF-Else Code
Re: Error in IF-Else Code
Quote:
Originally Posted by
Norm
Ask google or wikipedia.
Per Wikipedia,
Quote:
A software bug is the common term used to describe an error, flaw, mistake, failure, or fault in a computer program or system that produces an incorrect or unexpected result, or causes it to behave in unintended ways. Most bugs arise from mistakes and errors made by people.
Re: Error in IF-Else Code
Alright I got frustrated and decided to do a complete rewrite. This is what I have so far and it finally compiled and ran successfully.
Code :
import java.util.*;
public class convert {
public static void main(String args[]) {
System.out.println("C or F?: ");
Scanner scan = new Scanner(System.in);
String A = scan.nextLine();
if (A.equals("C")) {
System.out.println("C");
}
else {
System.out.println("I assume you said F");
}
}
}
Re: Error in IF-Else Code
Instead of assuming, you could test if A was "F" and if not, print out an error message.
Re: Error in IF-Else Code
Well I was up till 1 last night trying to get it working and I think I finally did. It will ask for "C or F" Then if C or F is entered it will ask for the temperature and then do the conversion and loop back to the beginning. If anything but C or F was entered then it throws an error.
Code :
import java.util.*;
public class convert {
public static void main(String args[]) {
dothis();
}
public static void dothis(){
System.out.println("");
System.out.println("C or F?: ");
Scanner scan = new Scanner(System.in);
String A = scan.nextLine();
if (A.equals("C")) {
System.out.println("What is the temperature in Celcius: ");
Scanner cel = new Scanner(System.in);
double B = Double.parseDouble(cel.nextLine());
double ans = ((B * 9/5) + 32);
System.out.print((double) ans);
dothis();
} else if (A.equals("F")){
System.out.println("What is the temperature in Farenheit: ");
Scanner fa = new Scanner(System.in);
double F = Double.parseDouble(fa.nextLine());
double Fans = ((F - 32) * 5/9 );
System.out.print((double) Fans);
dothis();
}
else {
System.out.println("ERROR");
}
}
}
Re: Error in IF-Else Code
Code :
if(usrInpt.equals("C")) {
You need to have parenthesis to surround the if statement.
Re: Error in IF-Else Code
Quote:
Originally Posted by
imsuperman05
Code :
if(usrInpt.equals("C")) {
You need to have parenthesis to surround the if statement.
What do you mean? The program runs without error so if I may ask, can I see an example and what will the parenthesis do? I don't mean to sound like a know-it-all, I'm just wondering why I performed an error
Re: Error in IF-Else Code
Quote:
Originally Posted by
jonathon6017
What do you mean? The program runs without error so if I may ask, can I see an example and what will the parenthesis do? I don't mean to sound like a know-it-all, I'm just wondering why I performed an error
Do you even compile the file? It should show errors.
Re: Error in IF-Else Code
Quote:
Originally Posted by
imsuperman05
Do you even compile the file? It should show errors.
The exact code I posted above is what I compiled and ran and it ran with no errors
Re: Error in IF-Else Code
The line should have been
if(usrInpt=="c")
Re: Error in IF-Else Code
Quote:
Originally Posted by
ranjithfs1
The line should have been
if(usrInpt=="c")
FYI Never, unless you have reason to compare objects, use == to compare strings. String's are objects, and thus should be compared using the equals method
Re: Error in IF-Else Code
Quote:
Originally Posted by
copeg
FYI Never, unless you have reason to compare objects, use == to compare strings. String's are objects, and thus should be compared using the equals method
Sorry, the above reply of mine was made seeing the last reply on page 1 of this thread.
But, I always use == to compare a variable(referring to a String) with a string constant in decision making statements. What's wrong with it?
Re: Error in IF-Else Code
Quote:
What's wrong with it?
The == operator tests if the two references point/refer to the same object.
It is possible for there to be two Strings (ie two different objects) with the same contents. The equals method would say they had the same value (true), the == operator would say the references point to different objects (false).
Re: Error in IF-Else Code
Quote:
if (String) usrInpt = "C"
Try this one
if (String) usrInpt = ="C"
replace = with ==
it works...