Java Queries Here..

Desi-Tek.com

In the zone
finally i fixed the problem by creating new instance of user in properties

Code:
 package com.desitek.cricket.action;

public class LoginAction {

    private User user = new User();

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public String login() {
        System.out.println(user.getUsername());
        return "success";
    }
}
 
Last edited:

Plasma_Snake

Indidiot
OK, guys here r 2 very n00bish questions from me so answer 'em like Experts ;) :D
1.Can we create or instantiate Object of a class, in a class other than main? I mean is this possible?
Code:
 class A { some code}
 class B {some code A a = new A}
 class C{
p s v main
{some code}
}
2. can we make a Static Array?
Hope u r able to understand the questions. :neutral:
 

Bandu

Journeyman
1. Yes. Thats what most of the times classes are for - instantiation, unless they are private in some other package or have private constructors. You need to see topics regarding class visibility - public, default, package, etc.

2. Yes, you can. What made you think you cannot.

Example for #1:
*img48.imageshack.us/img48/5640/plasmacr2.th.png*img48.imageshack.us/images/thpix.gif

Example for #2:
*img171.imageshack.us/img171/2609/plasmasnakewh0.th.png*img171.imageshack.us/images/thpix.gif
 
Last edited:

Plasma_Snake

Indidiot
Thanx for the screenshots, cleared the doubt like Harpic cleans the $hit from the flush. :D
In the first example u've created the object of A in constructor of B. Is it the only way or it can be done another way also? :idea:
 
OK, this is really n00bish compared to your standards, but still:
can anyone explain the "while","do while" functions (ie.loop) with an example? e-books arent helping here :|
 

QwertyManiac

Commander in Chief
Thanx for the screenshots, cleared the doubt like Harpic cleans the $hit from the flush. :D
In the first example u've created the object of A in constructor of B. Is it the only way or it can be done another way also? :idea:
An object can be created anywhere, anytime from a class as long as its scope allows it to be. Have you not learnt OO concepts at school yet?
 

Bandu

Journeyman
In the first example u've created the object of A in constructor of B. Is it the only way or it can be done another way also? :idea:

I used the constructor for the example. You can create it anywhere. Constructor is just another method + something more, which isn't the topic at the moment, so I'll refrain from writing about constructors.

OK, this is really n00bish compared to your standards, but still:
can anyone explain the "while","do while" functions (ie.loop) with an example? e-books arent helping here :|


I won't call it n00bish. Can be quite confusing even for seasoned programmers.


*img210.imageshack.us/img210/8887/twcrh5.th.png*img210.imageshack.us/images/thpix.gif

Code:
        [COLOR=SeaGreen][B]while(i < 2)[/B][/COLOR]
        {
            ...
        }

        System.out.println("\nTesting the exit controlled loop\n");
        int j = 0;
        do
        {
            ...
        } [COLOR=Red][B]while (j == 100);[/B][/COLOR]
    }
}
while loop is also called as "entry controlled" loop. With this, the code statements within the loop body are executed only if the boolean condition evaluates to true and this evaluation is carried out before entering the loop. Thats why entry controlled. The boolean condition is evaluated first before entering the loop body. Example is the green one in the above snippet.

do...while is also called as "exit controlled" loop. With this, the code statements are executed first. The control does enter the loop body and executes the statements within. At the end lies the boolean condition. Red one in the above code. If this condition evaluates to true, control loops back to the first statement in the loop body and so on... until the condition evaluates to false.

Its difficult to give an example... *searches_for_do_while_in_code* :)

Found...

I had a situation where I had to parse a line from a csv (comma separated values) file and process each part. Lets say, print each part of the token.

An example string: think,digit,rocks
Another example: digit

Both are valid csv examples. You can type in these lines in a file, save it as a .csv and open it in excel. It will work. Each token that is separated by a comma goes into a XL cell:

*img513.imageshack.us/img513/4739/csvvn0.th.png*img513.imageshack.us/images/thpix.gif

Anyways, back to the example.

The solution would be - no matter if a comma exists or not, we have to process the input - irrespective of the boolean condition (of having a comma in the string).

Note: The italicized words above is the boolean condition, and the underlined words form the loop body.

*img155.imageshack.us/img155/7893/twccsvparsefx9.th.png*img155.imageshack.us/images/thpix.gif
P.S. Refer Chandru's comment below. Do not use this as an example for CSV parsing. It's written to be an example for exit controlled loop. CSV parsing can be achieved more simply using Chandru's suggestion below.
 
Last edited:
I won't call it n00bish. Can be quite confusing even for seasoned programmer......................is the boolean condition, and the underlined words form the loop body.

*img155.imageshack.us/images/thpix.gif
Thanks man. Really great explanation (although it took some time to understand use of words such as "boolean" etc. as they are not used commonly [I know what boolean means :p] )
In the end, problem solved :)
You'll here more simple questions from me in future :D
 

Plasma_Snake

Indidiot
I was just comparing C# with JAVA and few of these questions just popped up in my mind so asked. :)
@QwertyM
If u r questioning my programming knowledge then I can tell u this that I've done Java 4 times in past 1 year but still ain't fully clear to me as possibilities are limitless in these Purely OOP languages. This thread is for asking questions, isn't it?
 

Bandu

Journeyman
[OFF TOPIC]

That CSV line can parsed easily using String.split(",") method.

Yes, I know that. Good that you mentioned so that anyone coming to this thread does not take it as an example for csv parsing (infact I've edited my original post to mention this). But in my case, that was a legacy code. Moreover, I just modded it and simplified it to serve as an example here. The actual code has lots of other things that a String.split would not have served anyways.
 

chandru.in

In the zone
Yes, I know that. Good that you mentioned so that anyone coming to this thread does not take it as an example for csv parsing (infact I've edited my original post to mention this). But in my case, that was a legacy code. Moreover, I just modded it and simplified it to serve as an example here. The actual code has lots of other things that a String.split would not have served anyways.
I never meant to undermine your reply. :)
 

Bandu

Journeyman
^^ Nopes. I did not say so either. What made you think that way? In fact, I appreciate you bringing it up.
 

QwertyManiac

Commander in Chief
@QwertyM
If u r questioning my programming knowledge then I can tell u this that I've done Java 4 times in past 1 year but still ain't fully clear to me as possibilities are limitless in these Purely OOP languages. This thread is for asking questions, isn't it?
No, its just that the doubt was too trivial. Why would I wanna doubt your programming knowledge? :\
 

Plasma_Snake

Indidiot
Anyhoo, I would like to tell all of u guys that, as I'm also doing a 3 year GNIIT from my local NIIT center, besides my B.Tech in I.T, I had the option to choose an elective subject there and options were PL/SQL, ASP.NET(SOAP,WES 3.0) and J2ME. Guess what I chose??? ;) :D Classes for it will begin early next year so till then I've to get good in my Core JAVA. That I'll do by myself and with ur help and support but for J2ME, they'll give books too but can u guys recommend some good ones which are of Engineering student's level, not n00bish like NIIT's?
 

Plasma_Snake

Indidiot
^^ Hey I'm just in for the certifications, I don't even read their books, only for passing their exams, for Core I've followed, Java 2 Complete Reference by Herbert Schildt, so now askin' one for J2ME
 
Top Bottom