Hello,

I have a Computer Science project for school. I am done writing the full code but I am missing one of the requirements. Some of the text have to be aligned to the right.


Here's an excerpt from my code which I want to change the alignment:

System.out.print (subj1 + "\t"); //  Subject Name
System.out.print (gradesubj1 + "%\n"); //Mark for Subject 1

In my code, I want the second line to be align to the right.


I did my search on google and found out that I can use this:

JTextField.setHorizontalAlignment(JTextField.RIGHT);

So, I try it in my code:

System.out.print (subj1 + "\t"); //  Subject Name
JTextField.setHorizontalAlignment(JTextField.RIGHT);
System.out.print (gradesubj1 + "%\n"); //Mark for Subject 1

But, I get an error that says:

Error: Cannot find symbol

I am currently using Dr.Java as my IDE.

What am I doing wrong here?
Should I import a package? Which one?


If you need my full code, you can find it here:

http://lnk.co/IJWSS

Thanks in advance.

Shifat Taushif
shifat.tk

The JTextField stuff applies to a GUI text field, not to console output. Have a look at using printf instead of print, because that allows you to specify formatting for items when you print them at the console.. printf is a method of the PrintStream class, so you'll find the documentation in the API doc for PrintStream, although you may have to follow the links from there to get the whole story...
(Using the API doc is an essential skill for all Java developers - practice is good for you!)

commented: Thanks! +0

Thank you!

OK - just to help point in the right direction - the format spec can include a "width" spec that defines the (minimum) number of chars for that formatted item, so, for example if it's a numeric value with a width of 5 then it will give you results like these

0
   99
  100
 1234
99999

... which is what I think you're looking for

Yes, this is what I was looking for.

OK - just to help point in the right direction - the format spec can include a "width" spec that defines the (minimum) number of chars for that formatted item, so, for example if it's a numeric value with a width of 5 then it will give you results like these

0
   99
  100
 1234
99999

... which is what I think you're looking for

Ok,

So I used printf and I made the following changes:

System.out.print (subj1 + "\t"); //Subject 1
System.out.printf ("%15d" + gradesubj1 + "%\n"); // Mark for Subject 1

It does not encounter an error when I compile it, but when I run it, it says the following when it reaches the point where it has to display the above code:

java.util.MissingFormatArgumentException: Format specifier '15d'
	at java.util.Formatter.format(Unknown Source)
	at java.io.PrintStream.format(Unknown Source)
	at java.io.PrintStream.printf(Unknown Source)
	at Budget.main(Budget.java:114)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.lang.reflect.Method.invoke(Unknown Source)
	at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:271)

What's wrong now?

(I am a beginner at Java so....)

Two+ parameters (separted by commas)- format and 1+values to format, eg
System.out.printf ("%15d\n%15d", 9, 12345);

Something like this?

System.out.printf ("%15d",gradesubj1);

like that.. EXCEPT change the 'd' is '%15d' (cuz that means number or digit or something) unless 'gradesubj1' is an integer, in which case don't change anything, ur fine.

In my code, 'gradesubj1' is a double.

I looked it up on google and found out that %15d is one of the ways to align text.

---

WHOLE CODE (So far..)

//Import Packages -Start 
   import java.io.*; 
   import java.util.*;
   import java.text.DecimalFormat;
   import java.text.NumberFormat;
   import java.lang.Math.*;
   import java.lang.Object.*;
   import javax.swing.JTextField;
  //Import Packages -End 
 
   public class Budget{ 
      public static void main(String args []){ // Main Program Start 
      
         Scanner sc = new Scanner(System.in);
         String name; //Name
         String subj1; //Subject 1
         String subj2;// Subject 2
         String subj3;//Subject 3
         String subj4;//Subject 4
         int birthyear; //Birth year
         final int currentyear = 2012; //Current year - The value needs to be changed every year to calculate the correct age.
         int age; //User's age
         double workhour; //Number of work hours per week
         double workweek; // Number of weeks worked
         double workmoney;//Amount of money earned from work 
         double workmoneytotal; // Total money earned
         double tutoringrate; //Rate of tutoring per hour
         double tutoringhours; //Number of tutoring hours affordable - NOT DISPLAYED
         double tutoringhours_int; //The int version of the total tutoring hours affordable. This is displayed in the final result.
         double gradesubj1; //Grade Mark for the first subject 
         double gradesubj2;//Grade Mark for the second subject 
         double gradesubj3;//Grade Mark for the third subject 
         double gradesubj4;//Grade Mark for the fourth subject 
         double gradeavg;// Average of all the subject's grade mark
         
         
       
         System.out.print("Enter your name: ");
         name = sc.nextLine (); // Gets user's name
         System.out.print ("Enter your birth year: ");
         birthyear = sc.nextInt (); // Gets user's birth year
         age = currentyear - birthyear; // Calculates user's age
      
      
      
      // Subject 1 -Start          
         System.out.print ("\n Enter your first subject : \t");
         subj1 = sc.next ();
         System.out.print ("\n Enter the mark for " + subj1 + " : \t");
         gradesubj1 = sc.nextDouble (); // Gets the percent park for subject 1
      // Subject 1 -End
      
      
      // Subject 2 -Start         
         System.out.print ("\n Enter your second subject : \t");
         subj2 = sc.next ();
         System.out.print ("\n Enter the mark for " + subj2 + " : \t");
         gradesubj2 = sc.nextDouble (); // Gets the percent park for subject 2
      // Subject 2 -End
         
      
      // Subject 3 -Start
         System.out.print ("\n Enter your third subject : \t");
         subj3 = sc.next ();
         System.out.print ("\n Enter the mark for " + subj3 + " : \t");
         gradesubj3 = sc.nextDouble (); // Gets the percent park for subject 3
      // Subject 3 -End
      
      
      // Subject 4 -Start         
         System.out.print ("\n Enter your fourth subject : \t");
         subj4 = sc.next ();
         System.out.print ("\n Enter the mark for " + subj4 + " : \t"); 
         gradesubj4 = sc.nextDouble (); // Gets the percent park for subject 4
      // Subject 4 -End
         
       
      //Work & Money Info -Start
         System.out.print ("\n\n Enter how many hours do you work per week: ");
         workhour = sc.nextDouble ();
         System.out.print ("\nEnter the number of weeks you work: ");
         workweek = sc.nextDouble ();
         System.out.print ("\nEnter your hourly wage: ");
         workmoney = sc.nextDouble ();
         System.out.print ("\nEnter the rate of tutoring per hour you have to pay: ");
         tutoringrate = sc.nextDouble ();
      //Work & Money Info -End 
      
      //Calculations -Start
         gradeavg = (gradesubj1 + gradesubj2 + gradesubj3 + gradesubj4)/4; // Calculates Average of all the grades
         workmoneytotal = workhour*workweek*workmoney; //Calculates the total money earned
         tutoringhours = workmoneytotal/tutoringrate;
         tutoringhours_int = (int) tutoringhours;
         
      //Calculations -End 
      
      //RESULTS -Start        
         System.out.println ("\n --------------------------------- \n"); // Used for separating the report      
         System.out.println ("VicPark University - Tutoring Program Student Budget Report \n\n"); //Title of the report.
         System.out.println ("Name: " + name + "\t\t Age: " + age);  // Displays user's name and age
         System.out.println ("Subjects: " + " Grade Mark:"); 
         
         System.out.print (subj1 + "\t"); //Subject 1
         System.out.printf ("%15d",gradesubj1); // Mark for Subject 1
         System.out.println (subj2 + "\t" + gradesubj2 + "%"); // Subject and Mark for Subject 2
         System.out.println (subj3 + "\t" + gradesubj3 + "%"); // Subject and Mark for Subject 3
         System.out.println (subj4 + "\t" + gradesubj4 + "%"); // Subject and Mark for Subject 4   
         NumberFormat formatter = new DecimalFormat ("#.##"); // Decimal format to two decimal places
         System.out.println ("\nAverage : " + gradeavg + "%"); // Grades Average         
         System.out.println ("\nTotal money earned : $" + workmoneytotal); 
         System.out.println ("Number of affordable tutoring hours: " + tutoringhours_int + " hours");
      
      //RESULTS -End
     }//Main Program End 
   }

SNIP

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.