Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #32906 > unrolled thread

Ascii art issue JAVA

Started byStavros Argyrou <stavris.47@gmail.com>
First post2015-10-20 08:31 -0700
Last post2015-10-20 11:59 -0400
Articles 2 — 2 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  Ascii art issue JAVA Stavros Argyrou <stavris.47@gmail.com> - 2015-10-20 08:31 -0700
    Re: Ascii art issue JAVA Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-10-20 11:59 -0400

#32906 — Ascii art issue JAVA

FromStavros Argyrou <stavris.47@gmail.com>
Date2015-10-20 08:31 -0700
SubjectAscii art issue JAVA
Message-ID<89196bf7-c590-472d-8791-6d501c2a7e8e@googlegroups.com>
So I wrote this, I want to create the shape of the Greek letter Φ, I changed this code a bit it worked with the char * but now I want to make it so it prints the row number instead of stars. I changed C as int to parse the row into my function. I get no errors but the result is something like [L]java.lang.String@58342342319423.. random numbers :S Thanks

package A11d;

import java.awt.Font;
import java.util.Arrays;
import javax.swing.JOptionPane;

public class DrawPhiwin2 {
    /**
     * Produces a string of spaces that has the character c
     * at the three specified positions, followed by a newline.
     */
    private static String row(int c, int left, int mid, int right) {
        String[] line = new String[right + 2];
        //c +=48;
        String a = Integer.toString(c);
        for(int j=0;j<line.length;j++){line[j]=" ";}
        line[left] = line[mid] = line[right] = a;
        line[right + 1] = "\n";
        return new String(line.toString());
    }

    public static String toString(int size) {
        StringBuilder out = new StringBuilder();
        int halfWidth = (size + 1)/2;
   
        // Top star
        out.append(row(1, halfWidth+3, halfWidth+3, halfWidth+3));

        // Body
        for (int rows = 2; rows <= size / 2; rows++) {
            out.append(row(rows, halfWidth - rows, halfWidth+2, halfWidth + rows+4));
        }
        for (int rows = halfWidth; rows >= 2; rows--) {
            out.append(row(size-rows+1, halfWidth - rows, halfWidth+2, halfWidth + rows+4));
        }

        // Bottom star
        out.append(row(size, halfWidth+3, halfWidth+3, halfWidth+3));
        
        return out.toString();
    }

    public static void main(String[] args) {    	
    	javax.swing.UIManager.put("OptionPane.font", new Font("Arial", Font.BOLD, 30));
        /**
         * Keep the program on a loop - if the user inputs a 
         * value lower than 4 or higher than 30 exit
         */
  
        //System.out.println("\nEnter a new Length for the Shape: ");
    int  size = Integer.parseInt(JOptionPane.showInputDialog("\nEnter a new Length for the Shape: "));
        
	while((size>=4)&&(size<=30)) {        	
			JOptionPane.showMessageDialog(null, toString(size));
        	size = Integer.parseInt(JOptionPane.showInputDialog("\nEnter a new Length for the Shape: "));
    }
	JOptionPane.showMessageDialog(null,"Value is less than 4 or higher than 30 - Program will now close...\n");
    }
}

[toc] | [next] | [standalone]


#32907

FromEric Sosman <esosman@comcast-dot-net.invalid>
Date2015-10-20 11:59 -0400
Message-ID<n05o99$8nf$1@dont-email.me>
In reply to#32906
On 10/20/2015 11:31 AM, Stavros Argyrou wrote:
> So I wrote this, I want to create the shape of the Greek letter Φ, I changed this code a bit it worked with the char * but now I want to make it so it prints the row number instead of stars. I changed C as int to parse the row into my function. I get no errors but the result is something like [L]java.lang.String@58342342319423.. random numbers :S Thanks

     When you get a result you do not understand and you want to
ask for help about it, *never* say "the result is something like."
Say what the result actually *is* instead.  Otherwise, the people
who would like to help must guess at the nature of your trouble,
and may guess incorrectly.

     My guess:

>          String[] line = new String[right + 2];
>          [...]
>          return new String(line.toString());

     The final line produces a String representation of the array
object itself, not a concatenation of all the array elements.
You should make a StringBuilder, append the individual Strings to
it (in the correct order), and return its toString() instead.

     But I'm just guessing.

-- 
esosman@comcast-dot-net.invalid
"Don't be afraid of work. Make work afraid of you." -- TLM

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web