i have a question?help me

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ahmee
    New Member
    • Mar 2010
    • 39

    i have a question?help me

    here is my code below. i have question regarding my code.why we have taken "output" as string.is this because we have used concate sign in this expression
    output+=value+" "; in the code if not plz tell me why??


    Code:
    int value;
    String output="";
    for(int i=1; i <=10 ; i++)
    {
      value= 1+(int)(6* Math.random());
      output+=value+"";
      if(i%2==0){
        output+="\n";
      }
    }
    Last edited by Frinavale; Aug 12 '10, 06:34 PM. Reason: Please post code in [code]...[/code] tags. Added code tags & fixed indentation (good indentation shows good coding practices)
  • TheServant
    Recognized Expert Top Contributor
    • Feb 2008
    • 1168

    #2
    Yes. It looks like you are to print your output variable, and so it is easier to be a string.

    I don't know what:
    Code:
    output+=value+"";
    means? I don't think you can add an int to a string like that? I may be wrong, but have you considered using an Integer instead, which allows you to utilize the .toString method. Second point is the +""? Why would you need to add an empty string?

    Just confirming that you know that with output as a string it just (as you say) concatinates the value to the string and doesn't actually add (mathematically ) any values. If this is your goal, it makes sense that you declare it a String.

    Comment

    • Frinavale
      Recognized Expert Expert
      • Oct 2006
      • 9749

      #3
      What are you trying to do in your code?
      Please explain it by writing the code out in words/sentences to explain what you are doing on each line.

      I don't even think that code will compile.

      I do not understand what your question is exactly either.

      -Frinny

      Comment

      • sixdonuts
        New Member
        • Jul 2010
        • 6

        #4
        My guess is doing value+"" is someone's way of forcing value to be a string instead of an integer. What you can do is

        Code:
        output += Integer.toString(value)

        Comment

        • Dheeraj Joshi
          Recognized Expert Top Contributor
          • Jul 2009
          • 1129

          #5
          I don't even think that code will compile.
          It compiles and runs properly.

          My guess is doing value+"" is someone's way of forcing value to be a string instead of an integer. What you can do is
          No it is not. If you are using + operator you need not to convert the non string value to string. The "" in OP's code has no significance.

          Ok.

          Code:
          output+=value+"";
          is same as

          Code:
          output=output+value+""
          This is legal in Java. '+' operator can work between String and String,Integer, character,doubl e,float.

          '+' operator does convert the non string value into its corresponding string representation.

          As far as OP's code is concerned i think OP want single string to store all the values with a new line character which is generated within that loop.

          Regards
          Dheeraj Joshi

          Comment

          Working...