Page 1 of 2 12 LastLast
Results 1 to 20 of 26

Thread: Little help...

  1. #1
    hotice1027 is offline Member
    Join Date
    Sep 2008
    Posts
    18
    Rep Power
    0

    Default Little help...

    Hello,
    I am new to java and I am taking it in college.
    I need to write a program that accepts a String like "00110101000110011"
    then it needs to read it and print a "*" if it is a 1 or " " if it is a 0.

    I was told that I have to create a tester file as well.
    Now, I do not need code. I will try to do it on my own.
    All I need is help with the overall construct of the program.
    I just do not know what methods should be in the class file etc.

    Thanks in advance!

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    It will still be a whole lot better if you give it a go first and if it doesn't work, post your attempt here. Also remember that the name should tell what the method does. Good luck!

  3. #3
    hotice1027 is offline Member
    Join Date
    Sep 2008
    Posts
    18
    Rep Power
    0

    Default

    OK,

    So far I got:
    tester file:

    public class letterPrintTester
    {
    public static void main(String[] args)
    {
    String[] letterPrint = new String[] {"01110001000010000100001000010001110"};

    for (int i = 0; i < letterPrint.length; i++)

    {
    if (letterPrint[i].equals ("0"))
    System.out.print(" ");
    else
    System.out.print("*");
    }


    }
    }

    Yes, that string is hardcoded.

    Class file:

    public class letterPrint
    {

    private String numString;

    //Constructor
    public letterPrint(String numString)
    {
    this.numString = numString;
    }

    public letterPrint()
    {

    }

    //Methods

    public String getChar(String numString)
    {
    return numString;
    }

    }

    Thanks!

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,570
    Rep Power
    23

    Default

    And what is your question?
    Do you get errors? Is the output not what you want? Or ???

  5. #5
    hotice1027 is offline Member
    Join Date
    Sep 2008
    Posts
    18
    Rep Power
    0

    Default

    Yes, the output is not what I want.
    All I get is a one "*".

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,570
    Rep Power
    23

    Default

    Add some more println()s to your code that show where the code is executing and what the values of variables are.

  7. #7
    hotice1027 is offline Member
    Join Date
    Sep 2008
    Posts
    18
    Rep Power
    0

    Default

    I am sorry, but I am not sure where to insert those lines...

    I think it does not work because the it sees the String array as a whole and
    cannot go through that String. All it does is goes to the first character or something...
    Last edited by hotice1027; 09-20-2008 at 10:36 PM.

  8. #8
    serjant's Avatar
    serjant is offline Senior Member
    Join Date
    Jun 2008
    Location
    Ukraine,Zaporozhye
    Posts
    487
    Rep Power
    6

    Default

    Java Code:
    public class letterPrintTester
    {
    public static void main(String[] args)
    {
    String letterPrint = new String("01110001000010000100001000010001110");
    char[] c=letterPrint.toCharArray();
    for (int i = 0; i < c.length; i++)
    
    {
    if (c[i]=='0')
    System.out.print(" ");
    else
    System.out.print("*");
    }
    
    
    }
    }

  9. #9
    hotice1027 is offline Member
    Join Date
    Sep 2008
    Posts
    18
    Rep Power
    0

    Default

    serjant - Спасибо!

  10. #10
    serjant's Avatar
    serjant is offline Senior Member
    Join Date
    Jun 2008
    Location
    Ukraine,Zaporozhye
    Posts
    487
    Rep Power
    6

    Default

    Пожалуйста!Рад был помочь

  11. #11
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,570
    Rep Power
    23

    Default

    String[] letterPrint = new String[] {"01110001000010000100001000010001110"};
    This defines an array with one element.
    If you want more than one element you can use:
    String[] anArr = new String[] {"first", "second", "third"};

  12. #12
    hotice1027 is offline Member
    Join Date
    Sep 2008
    Posts
    18
    Rep Power
    0

    Default

    Yes, that's an option.
    However, we were told to keep that string in an array as a whole.
    So, the String is Ex. "00110101110011010...."

  13. #13
    serjant's Avatar
    serjant is offline Senior Member
    Join Date
    Jun 2008
    Location
    Ukraine,Zaporozhye
    Posts
    487
    Rep Power
    6

    Default

    So if it is in the array,then it should be this way:
    Java Code:
    String[] letterPrint = new String[] {"01110001000010000100001000010001110"};
    char[] c=letterPrint[0].toCharArray();
    ......

  14. #14
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,570
    Rep Power
    23

    Default

    keep that string in an array as a whole
    That seems a weird way to do it. Why use an array of one element?
    What could be the reason to do it that way?

  15. #15
    hotice1027 is offline Member
    Join Date
    Sep 2008
    Posts
    18
    Rep Power
    0

    Default

    Yea, I know.
    This is a school project...
    All right - here is a full description of a project - I probably did not describe it very well in the beginning...

    Write a class, called letterPrint, for printing a letter as a 7x5 grid of either spaces or asterisks (*). The letter is made up of a list of thirty-five 1's and 0's, which will be stored in an array representing the letter. This will be the only instance variable of the class.

    For example, the input for letter I would look like this:

    01110001000010000100001000010001110

    reading this input, your program will output a 1 as a * and a 0 as a space. So, the output will look like:

    ***
    *
    *
    *
    *
    *
    ***

    As you have noticed, after every 5 elements is printed, you will print a new line to get the output the way it looks like, an I.

    Write the following methods:
    - A constructor with one parameter, which is an array of thirty-five 0's or 1's. This is the only instance variable for your class. Make sure to enforce the constraint of only 0's and 1's.
    - Accessor, toString, and equals methods
    - A method for printing out the letter (like the example)

    As always, you will need to write a tester to test all these methods.

  16. #16
    jason wang is offline Member
    Join Date
    Sep 2008
    Posts
    16
    Rep Power
    0

    Default

    Quote Originally Posted by hotice1027 View Post
    OK,

    So far I got:
    tester file:

    public class letterPrintTester
    {
    public static void main(String[] args)
    {
    String[] letterPrint = new String[] {"01110001000010000100001000010001110"};

    for (int i = 0; i < letterPrint.length; i++)

    {
    if (letterPrint[i].equals ("0"))
    System.out.print(" ");
    else
    System.out.print("*");
    }


    }
    }

    Yes, that string is hardcoded.

    Class file:

    public class letterPrint
    {

    private String numString;

    //Constructor
    public letterPrint(String numString)
    {
    this.numString = numString;
    }

    public letterPrint()
    {

    }

    //Methods

    public String getChar(String numString)
    {
    return numString;
    }

    }

    Thanks!

    String[] letterPrint = new String[] {"01110001000010000100001000010001110"};
    I think this is not very good in your code, because you should see letterPrint as a char, not a string type. It will have a performance problem.

  17. #17
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    6

    Default

    Don't want to be an ass, but:
    Java Code:
    public class letterPrintTester
    Follow the conventions, it should be LetterPrintTester. ;)
    I die a little on the inside...
    Every time I get shot.

  18. #18
    hungleon88 is offline Member
    Join Date
    Aug 2008
    Posts
    41
    Rep Power
    0

    Default

    for (int i = 0; i < letterPrint.length; i++)

    {
    if (letterPrint[i].equals ("0"))
    System.out.print(" ");
    else
    System.out.print("*");
    }


    it must be

    if (letterPrint.charAt(i).equals ('0'))

    i think that.

  19. #19
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    6

    Default

    doesn't matter hungleon, does the same.
    I die a little on the inside...
    Every time I get shot.

  20. #20
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,570
    Rep Power
    23

    Default

    an array of thirty-five 0's or 1's
    That seems to say:
    = new String[] {"1", "0", ....for 35 total ...};

Page 1 of 2 12 LastLast

Posting Permissions

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