Programming help please!

Discussion in 'Scripting & Programming' started by zimbo, Oct 17, 2006.

  1. zimbo
    Honorary Member

    zimbo Petabyte Poster

    5,215
    99
    181
    Im working on a ex which is in java.... if someone could help me. here is my code for the program:

    Code:
    import javax.swing.JOptionPane;
    
    public class Ex4 {
    	
    	public static void main(String args[])
    	
    	{
    		int number,i,j,total;
    		String snumber;
    		
    		snumber = JOptionPane.showInputDialog(    
    			"Please enter the number the table should go up to");
    			
    		number = Integer.parseInt(snumber);
    		
    		System.out.println("number    times 5    cubed");
    		
    		for(i=0; i<=number; i++)
    		{
    			System.out.print(i + "          ");
    			System.out.print(i*5 + "          ");
    			System.out.println(i*i*i);
    			
    			
    		}
    	}
    }
    Code:
    number    times 5    cubed
    
    0          0          0
    1          5          1
    2          10          8
    3          15          27
    4          20          64
    
    what im trying to do is straighten the cubed - its driven me crazy so any help because im done with the ex just that little bug...

    thanks!:biggrin
     
    Certifications: B.Sc, MCDST & MCSA
    WIP: M.Sc - Computer Forensics
  2. Stoney

    Stoney Megabyte Poster

    731
    23
    69
    I'm no Java programmer but i think you may have to look into the way java handles properties for printing.

    It's inserting the same amount of space between the numbers in the number, times 5, and cubed columns. Where a column has a number with 2 digits it causes the spacing to move across 1, and thus push the other column out of alignment.

    I imagine you may have to write another line of code along the lines of:

    where i >9, set print spacing property to be 1 less than what ever the default is. (As I said i'm no programmer!!) 8)
     
    Certifications: 25 + 50 metre front crawl
    WIP: MCSA - Exam 70-270
  3. hbroomhall

    hbroomhall Petabyte Poster Gold Member

    6,624
    117
    224
    Stoney is correct about what is happening! What you need is some sort of formatting, where you specifiy the width of the field to put the number into.

    How you do this depends on the version of Java you are using I believe.

    In C it is easy - you use printf - but doing it in Java can be a problem if your reference copy of Java is old.

    Harry.
     
    Certifications: ECDL A+ Network+ i-Net+
    WIP: Server+
  4. zimbo
    Honorary Member

    zimbo Petabyte Poster

    5,215
    99
    181
    ok i know about field width but forgot about it.. been 2 years since i did any programming! right so how can i:

    a) find out which version of java im using?
    b)insert field widths?

    thanks to both of you btw:biggrin
     
    Certifications: B.Sc, MCDST & MCSA
    WIP: M.Sc - Computer Forensics
  5. zimbo
    Honorary Member

    zimbo Petabyte Poster

    5,215
    99
    181
    im using pure java... like in dietel and dietel java how to program! :biggrin
     
    Certifications: B.Sc, MCDST & MCSA
    WIP: M.Sc - Computer Forensics
  6. hbroomhall

    hbroomhall Petabyte Poster Gold Member

    6,624
    117
    224
    Try:
    Code:
    C:\Documents and Settings\Harry>java -version
    java version "1.5.0_01"
    Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_01-b08)
    Java HotSpot(TM) Client VM (build 1.5.0_01-b08, mixed mode)
    
    C:\Documents and Settings\Harry>
    Harry.
     
    Certifications: ECDL A+ Network+ i-Net+
    WIP: Server+
  7. zimbo
    Honorary Member

    zimbo Petabyte Poster

    5,215
    99
    181
    im running 1.5.0_07
     
    Certifications: B.Sc, MCDST & MCSA
    WIP: M.Sc - Computer Forensics
  8. Bluerinse
    Honorary Member

    Bluerinse Exabyte Poster

    8,878
    181
    256
    Zimbo, Harry is showing you how to find the version of Java, so at the command prompt type java -version

    So that he can see if the command *System.out.printf* or maybe System out.format is compatible.

    But I know diddly squat but I can read :biggrin

    EDIT: I see you changed your answer now :wink:
     
    Certifications: C&G Electronics - MCSA (W2K) MCSE (W2K)
  9. zimbo
    Honorary Member

    zimbo Petabyte Poster

    5,215
    99
    181
    lol as usual i jumped the gun! any clue on which line that will go pete?8)

    edit: the reason i cant find this myself (i tried googling) is because formatting is beyond this module and the tutor wants the output like i have it now... but i want to do it correct so a little programming help on cf please! thanks!
     
    Certifications: B.Sc, MCDST & MCSA
    WIP: M.Sc - Computer Forensics
  10. Bluerinse
    Honorary Member

    Bluerinse Exabyte Poster

    8,878
    181
    256
    Googling blind without a clue but wondered if this link is any good? :blink

    http://java.sun.com/developer/JDCTechTips/2004/tt1005.html#2
     
    Certifications: C&G Electronics - MCSA (W2K) MCSE (W2K)
  11. Boycie
    Honorary Member

    Boycie Senior Beer Tester

    6,281
    85
    174
    slightly off topic, but i notice the -version command doesn't work for all programs installed. I know you type ver for the current OS install.

    Si
     
    Certifications: MCSA 2003, MCDST, A+, N+, CTT+, MCT
  12. zimbo
    Honorary Member

    zimbo Petabyte Poster

    5,215
    99
    181
    i think thats the command for the java virtual machine... if you want to run a java program you type java then the location of the program so that why i think you got your error...
     
    Certifications: B.Sc, MCDST & MCSA
    WIP: M.Sc - Computer Forensics
  13. zimbo
    Honorary Member

    zimbo Petabyte Poster

    5,215
    99
    181
    thanks pete but the commands i have understood there seem to be for formatting at run time on the command line unless im missing something? which lines in my code need formatting?:blink
     
    Certifications: B.Sc, MCDST & MCSA
    WIP: M.Sc - Computer Forensics
  14. hbroomhall

    hbroomhall Petabyte Poster Gold Member

    6,624
    117
    224
    You use the printf method instead of the print one.

    I would suggest using it for the header line as well - it makes things hang together better.

    Harry.
     
    Certifications: ECDL A+ Network+ i-Net+
    WIP: Server+

Share This Page

Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.