• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

syntax and tricky error in java (ifandelse)

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
//PROBLEM3 - if x has a value of 4, print "ddd", else print "eee"
result = "";
if (x == 4);
result = result + "ddd";
else
result = result + "eee";
System.out.println("3.\t" + "result");
 
Ranch Hand
Posts: 250
1
Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your problem is a misplaced semicolon.
 
Luan Vo
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Joel Christophel wrote:Your problem is a misplaced semicolon.

actually the problem is like this

That is the original prob
 
Joel Christophel
Ranch Hand
Posts: 250
1
Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Luan Vo wrote:

Joel Christophel wrote:Your problem is a misplaced semicolon.

actually the problem is like this
//PROBLEM3 - if x has a value of 4, print "ddd", else print "eee"
result = "";
if (x == 4)
result = result + "ddd";
else
result = result + "eee";
System.out.println("3.\t" + result);
That is the original prob



Well that should work now that there is no semicolon after the if statement. Does it not?
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is highly recommended to use braces for the if and else parts even if they are single statements.

Otherwise, your code is harder to read and can be fragile. (and is prone to errors)
 
Luan Vo
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Joel Christophel wrote:

Luan Vo wrote:

Joel Christophel wrote:Your problem is a misplaced semicolon.

actually the problem is like this
//PROBLEM3 - if x has a value of 4, print "ddd", else print "eee"
result = "";
if (x == 4)
result = result + "ddd";
else
result = result + "eee";
System.out.println("3.\t" + result);
That is the original prob



Well that should work now that there is no semicolon after the if statement. Does it not?


Nope there are three error in this problem
Number 1 is (reached end of file while parsing
'else' without 'if') it's on the column result = result + "ddd";
number 2 is (cannot find the symbol and location) on conlumn ( result = result + "eee";
#3 is ( canot not find the symbol: class out and location: class java.lang.system) on System.out.println("3.... + result)
 
Luan Vo
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:It is highly recommended to use braces for the if and else parts even if they are single statements.

Otherwise, your code is harder to read and can be fragile. (and is prone to errors)


I just added braces to if and else. But still the same errors
 
Joel Christophel
Ranch Hand
Posts: 250
1
Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Luan Vo wrote:

Joel Christophel wrote:

Luan Vo wrote:

Joel Christophel wrote:Your problem is a misplaced semicolon.

actually the problem is like this
//PROBLEM3 - if x has a value of 4, print "ddd", else print "eee"
result = "";
if (x == 4)
result = result + "ddd";
else
result = result + "eee";
System.out.println("3.\t" + result);
That is the original prob



Well that should work now that there is no semicolon after the if statement. Does it not?


Nope there are three error in this problem
Number 1 is (reached end of file while parsing
'else' without 'if') it's on the column result = result + "ddd";
number 2 is (cannot find the symbol and location) on conlumn ( result = result + "eee";
#3 is ( canot not find the symbol: class out and location: class java.lang.system) on System.out.println("3.... + result)



Well have you declared the variable result as a String?
 
Luan Vo
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Joel Christophel wrote:

Luan Vo wrote:

Joel Christophel wrote:

Luan Vo wrote:

Joel Christophel wrote:Your problem is a misplaced semicolon.

actually the problem is like this
//PROBLEM3 - if x has a value of 4, print "ddd", else print "eee"
result = "";
if (x == 4)
result = result + "ddd";
else
result = result + "eee";
System.out.println("3.\t" + result);
That is the original prob



Well that should work now that there is no semicolon after the if statement. Does it not?


Nope there are three error in this problem
Number 1 is (reached end of file while parsing
'else' without 'if') it's on the column result = result + "ddd";
number 2 is (cannot find the symbol and location) on conlumn ( result = result + "eee";
#3 is ( canot not find the symbol: class out and location: class java.lang.system) on System.out.println("3.... + result)



Well have you declared the variable result as a String?

Yes i did at the begginng.
here is my java
import java.util.*;

public class MinilabIfElse
{
public static void main(String args[])
{
//declare variables
String result;
int x;
int inputx;

//prompt for, read in a value for x
Scanner kb = new Scanner(System.in);
System.out.print("Please enter a value for x (integer): ");
inputx = kb.nextInt();

//************************************************************************
//The if-else statements in this section have syntax errors (will not compile).
//Fix each of them so that it compiles and the proper result is printed.
//************************************************************************
System.out.println("\n\n\"Common syntax errors\" section");;
x=inputx;


//PROBLEM1 - if x has a value of 4, print "ddd", else print "eee"
result = "";
if (x == 4)
result = result + "ddd";
else
result = result + "eee";
System.out.println("1.\t" + result);


//PROBLEM2 - if x has a value of 3 or 4, print "ddd", else print "eee"
result = "";
if((x==3) ||(x==4))
result = result + "ddd";
else
result = result + "eee";
System.out.println("2.\t" + result);


//PROBLEM3 - if x has a value of 4, print "ddd", else print "eee"
result = "";
if (x == 4)
result = result + "ddd";
else
result = result + "eee";
System.out.println("3.\t" + result);
 
Joel Christophel
Ranch Hand
Posts: 250
1
Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Luan Vo wrote:

Joel Christophel wrote:

Luan Vo wrote:

Joel Christophel wrote:

Luan Vo wrote:

Joel Christophel wrote:Your problem is a misplaced semicolon.

actually the problem is like this

import java.util.*;

public class MinilabIfElse
{
public static void main(String args[])
{
//declare variables
String result;
int x;
int inputx;

//prompt for, read in a value for x
Scanner kb = new Scanner(System.in);
System.out.print("Please enter a value for x (integer): ");
inputx = kb.nextInt();

//************************************************************************
//The if-else statements in this section have syntax errors (will not compile).
//Fix each of them so that it compiles and the proper result is printed.
//************************************************************************
System.out.println("\n\n\"Common syntax errors\" section");;
x=inputx;


//PROBLEM1 - if x has a value of 4, print "ddd", else print "eee"
result = "";
if (x == 4)
result = result + "ddd";
else
result = result + "eee";
System.out.println("1.\t" + result);


//PROBLEM2 - if x has a value of 3 or 4, print "ddd", else print "eee"
result = "";
if((x==3) ||(x==4))
result = result + "ddd";
else
result = result + "eee";
System.out.println("2.\t" + result);


//PROBLEM3 - if x has a value of 4, print "ddd", else print "eee"
result = "";
if (x == 4)
result = result + "ddd";
else
result = result + "eee";
System.out.println("3.\t" + result);



Add two braces to the end of the file to close main method and class. Other than that, I get no errors.

 
Luan Vo
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Joel Christophel wrote:

Luan Vo wrote:

Joel Christophel wrote:

Luan Vo wrote:

Joel Christophel wrote:

Luan Vo wrote:

Joel Christophel wrote:Your problem is a misplaced semicolon.

actually the problem is like this

import java.util.*;

public class MinilabIfElse
{
public static void main(String args[])
{
//declare variables
String result;
int x;
int inputx;

//prompt for, read in a value for x
Scanner kb = new Scanner(System.in);
System.out.print("Please enter a value for x (integer): ");
inputx = kb.nextInt();

//************************************************************************
//The if-else statements in this section have syntax errors (will not compile).
//Fix each of them so that it compiles and the proper result is printed.
//************************************************************************
System.out.println("\n\n\"Common syntax errors\" section");;
x=inputx;


//PROBLEM1 - if x has a value of 4, print "ddd", else print "eee"
result = "";
if (x == 4)
result = result + "ddd";
else
result = result + "eee";
System.out.println("1.\t" + result);


//PROBLEM2 - if x has a value of 3 or 4, print "ddd", else print "eee"
result = "";
if((x==3) ||(x==4))
result = result + "ddd";
else
result = result + "eee";
System.out.println("2.\t" + result);


//PROBLEM3 - if x has a value of 4, print "ddd", else print "eee"
result = "";
if (x == 4)
result = result + "ddd";
else
result = result + "eee";
System.out.println("3.\t" + result);



Thanks youvery much! It's really good to help this kind of help out side of class. Thanks again Joel

 
Joel Christophel
Ranch Hand
Posts: 250
1
Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You will have no errors if your code looks exactly like this:

 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I see ifs with no braces. And you have already been asked to UseCodeTags when posting code. Future posts without them will be ignored.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Joel Christophel wrote:You will have no errors if your code looks exactly like this:
. . .

You mean the compiler will not notice any errors. I can see several errors, eg
  • 1: use of // for multiple comments: should be /*...*/
  • 2: main method more than one statement long
  • 3: unnecessary local variable inputx: you can assign to x directly.
  • 4: unnecessary assignment to ""; that stage can be omitted altogether.
  • 5: empty statement on line 20
  • 6: class name beginning with small letter. That error appears not to be in the original code.
  • Several other errors including missed out {} have already been mentioned.

    All right, you have been given this as an example to find compiler errors. But the whole thing is poor, non‑object‑oriented code. You can buy books which say Java™ is an object language and then show you hundreds of pages without any objects in. You will never learn object‑oriented programming like that.
    I would have expected an “else without if” compiler error from the original code with the excess semicolon.
     
    Get off me! Here, read this tiny ad:
    a bit of art, as a gift, the permaculture playing cards
    https://gardener-gift.com
    reply
      Bookmark Topic Watch Topic
    • New Topic