Help for this.

Java programming topics
User avatar
Face
Major
Major
Posts: 727
Joined: Thu Feb 18, 2010 5:06 pm
Location: SRI LANKA.KANDY.

Help for this.

Post by Face » Mon Jul 19, 2010 8:43 pm

I am back.I need help from you again.I started studding again.I do self studies & need help for these little things.I am not joking.I am just a beginner friends.

Code: Select all

import java.io.DataInputStream;

class three_numbers_additiion
{
	public static void main (String args [] )
	{
		DataInputStream in = new DataInputStream(System.in);
		int intNumber1=0;
		int intNumber2=0;
		int intNumber3=0;
		int answer = 0;
		System.out.println("display 3numbers & get the total of them");
		try
		{
			System.out.println("Enter an Integer 1:");
			intNumber1=Integer.parseInt(in.readLine());
			System.out.println("Enter an intiger 2:");
			intNumber2=Integer.parseInt(in.readLine());
			System.out.println("Enter an Integer 1:");
			intNumber3=Integer.parseInt(in.readLine());
			int answer = intNumber1+intNumber2+intNumber3;	
		}
		catch(Exception e ) {}
			
		
			System.out.println("intNumber1="intNumber1);
			System.out.println("intNumber2="intNumber2);
			System.out.println("intNumber3="intNumber3);
			System.out.println("total of three numbers="answer);
			
	}
	

}
			
i wrote this according to my knowledge & it has errors.(I hate that word ERRORS yaakkaa :x )

need to correct it..
User avatar
Neo
Site Admin
Site Admin
Posts: 2642
Joined: Wed Jul 15, 2009 2:07 am
Location: Colombo

Re: Help for this.

Post by Neo » Mon Jul 19, 2010 10:59 pm

hello and welcome back :D
Topic was submitted under C++, so I started with moving it here :lol:

Can you paste the errors that you get as well ?
User avatar
Face
Major
Major
Posts: 727
Joined: Thu Feb 18, 2010 5:06 pm
Location: SRI LANKA.KANDY.

Re: Help for this.

Post by Face » Sun Jul 25, 2010 5:30 pm

yeah bro..I am back...But little less time I have for my studies.I started JAVA studies.It is too hard to learn by books & PFDs & with you tube.....:)

I use small editor for this...

Code: Select all

>javac three_numbers_additiion.java
three_numbers_additiion.java:26: ')' expected
             System.out.println("intNumber1="intNumber1);
                                             ^
three_numbers_additiion.java:26: illegal start of expression
             System.out.println("intNumber1="intNumber1);
                                                       ^
three_numbers_additiion.java:27: ')' expected
             System.out.println("intNumber2="intNumber2);
                                             ^
three_numbers_additiion.java:27: illegal start of expression
             System.out.println("intNumber2="intNumber2);
                                                       ^
three_numbers_additiion.java:28: ')' expected
             System.out.println("intNumber3="intNumber3);
                                             ^
three_numbers_additiion.java:28: illegal start of expression
             System.out.println("intNumber3="intNumber3);
                                                       ^
three_numbers_additiion.java:29: ')' expected
             System.out.println("total of three numbers="answer);
                                                         ^
three_numbers_additiion.java:29: illegal start of expression
             System.out.println("total of three numbers="answer);
                                                               ^
8 errors
>Exit code: 1
these are the errors i get in that editor.........can you help me on this
User avatar
Neo
Site Admin
Site Admin
Posts: 2642
Joined: Wed Jul 15, 2009 2:07 am
Location: Colombo

Re: Help for this.

Post by Neo » Mon Jul 26, 2010 3:15 pm

Try this.

Code: Select all

         System.out.println("intNumber1 = " + intNumber1);
         System.out.println("intNumber2 = " + intNumber2);
         System.out.println("intNumber3 = " + intNumber3);
         System.out.println("total of three numbers = " + answer);
User avatar
Face
Major
Major
Posts: 727
Joined: Thu Feb 18, 2010 5:06 pm
Location: SRI LANKA.KANDY.

Re: Help for this.

Post by Face » Tue Aug 03, 2010 8:39 pm

yeah bro.I tried that....it works....

Now I need another help for some questions form pass papers I found.I need to learn about some programs step by step what happen on that lines.

Code: Select all

public class Ex27{
public static void main(String args[]){
int x = 0 , y = 0, z = 0;
if(x == 1)
if(y == 2)
if(z == 3)
System.out.println("%%%");
else
System.out.println("***");
System.out.println("###");
}
}
In this program I got out put ###.

But I thought out put will like this
* * *
###

but only got ###.What happen to the "If"condition of this program.according to the logic it should get true or false out put.but in this program we don't have any out put like *** or %%%.

can you please explain about this.This program doesn't have any compiling errors...

please explain this program step by step?
User avatar
Face
Major
Major
Posts: 727
Joined: Thu Feb 18, 2010 5:06 pm
Location: SRI LANKA.KANDY.

Re: Help for this.

Post by Face » Tue Aug 03, 2010 8:42 pm

I got the Idea of this program.

Code: Select all

public class Ex27{
public static void main(String args[]){
int x = 1 , y = 2, z = 3;
if(x == 1)
if(y == 2)
if(z == 3)
System.out.println("%%%");
else
System.out.println("***");
System.out.println("###");
}
}
This out put is %%% ###.Can you explain first program for me...!
User avatar
Face
Major
Major
Posts: 727
Joined: Thu Feb 18, 2010 5:06 pm
Location: SRI LANKA.KANDY.

Re: Help for this.

Post by Face » Tue Aug 03, 2010 9:00 pm

word "start" is a Label name in this program..

I don't know the meaning of that word called Label name...please tell me about label names.

Code: Select all

public class Ex30{
public static void main(String args[]){
int sum = 0;
start:
for(int r = 0 ; r < 2 ; r++){
for(int c = 0 ; c < 4 ; c++){
if( c == 2) break start;
System.out.print("*");
}
System.out.println();
}
} }
User avatar
Neo
Site Admin
Site Admin
Posts: 2642
Joined: Wed Jul 15, 2009 2:07 am
Location: Colombo

Re: Help for this.

Post by Neo » Tue Aug 03, 2010 10:01 pm

G-sparkZ wrote:yeah bro.I tried that....it works....

Now I need another help for some questions form pass papers I found.I need to learn about some programs step by step what happen on that lines.

Code: Select all

public class Ex27{
public static void main(String args[]){
int x = 0 , y = 0, z = 0;
if(x == 1)
if(y == 2)
if(z == 3)
System.out.println("%%%");
else
System.out.println("***");
System.out.println("###");
}
}
In this program I got out put ###.

But I thought out put will like this
* * *
###

but only got ###.What happen to the "If"condition of this program.according to the logic it should get true or false out put.but in this program we don't have any out put like *** or %%%.

can you please explain about this.This program doesn't have any compiling errors...

please explain this program step by step?
Lets go one step at a time. There are two things to tell you.

1. In programming, it is essential to maintain proper indents (tabs) so it is easier to understand codes.
2. If there is no brackets for "if" or "else" only the next statement will be accepted to execute.

First lets put your code with proper indents.

Code: Select all

public class Ex27{
	public static void main(String args[]){
		int x = 0 , y = 0, z = 0;
		if(x == 1)
			if(y == 2)
				if(z == 3)
					System.out.println("%%%");
				else
					System.out.println("***");

		System.out.println("###");
	}
}
Now you can see that the line with ### is always executing.

To understand "if" or "else" without brackets, lets put brackets.

Code: Select all

public class Ex27{
	public static void main(String args[]){
		int x = 0 , y = 0, z = 0;
		if(x == 1){
			if(y == 2){
				if(z == 3){
					System.out.println("%%%");
				}
				else{
					System.out.println("***");
				}
			}
		}
		
		System.out.println("###");
	}
}
I think you are clear now on this. Lets go to the second issue.
User avatar
Neo
Site Admin
Site Admin
Posts: 2642
Joined: Wed Jul 15, 2009 2:07 am
Location: Colombo

Re: Help for this.

Post by Neo » Tue Aug 03, 2010 10:05 pm

G-sparkZ wrote:I got the Idea of this program.

Code: Select all

public class Ex27{
public static void main(String args[]){
int x = 1 , y = 2, z = 3;
if(x == 1)
if(y == 2)
if(z == 3)
System.out.println("%%%");
else
System.out.println("***");
System.out.println("###");
}
}
This out put is %%% ###.Can you explain first program for me...!
See whether you understood this in the right way. I guess you understood it wrong so thought of putting you the code with proper indents.

Code: Select all

public class Ex27{
	public static void main(String args[]){
		int x = 1 , y = 2, z = 3;
		if(x == 1)
			if(y == 2)
				if(z == 3)
					System.out.println("%%%");
				else
					System.out.println("***");

		System.out.println("###");
	}
}
User avatar
Neo
Site Admin
Site Admin
Posts: 2642
Joined: Wed Jul 15, 2009 2:07 am
Location: Colombo

Re: Help for this.

Post by Neo » Tue Aug 03, 2010 10:14 pm

G-sparkZ wrote:word "start" is a Label name in this program..

I don't know the meaning of that word called Label name...please tell me about label names.

Code: Select all

public class Ex30{
public static void main(String args[]){
int sum = 0;
start:
for(int r = 0 ; r < 2 ; r++){
for(int c = 0 ; c < 4 ; c++){
if( c == 2) break start;
System.out.print("*");
}
System.out.println();
}
} }
Code with proper indents,

Code: Select all

public class Ex30{

	public static void main(String args[]){
		int sum = 0;

start:

		for(int r = 0 ; r < 2 ; r++){
			for(int c = 0 ; c < 4 ; c++){
				if( c == 2){
					break start;
				}
				
				System.out.print("*");
			}
			
			System.out.println();
		}
	}
}
I recommend you to put brackets so you will not be confused as I did to "if".

Labels are special points of your code. When you need to jump to that special point from another point(only within the same scope (method) ), you can do that with commands goto and break label_name.

here what happens is, when c becomes 2, the code will break the execution and jump to the location labels as "start". From there the execution of the 2 loops will be started again.

Label is just a name to specify a jump point of the code.

is that explanation clear to you?
Post Reply

Return to “Java Programming”