Results 1 to 5 of 5

Thread: Help please :(

  1. #1
    Eimo is offline Member
    Join Date
    Oct 2010
    Posts
    26
    Rep Power
    0

    Default Help please :(

    Hi,
    Please I need help in reading a line from a user (exp, + 1 2) and store each operation and numbers in a declared integer....

    This is what I wrote but the problem that I cannot compare String to String[]... so what should I do.....

    Here is the code....

    InputStreamReader reader = new InputStreamReader(System.in);

    BufferedReader buf_reader = new BufferedReader(reader);
    Try {
    String strInput = buf_reader.readLine();

    int op = Integer.parseInt(strInput(0));
    int n1 = Integer.parseInt(strInput(1));
    int n2 = Integer.parseInt(strInput(2));
    }

    Please anyone help me...

  2. #2
    Alessio is offline Member
    Join Date
    Oct 2010
    Posts
    14
    Rep Power
    0

    Default

    Ok, first of all: Java Arrays are read using square brackets, eg. arrayName[index] - not the parenthesis you are using.

    Second: why do you try to parse the operator into an Integer, if I read your assignment right it should be a String.

    The main problem is that you need to split the read line into the three parts (i.e. the op, n1 and n2). This is done by using the .split() method of the Java String class. This method returns an array with all pieces found in the string.



    Java Code:
    		InputStreamReader reader = new InputStreamReader(System.in);
    		BufferedReader buf_reader = new BufferedReader(reader);
    		try {
    			String strInput = buf_reader.readLine();
    			String[] splitInput = strInput.split(" "); //use space to split the string
    			String op = splitInput[0];
    			int n1 = Integer.parseInt(splitInput[1]);
    			int n2 = Integer.parseInt(splitInput[2]);			
    			System.out.printf("op: %s, n1: %d, n2: %d\n",op, n1, n2);
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    Regards,
    Alessio

  3. #3
    Eimo is offline Member
    Join Date
    Oct 2010
    Posts
    26
    Rep Power
    0

    Default

    Thank you so much Alessio that helped a lot. I have another question if you don't mind. I'm doing a Client/Server program using CORBA. I compiled the IDL file, then I wrote both the server and client java files and I compiled it. But it is not connecting, I don't know what is the problem. I used these lines to initiate ORB:
    In the server:
    orbd -ORBInitialPort 1050&
    java CaServer -ORBInitialPort 1050 -ORBInitialHost localhost&

    In the Client:
    java HelloClient -ORBInitialPort 1050 -ORBInitialHost localhost

    Is that right or I'm doing something wrong?

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    It is not connecting sense, did you get any error message? If you could show us your code over here, it may helpful to comment.

  5. #5
    Eimo is offline Member
    Join Date
    Oct 2010
    Posts
    26
    Rep Power
    0

    Default The Client side

    Hi,
    I solved the error issue and I started the server but I couldn't start the Client, I think I'm having a problem in passing the object, how can I do that?? please advice...

    ORB orb = ORB.init(args, null);
    org.omg.CORBA.Object objRef = orb.resolve_initial_references("NameService");
    NamingContextExt ncRef = NamingContextExtHelper.narrow(objRef);

    String name = "Calc";
    CalcImpl = CalcHelper.narrow(ncRef.resolve_str(name));

    InputStreamReader reader = new InputStreamReader(System.in);
    BufferedReader buf_reader = new BufferedReader(reader);

    CalcClient objCalcClient = new CalcClient();


    System.out.println("Client:" +strInput);

    String strInput = buf_reader.readLine().toString().trim();
    String[] splitInput=strInput.split(" ");

    //Trim the whitespaces before parsing
    int op = Integer.parseInt(splitInput[0]);
    int n1 = Integer.parseInt(splitInput[1]);
    int n2 = Integer.parseInt(splitInput[2]);

    //Calling functions to implement different calculations
    CalcImpl.calculate(op, n1, n2);
    //int result=n1 + op + n2;
    System.out.println("The result is: "+objCalcClient);

    I think the problem that I'm not passing the object back to the Server, can please anyone advice how can I do that.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •