Hello, World! My name is Alex and i am a beginner in Java. I tried to create 2 simple objects from the same class and give them the values inputed from console( as string). Is this correct or it should be done in another particular manner? Also, the constructor asks for a String parameter ( "string x" has nothing to do as far as i know with the programm variables i use but the ide do not let me to compile other way...perhaps it has something to do with creating objects - constructor - I/O implementation , yet it should be a parameter formula like String a, String b ... well , i'm a noob) i am very open for suggestion. Thanks in advance ! alex

    import java.util.Scanner;

    public class ClassObject2 {

    public ClassObject2(String x) {

    }

    public static void main(String[] args) {

        Scanner scan = new Scanner(System.in);
        System.out.println("Please insert a value for a: ");
        String a = scan.nextLine();

        System.out.println("Please insert a value for b: ");
        String b = scan.nextLine();

        ClassObject2 obj1 = new ClassObject2(a);
        ClassObject2 obj2 = new ClassObject2(b);

        System.out.println("object a has val.: " + a);
        System.out.println("object b has val.: " + b);
    }
}

Can you post the full text of the error messages you get.

Can you explain what problems you are having?

import java.util.Scanner;

public class ClassObject2 {

    public ClassObject2(String a){

    }
    public static void main(String[] args) {

        Scanner scan = new Scanner(System.in);
        System.out.println("Please insert a value for a: ");
        String a = scan.nextLine();

        System.out.println("Please insert a value for b: ");
        String b = scan.nextLine();

        ClassObject2 obj1;
        obj1 = new ClassObject2(a);
        ClassObject2 obj2 = new ClassObject2(b);

        System.out.println("object a has val.: " + a);
        System.out.println("object b has val.: " + b);
    }
}

First of all, thanks for the response. there is no run error. Pb. is i am a noob . hope now is ok. any suggestion to make the programm better? i'm still confused about the fact that constructor only accepts String a and not (string a, string b) as it shoud be logical. i am not sure if obj2 accept b as value from console just because it follows the same routine as obj1 beeing from the same class or i have to add another constructor for obj2 as well?! thanks

confused about the fact that constructor only accepts String a

Look at the definition of the constructor and you can see that it takes just one String as an argument: public ClassObject2(String aStr){

The constructor would look like this if it took 2 Strings:
public ClassObject2(String a1, String a2){

it looks better now from my point of view :)
amyway, it's just practice and this is just an exercise. i am glad people share knowlledge and help each other these days.
thanks for responses
alex

import java.util.Scanner;

public class ClassObject2 {

    public ClassObject2(String a, String b){

    }
    public static void main(String[] args) {

        Scanner scan = new Scanner(System.in);
        System.out.println("Please insert a value for a: ");
        String a = scan.nextLine();

        System.out.println("Please insert a value for b: ");
        String b = scan.nextLine();

        ClassObject2 obj1;
        obj1 = new ClassObject2(a,b);
        ClassObject2 obj2 = new ClassObject2(b,a);

        System.out.println("object a has val.: " + a + "" + b);
        System.out.println("object b has val.: " + b + "" + a);
    }
}

Why does it look better? As coded, both objects have exactly the same content. That doesn't always make sense. Many times each object will have a different content, one will have the String a and the other the String b. What if you want a variable number of objects, each with its own contents?

I think the change you made is going in the wrong direction. If you have 2 objects each should have its own string, and doesn't need to know about the other object's string. What will happen when you have 3 objects, a thousand objects? Will you have a thousand string constructor?
Normally you would have a String variable defined in the class. The constructor would take one string as a parameter and use that to set the variable. That way every time you create a new object it automatically has its own new string.

A suggestion: If you use a real-life example rather than just abstract class names it will be a lot easier to see what makes sense and what doesn't. Eg: just change the name of your class to Computer and the name of the String to manufacturer. Now which makes more sense:

Computer a = new Computer("Apple");
Computer b = new Computer ("Dell");

or

Computer a = new Computer("Apple", "Dell");
Computer b = new Computer("Apple", "Dell");

thanks for your advices and yes! i think too, the best, if not asked other way, is to have a separate constructor for each String type of course. but even that the sum ( as structure) is "the same" , the order (a + b) && (b + a) is not the same and this relation is what make me smile. If i say, for ex. : i need a 7 - as sum made from all the numbers from 0 to 7, where members of sum operation are 2 (a & b). First object, let's say it follows the rulle: a + b .... The second obj. will be upsidedown (b + a) but with the same result ... where a & b can encapsulate other objects. I repeat, this is a solo imaginary example from my noobish experience but that's the funny part with Java...you can play "hide & seek" very well. the sum is the same...the members are the same and yet in some context you can view as different a and b if the position in the object affect other objects as well but i need to practice and learn more to be able to disscus this subjects. cheers!

OK!
If you don't mind a bit more advice...
please be very careful about how you use Java terminiology, otherize you will confuse people and therefore possibly get completely misleading answers. For example:

have a separate constructor for each String type

I honestly don't know what meaning you intended with this. There is only one String type and that is String. String is a final class with no subclasses, so there can never be any other types of String. In any class definition you can only have one constructor with any given list of parameter types, so you can only have one constructor that takes a single String.

I'm noy trying to be picky here, just trying to ensure that you get the right answers to your questions.

dude, is simple. i'm not talking chinese here. it's about getting some values from the console and use that values for two simple objects (for ex.). and if all the noobs should not ask questions, only if they know how to "use carefully java terminology", well...excuse my english. ANd, yes, in my last posts, i rised a logical question: if we have 2 values and we make the sum (one for each object), even that the sum is the same, there are members (a and b) that creates the value(sum) for obj1 and obj2 and because the place they occupy for doing that in one manner or another, it can affect other objects, if are to be created ... i'm talking about possibilities. and yes, i am a noob, i do not know the entire alphabet of java terminology but i am stil learning. i am pretty sure there is at least one that can understand what i am talking about or at least not to feel offended by my monkey question. thank you!:)

Woahthere! You misundertand me! I meant no insult, nor did I mean to discourage you. I was not offended, nor did I want to offend you. In no way was I suggesting that you shouldn't ask questions. I was just trying to alert you to a problem that's pretty common with people who are at just starting with Java. I was just trying to help you get the best out of DaniWeb.

thank you so much. perhaps i should study more and then i should learn to listen and just then i should learn to talk. you are a nice person and i have to practice more before writing some threads and of course, i have to be more clear and specific about the questions i am asking .thanks again for your time! :)

p.s.

this is the idea: if a + b = c and b + a = c, then the only difference is the position in the concatenation and not the result. My noob question is: if the sum of a and b is representing the same value for two objects, can we create further objects that will be affected by this position of a and b from the first two objects or not? )

If I understand the question properly...
If your objects just store the value of c, then the information about whether it was a+b or b+a will be lost, and can't affect anything afterwards. On the other hand, if your objects store the two input values (eg arg1 = a, arg2 = b for the first case, arg1 = b, arg2 = a for the second) then they can make that info available to other classes.

Thank you, James! That's what i was asking about. I'm still learning the basics of Java SE7 from scratch( i never programmed before as far as i remember) and i will try to put into code this idea in the proper manner. Thanks a lot! Now, there is hope:D

Thank you , NormR1 too . You are all nice people.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.