Results 1 to 7 of 7
  1. #1
    Mash93 is offline Member
    Join Date
    May 2013
    Posts
    5
    Rep Power
    0

    Default New, could use some help :)

    Java Code:
    package labone;
    //Import needed classes
    import java.util.Scanner;
    
    /**
     *
     * @author Mash
     */
    public class LabOne {
    
        public static void main(String[] args) {
    
            Scanner input = new Scanner(System.in);
    
            int groups;
    
            
              System.out.print ("How many groups: "); 
              groups = input.nextInt();
             
                
            for (int i = 0; i < 128; i++) {
                if (i == 7) {
                    System.out.print(" ");
                    System.out.print(" " + (Integer.toHexString(i)));
                    System.out.print(" " + i);
                    
                } else if (i == 8) {
                    System.out.print(" ");
                    System.out.print(" " + (Integer.toHexString(i)));
                    System.out.print(" " + i);
                    
                } else if (i == 9) {
                    System.out.print(" ");
                    System.out.print(" " + (Integer.toHexString(i)));
                    System.out.print(" " + i);
                    
                } else if (i == 10) {
                    System.out.print(" ");
                    System.out.print(" " + (Integer.toHexString(i)));
                    System.out.print(" " + i);
                    
                } else if (i == 13) {
                    System.out.print(" ");
                    System.out.print(" " + (Integer.toHexString(i)));
                    System.out.print(" " + i);
                    
                } else {
                    System.out.print((char) i);
                    System.out.print(" " + (Integer.toHexString(i)));
                    System.out.print(" " + i);
                    
    
                }
            }
        }
    }
    So basically I have to print the ASCII table avoiding the character for 7,8,9,10,13 due to formatting issues. The part I am stuck on is making another loop somewhere to allow me to display a certain number of ASCII outputs (sets of 3) depending on the number of "groups" wanted by the user.

    Sorry it's hard to even explain myself haha as I am totally new to Java.

    Also looking at this mess now I realize I could of probably done an "or" in the if statement instead of separate if statements for each number (7,8,9,10,13). Correct?

    Thanks in advance for any explanations possible!

    This might help, as it is what the end result is suppose to look like;

    Last edited by Mash93; 05-14-2013 at 06:50 PM.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,762
    Blog Entries
    7
    Rep Power
    21

    Default Re: New, could use some help :)

    Basically you don't want to print the ASCII representation if the code value is 7, 8, 9, 10 or 13; for those situations simply print a space character ' '. A simple if with a few or-clauses can make the selection. But there is more: all the decimal values should take up three positions and all hexadecimal values should take up two positions (to make the columns neatly aligned); have a look at the System.out.printf( ... ) method for that.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Mash93 is offline Member
    Join Date
    May 2013
    Posts
    5
    Rep Power
    0

    Default Re: New, could use some help :)

    Quote Originally Posted by JosAH View Post
    Basically you don't want to print the ASCII representation if the code value is 7, 8, 9, 10 or 13; for those situations simply print a space character ' '. A simple if with a few or-clauses can make the selection. But there is more: all the decimal values should take up three positions and all hexadecimal values should take up two positions (to make the columns neatly aligned); have a look at the System.out.printf( ... ) method for that.

    kind regards,

    Jos
    Thanks for the help! After reading a bit and trying a few things, I have another noob question :( lol. Can you convert in-line? of the system.out.printf()? Like this for example;

    Java Code:
            
    int i = 3;
    System.out.printf("%d",(Integer.toHexString(i)));
    or is some sort of other approach needed? I realize that doing this changes the integer to a string, correct?

  4. #4
    Mash93 is offline Member
    Join Date
    May 2013
    Posts
    5
    Rep Power
    0

    Default Re: New, could use some help :)

    Nevermind I figured it out! "%s" is for hex!. Thanks a bunch for pointing me to that doc!

    My next question where I am stuck is how do I format it so that it starts a new line everytime it reads "x" number of groups.

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,762
    Blog Entries
    7
    Rep Power
    21

    Default Re: New, could use some help :)

    Quote Originally Posted by Mash93 View Post
    Nevermind I figured it out! "%s" is for hex!. Thanks a bunch for pointing me to that doc!

    My next question where I am stuck is how do I format it so that it starts a new line everytime it reads "x" number of groups.
    Nope, 's' is for Strings; use 'x' for hexadecimal (it's all in the documentation); you are printing six columns (each column has three sub-columns); so if i can be written as 6j-1 after having printed colunn i, you have to print a new line.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    Mash93 is offline Member
    Join Date
    May 2013
    Posts
    5
    Rep Power
    0

    Default Re: New, could use some help :)

    How do I write it so that every 6 times my loop runs it prints a new line?

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,762
    Blog Entries
    7
    Rep Power
    21

    Default Re: New, could use some help :)

    Let 'i' be the column number; after printing an entry, if i%6 == 5, you should print a new line (% is the modulo operator).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Posting Permissions

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