aspose file tools*
The moose likes Beginning Java and the fly likes Printf and multi-dimensional array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Printf and multi-dimensional array" Watch "Printf and multi-dimensional array" New topic
Author

Printf and multi-dimensional array

Chas Martin
Greenhorn

Joined: Oct 07, 2008
Posts: 18
Hey guys, Im having a problem and I was wondering if someone could help me through it. I am student teaching a 12th grade honors math class and I want to show the kids an example of some things you can do with java and math. I made a few simple examples of easy calculations but I want to show them that you can do some things a little more in depth. Im trying to make a multiplication table using a file and a scanner as well as using multi-dim arrays and I want it to print out nice and clean. Its been a couple years since I took a java class and Im a little rusty.
this is what I have so far:

What Im doing is reading in a simple text file that I made which consists of this:
1-3 4-6
2-4 3-9
I split the tokens at the dash and insert them into a matrix form to make the mult table.
so far I have got it to give me this:
run:
4 5 6
8 10 12
12 15 18
6 8 10 12 14 16 18
9 12 15 18 21 24 27
12 16 20 24 28 32 36
BUILD SUCCESSFUL (total time: 4 seconds)

I want it to give me this:


then another table just like the one above but with rows from 2-4 and columns 3-9.
I know I need to use the printf method I dont know how to use it with the multi-dimensional array.
anything will help
Thanks for the help guys
[ November 21, 2008: Message edited by: Campbell Ritchie ]
Chas Martin
Greenhorn

Joined: Oct 07, 2008
Posts: 18
oops, where I typed in what i want it to give me the 4 should be above the column with 4, 8, 12 in it and so on.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39815
    
  28
There are lots of nextXXX methods in the Scanner class. I think it would be easier if you use one of those methods to get your text straight into a number. You will need to change the delimiter on Scanner to include the -; I think you want "[\\s\\-]".

You seem to be getting on quite well with it, and you seem to have the correct results. You are using single spaces between successive numbers, and want differing numbers of spaces (It didn't show up well, but I added code tags to what you want as your output, which has improved the formatting).

Suggest:
  • Full details about the different formatting options are in the Formatter class.
  • There are details you can actually read and understand in the Java� Tutorials.
  • To print out a decimal integer "n" at the right end of a 4-character block, try ...printf("%4d", n);
  • There are several ways to print new lines; I think you are probably best with ...println(), but leave out the argument. Details here.
  • Try a ...printf("%4d", i); before the inner "for" in the loop where you do the printing.
  • You will have to do something similar for the row at the top of each block.
  • I hope that will be of some help to you.

    CR
    Chas Martin
    Greenhorn

    Joined: Oct 07, 2008
    Posts: 18
    I looked at the stuff you gave me but I just cant get it to work right. could someone explain to me how I would set up the printf method for this case.
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 39815
        
      28
    Chas Martin
    Greenhorn

    Joined: Oct 07, 2008
    Posts: 18
    Thanks CR, I really appreciate the help.
    One last thing; How can I get it to print the numbers that I am multiplying as well. It is only printing out the products but it is not showing you what numbers made those products, Ive tried probably ten different things but I cant get anything close to working. Any ideas???
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 39815
        
      28
    You're welcome. You just need more printf statements.

    Suggest ...printf("%-4d" i + third); just before that block I showed earlier, inside the outer "for".
    Suggest another "for" earlier.You do realise you don't need the arrays at all? You can simply print out i * j.
    Chas Martin
    Greenhorn

    Joined: Oct 07, 2008
    Posts: 18
    Thanks again, I'm am so close. I got it to print out the numbers at the beginning of each row but I can't figure out how to print the numbers at the top of each column. This is my output so far:


    also, How would I go about putting a blank line in between the two tables just so it looks a little nicer?
    [ November 23, 2008: Message edited by: Chas Martin ]
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 39815
        
      28
    That's a lot improved
    To get blank lines, the easiest way is to insert a ...println(); statement at the appropriate place.


    Try printing the " " then a for loopYou will have seen that you can alter the format and spacing quite simply by altering the numbers after the % and using other characters eg -.

    [edit]Add a ...print(" "); line and a missing comma.[/edit]
    [ November 23, 2008: Message edited by: Campbell Ritchie ]
    Jacquie Banares
    Greenhorn

    Joined: Apr 13, 2010
    Posts: 1
    [Deleted as hi-jack]
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 39815
        
      28
    Welcome to the Ranch

    Please do not ask new questions on an existing thread, which is called thread hi-jacking. Please post your question as a new thread; I shall fell obliged to delete your hi-jack.
    Also, don't expect us to do your work for you. Please post what you have at present and tell us what happens when you run it.

    By the way: this is a Java forum; if you have JavaScript questions please post them on our JavaScript forum.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Printf and multi-dimensional array