Results 1 to 7 of 7
  1. #1
    aejames23 is offline Member
    Join Date
    Nov 2012
    Posts
    5
    Rep Power
    0

    Default Help would be Greatly Appreciated.. please

    Alright im making this big project. Basically I have a text file
    FirstName LastName Salary
    Benjamin Jensen 1,800
    Elizabeth Adams 45,700
    Elise Killy 2,913,500
    Austin Sasiak 156,000

    Now i have to make 2 scanner classes, one to read the file and one to take input from user.
    The user has to enter in

    Enter field widths for first, last, and salary (2-15): 12 16 10
    Enter field widths for first, last, and salary (2-15): 12 10 10
    Enter alignment in field (L or R): r
    Enter exponent of 10 for rounding (0-3): 2
    And this would make display
    First Last Salary
    Benjamin Jensen 1,800
    Elizabeth Adams 45,700
    Elise Killy 2,913,500
    I have to change the alignments and the widths by whatever the user enters. I have do not know how to do this. help would be much appreciated. so far i have.

    Java Code:
    public static void main(String[] args) throws IOException
    	{
    		File file = new File ("employee.txt");
    		Formatter formatter = new Formatter();
    	    System.out.println(formatter.format("%5s %5s %5s", "FirstName","LastName", "Salary"));
    
    		BufferedReader in = new BufferedReader(new FileReader("employee.txt"));
    		String line;
    		while((line = in.readLine()) != null)
    		{
    		    System.out.println(line);
    		}
    		System.out.println(TableFormat.padString("Hello", 5, 'L'));
    		TableFormat.round();
    		System.out.println(TableFormat.addCommas(723723));
    	    
    	}

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Help would be Greatly Appreciated.. please

    You're already using a Formatter object:

    Java Code:
    System.out.println(formatter.format("%5s %5s %5s", "FirstName","LastName", "Salary"));
    This functions similar to System.out.printf and String.format. The only thing you have to change is the format String, "%5s %5s %5s". Instead of using the hard coded "5"'s create a format String that uses the numbers given by the user, and then use that with your Formatter (or printf of String.format).

  3. #3
    aejames23 is offline Member
    Join Date
    Nov 2012
    Posts
    5
    Rep Power
    0

    Default Re: Help would be Greatly Appreciated.. please

    What do you mean by create a format String?, i do not know how to make that and incorporate it with the formatter

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Help would be Greatly Appreciated.. please

    You're already using a format String, "%5s %5s %5s", but you're hard coding it with 5's. Instead create one with variables:

    Java Code:
    // specifier1, specifier2, specifier3 are variables
    String formatString = "%" + specifier1 + "s %" + (.... you should get the idea);

  5. #5
    aejames23 is offline Member
    Join Date
    Nov 2012
    Posts
    5
    Rep Power
    0

    Default Re: Help would be Greatly Appreciated.. please

    oh okay! i also have two other methods. One that puts commas in the salary. and one that rounds the salary. How would I incorporate this into there?

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Help would be Greatly Appreciated.. please

    Quote Originally Posted by aejames23 View Post
    oh okay! i also have two other methods. One that puts commas in the salary. and one that rounds the salary. How would I incorporate this into there?
    By doing a little experimenting. Play around with your code and try things out, and I'll bet you'll solve it. If not, come back with your attempts and let us see what you've tried. Just don't rush back here. give it a decent go first.

  7. #7
    aejames23 is offline Member
    Join Date
    Nov 2012
    Posts
    5
    Rep Power
    0

    Default Re: Help would be Greatly Appreciated.. please

    Java Code:
    File file = new File ("employee.txt");
    		
    		Scanner input = new Scanner(System.in);
    		System.out.println("Enter field widths for first, last, and salary (2-15) ");
    		int one;int two; int three;
    		one = input.nextInt();
    		two = input.nextInt();
    		three = input.nextInt();
    		if (one < 2 || two < 2 || three < 2 || one > 15 || two > 15 || three > 15 )
    		{
    			do
    			{
    				System.out.println("Enter field widths for first, last, and salary (2-15) ");
    				one = input.nextInt();
    				two = input.nextInt();
    				three = input.nextInt();
    			}
    		while ((one < 2 || two < 2 || three < 2 || one > 15 || two > 15 || three > 15));
    		}
    	
    		Formatter formatter = new Formatter();
    	    System.out.println(formatter.format("%"+ one + "s %" + two + "s %" +three + "s", "","", ""));
    
    		BufferedReader in = new BufferedReader(new FileReader("employee.txt"));
    		String line;
    		while((line = in.readLine()) != null)
    		{
    		    System.out.println(line);
    		}

    Alright i got this, but i do not know how to incorprate in the methods of adding commas, and rounding. How do i make sure that only the salary part gets rounded and puts commas to?
    How do i use only the salary part of the text file?

Similar Threads

  1. Replies: 5
    Last Post: 10-24-2012, 08:17 PM
  2. Replies: 32
    Last Post: 02-29-2012, 10:21 AM
  3. Any Help Much Appreciated!
    By danielinthebed in forum Advanced Java
    Replies: 4
    Last Post: 01-04-2012, 10:27 AM
  4. Replies: 5
    Last Post: 12-08-2011, 08:54 PM
  5. Java Calculator Help Would Be Greatly Appreciated
    By TommyR in forum New To Java
    Replies: 1
    Last Post: 03-14-2011, 10:30 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •