Jump to content

Help with Java code

Go to solution Solved by madknight3,

Thank you but do i'm still not getting this :/. Do you know why it should be 8 characters long?

I'm reading the article but its way above my level. I've never heard of a string argument.

 

It will append white space before the String if it is less than 8 characters. Here's an example how different names would look.

   James   Laura    Sara  Alexis Matthew

I assume that was the desired effect and why "%8s" was used.

Hi I'm creating a program in BlueJ (java) and I have some code my teacher gave me and never explained. I has managed to fit the code into my program but in order for me to proceed with the next step I will need to understand the code. Its only losing a mark if I don't do this but overall worth 15 marks of my grade so help is really appreciated. I unfortunately can't show you the rest of the program because that would be cheating but an explanation would really help. 

 

The code below is code which chooses a random name from an array of five names. It then displays a message to the winner. It runs the loop once.

 

My other question is why is "%8s" being used? 

 

 

 

 

  for(int i=0; i<1; i++)
          {
             // Generate random number 0-5
              reelSpin=(int) (5*Math.random());
              
             
              
              // Display corresponding names
             System.out.printf("%8s", Names[reelSpin]);
          }
 
Thanks, help appreciated.

Computer Spec: CPU: i5 4690K @ 4.85GHz Delided    Motherboard: ASUS Maximus VII Hero    RAM: Corsair Vengence 2*4GB    GPU: Gigabyte G1 Gaming GTX 970    PSU: Corsair RM650    Boot Drive: Crucial MX100 256GB SSD    Storage: 1TB WD Blue HDD   Case: NZXT H440 Red and Black 

Cooling: EK PWM D5 Pump & EK 140ML RES Combo unit , EK Supremacy Evo CPU Block , 360mm Alphacool Radiator , 3 Noctua NF-F12 Fans on the radiator , Some fancy fittings

Link to comment
Share on other sites

Link to post
Share on other sites

It's a string argument.

% signals the argument.

8 means it should be 8 chars long.

s means it's a string.

http://javarevisited.blogspot.ro/2012/08/how-to-format-string-in-java-printf.html

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to comment
Share on other sites

Link to post
Share on other sites

It's a string argument.

% signals the argument.

8 means it should be 8 chars long.

s means it's a string.

http://javarevisited.blogspot.ro/2012/08/how-to-format-string-in-java-printf.html

 

Thank you but do i'm still not getting this :/. Do you know why it should be 8 characters long?

I'm reading the article but its way above my level. I've never heard of a string argument.

Computer Spec: CPU: i5 4690K @ 4.85GHz Delided    Motherboard: ASUS Maximus VII Hero    RAM: Corsair Vengence 2*4GB    GPU: Gigabyte G1 Gaming GTX 970    PSU: Corsair RM650    Boot Drive: Crucial MX100 256GB SSD    Storage: 1TB WD Blue HDD   Case: NZXT H440 Red and Black 

Cooling: EK PWM D5 Pump & EK 140ML RES Combo unit , EK Supremacy Evo CPU Block , 360mm Alphacool Radiator , 3 Noctua NF-F12 Fans on the radiator , Some fancy fittings

Link to comment
Share on other sites

Link to post
Share on other sites

Thank you but do i'm still not getting this :/. Do you know why it should be 8 characters long?

I'm reading the article but its way above my level. I've never heard of a string argument.

 

It will append white space before the String if it is less than 8 characters. Here's an example how different names would look.

   James   Laura    Sara  Alexis Matthew

I assume that was the desired effect and why "%8s" was used.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×