Java Assignment

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • yolazyguy
    New Member
    • Jul 2007
    • 1

    #1

    Java Assignment

    Hello I am new but I have been in a intro programming class with Java as the main entree. I have this program where I need to end input characters at 65 characters and break the line and then add a new line.

    This is my code:

    import static java.lang.Syste m.*;
    import java.util.Scann er;

    class formatter3 {

    public static void main (String[] args) {
    Scanner stdin = new Scanner (System.in);
    int maxlen = 65;
    int outlen = 0;
    int spaces = 0;
    while (stdin.hasNext ()) {
    String word = stdin.next();
    System.out.prin tf(word); ++(spaces);
    if ((outlen + spaces + word.length) > maxlen) printf ("%n");
    }
    }

    }


    and i get these error messages when i try to complie:

    bash-2.03$ javac formatter3.java
    formatter3.java :14: cannot find symbol
    symbol : variable length
    location: class java.lang.Strin g
    if ((outlen + spaces + word.length) > maxlen) printf ("%n");
    ^
    formatter3.java :14: operator + cannot be applied to int,java.lang.S tring.length
    if ((outlen + spaces + word.length) > maxlen) printf ("%n");
    ^
    formatter3.java :14: operator > cannot be applied to <nulltype>,in t
    if ((outlen + spaces + word.length) > maxlen) printf ("%n");
    ^
    formatter3.java :14: cannot find symbol
    symbol : method printf(java.lan g.String)
    location: class formatter3
    if ((outlen + spaces + word.length) > maxlen) printf ("%n");
    ^
    4 errors


    Waaahhhh!!!! any suggestions would be helpful. please I've been searching aorund campus for a community that does java and could help out and let me be a part, but santa cruz is HIPPY! x_x
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    Hi, and welcome to TSDN!

    You have come to the right place my friend!
    Let me just move this question over to the Java forums, where our Java experts are more likely to find it.

    Moderator

    Comment

    • adarcorerlreth
      New Member
      • May 2007
      • 1

      #3
      1. you need to import java.lang.Strin g class

      code:
      import java.lang.Strin g;

      2. there is nop printf() in Java, you need to use:

      System.out.prin tln("");

      think that should fix it

      Originally posted by yolazyguy
      Hello I am new but I have been in a intro programming class with Java as the main entree. I have this program where I need to end input characters at 65 characters and break the line and then add a new line.

      This is my code:

      import static java.lang.Syste m.*;
      import java.util.Scann er;

      class formatter3 {

      public static void main (String[] args) {
      Scanner stdin = new Scanner (System.in);
      int maxlen = 65;
      int outlen = 0;
      int spaces = 0;
      while (stdin.hasNext ()) {
      String word = stdin.next();
      System.out.prin tf(word); ++(spaces);
      if ((outlen + spaces + word.length) > maxlen) printf ("%n");
      }
      }

      }


      and i get these error messages when i try to complie:

      bash-2.03$ javac formatter3.java
      formatter3.java :14: cannot find symbol
      symbol : variable length
      location: class java.lang.Strin g
      if ((outlen + spaces + word.length) > maxlen) printf ("%n");
      ^
      formatter3.java :14: operator + cannot be applied to int,java.lang.S tring.length
      if ((outlen + spaces + word.length) > maxlen) printf ("%n");
      ^
      formatter3.java :14: operator > cannot be applied to <nulltype>,in t
      if ((outlen + spaces + word.length) > maxlen) printf ("%n");
      ^
      formatter3.java :14: cannot find symbol
      symbol : method printf(java.lan g.String)
      location: class formatter3
      if ((outlen + spaces + word.length) > maxlen) printf ("%n");
      ^
      4 errors


      Waaahhhh!!!! any suggestions would be helpful. please I've been searching aorund campus for a community that does java and could help out and let me be a part, but santa cruz is HIPPY! x_x

      Comment

      • prometheuzz
        Recognized Expert New Member
        • Apr 2007
        • 197

        #4
        Originally posted by adarcorerlreth
        1. you need to import java.lang.Strin g class

        code:
        import java.lang.Strin g;
        No everything from the java.lang package is automatically imported.


        Originally posted by adarcorerlreth
        2. there is nop printf() in Java, you need to use:

        System.out.prin tln("");

        think that should fix it
        Yes there is:
        http://java.sun.com/j2se/1.5.0/docs/api/java/io/PrintStream.htm l#printf(java.u til.Locale,%20j ava.lang.String ,%20java.lang.O bject...)
        and since the OP imported System.out with a static import, this should work:

        [CODE=java]out.printf("%n" ); // the 'out' must be in front of it![/CODE]

        But the real problem is the fact that the OP is trying to access the length of the String as if it was a public attribute, like this:

        [CODE=java]int i = aString.length;[/CODE]

        but the method length() should be called:

        [CODE=java]int i = aString.length( );[/CODE]

        Comment

        • nickyeng
          Contributor
          • Nov 2006
          • 252

          #5
          word.length

          if want to get word's length...you should call

          word.length()

          Comment

          • prometheuzz
            Recognized Expert New Member
            • Apr 2007
            • 197

            #6
            Originally posted by nickyeng
            word.length

            if want to get word's length...you should call

            word.length()
            How is that different from my answer?

            Comment

            • JosAH
              Recognized Expert MVP
              • Mar 2007
              • 11453

              #7
              Originally posted by prometheuzz
              How is that different from my answer?
              Your answer was first and his answer wasn't?

              kind regards

              Jos ;-)

              Comment

              • prometheuzz
                Recognized Expert New Member
                • Apr 2007
                • 197

                #8
                Originally posted by JosAH
                Your answer was first and his answer wasn't?

                kind regards

                Jos ;-)
                No, his answer was last and mine wasn't!
                ; )

                Comment

                • JosAH
                  Recognized Expert MVP
                  • Mar 2007
                  • 11453

                  #9
                  Originally posted by prometheuzz
                  No, his answer was last and mine wasn't!
                  ; )
                  We'd call it an 'ex equo' then and split the prize!

                  kind regards,

                  Jos ;-)

                  Comment

                  Working...