2

Expected Output

Enter exam score:             Enter grade:
Math:                         Math:
Science:                      Science:
History:                      History:                 

                                                                    

How to get this output?

My output

Enter exam score:                
Math:                                     
Science:                                        
History:

Enter Grade:
Math:
Science:                                                        
History:

How do I enter input in the next column?

Subrato Pattanaik
  • 5,331
  • 6
  • 21
  • 52

2 Answers2

3

Sounds like you're looking for System.out.printf() where you can use %- to add padding to your strings to make them even when printing. You can adjust the exact amount of spacing by changing the 30, but this should work for you:

String mathStr = "Math:";
String scienceStr = "Science:";
String historyStr = "History:";
System.out.printf("%-30s%s\n", "Enter exam score:", "Enter grade:");
System.out.printf("%-30s%s\n", mathStr, mathStr);
System.out.printf("%-30s%s\n", scienceStr, scienceStr);
System.out.printf("%-30s%s\n", historyStr, historyStr);

Edit: Added variables to show OP it works with those as well.

Kel Varnsen
  • 314
  • 2
  • 8
  • How to column " Enter the Grade" – mary ann fernandez Oct 10 '20 at 04:19
  • O instead of using System.out.println() use System.out.print() and add `\t` tab characters as needed – Kel Varnsen Oct 10 '20 at 04:21
  • @maryannfernandez Sorry didn't understand what you wanted to do at first. Try it now. – Kel Varnsen Oct 10 '20 at 04:34
  • Wow thanks a lot. Can i use printf if the statement have variables – mary ann fernandez Oct 10 '20 at 05:36
  • @maryannfernandez Yes essentially how the printf works is the first string parameter is the formatting. %s corresponds to String, %d to Integer, %f to Float, etc. So if you had `printf("%s%d", strVar, intVar)` that would print out your strVar and intVar with no spaces in between. Notice that the parameter order matches the % order. The 30 in `"%-30s"` sets the padding (and the - I believe just specifies starting from the left) so a 10 character String would have 20 spaces after it. – Kel Varnsen Oct 10 '20 at 05:50
  • @maryannfernandez The line `System.out.printf("%-30s%s\n", "Math:", "Math:");` could easily be replaced with: `String mathStr = "Math:"; ` `System.out.printf("%-30s%s\n", mathStr, mathStr);` – Kel Varnsen Oct 10 '20 at 05:52
  • System.out.print( "Math: "); int HW= Integer.parseInt(num.readLine()); System.out.print("Science: "); int AT= Integer.parseInt(num.readLine()); System.out.print("History: "); int BE= Integer.parseInt(num.readLine()); double res=(HW+AT+BE+)/3; double perc = res*.25; – mary ann fernandez Oct 10 '20 at 06:00
  • If you want to print integers with it you have to use %d instead of %s. And the number of % signs in the first parameter to `printf()` should match the number of parameters after. – Kel Varnsen Oct 10 '20 at 06:08
0

OK, so I think you want to provide a way for the user to enter the data in a columnar display on a console.

That is difficult. The problem is that when the terminal driver is in normal ("cooked") mode, the console only accepts the user's input and sends it to your program when the user types ENTER. And that causes the console display driver to go to the next line.

To solve this, you have to do one of the following:

But I would recommend that you don't do this. If you want a "fancy" input screen for your users to enter input, implement it in Swing, or JavaFX, or using a web (HTML + CSS + Javascript) interface. If you need to accept input from the console, make it look like this:

Enter exam score (0-100) followed by a grade (A-F):
Math: 99 A
Science: 88 B  
History: 21 F
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216