Results 1 to 7 of 7
- 11-04-2012, 07:18 PM #1
Member
- Join Date
- Nov 2012
- Posts
- 5
- Rep Power
- 0
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
And this would make displayEnter 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
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.First Last Salary
Benjamin Jensen 1,800
Elizabeth Adams 45,700
Elise Killy 2,913,500
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)); }
-
Re: Help would be Greatly Appreciated.. please
You're already using a Formatter object:
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).Java Code:System.out.println(formatter.format("%5s %5s %5s", "FirstName","LastName", "Salary"));
- 11-04-2012, 09:09 PM #3
Member
- Join Date
- Nov 2012
- Posts
- 5
- Rep Power
- 0
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
-
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);
- 11-04-2012, 09:36 PM #5
Member
- Join Date
- Nov 2012
- Posts
- 5
- Rep Power
- 0
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?
-
Re: Help would be Greatly Appreciated.. please
- 11-04-2012, 10:54 PM #7
Member
- Join Date
- Nov 2012
- Posts
- 5
- Rep Power
- 0
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
-
Using a loop to prevent a program from closing? Help greatly appreciated :)
By RarkMowe in forum New To JavaReplies: 5Last Post: 10-24-2012, 07:17 PM -
What's wrong with my code? (Danish java-programmer would be greatly appriciated!)
By spande in forum New To JavaReplies: 32Last Post: 02-29-2012, 09:21 AM -
Any Help Much Appreciated!
By danielinthebed in forum Advanced JavaReplies: 4Last Post: 01-04-2012, 09:27 AM -
Java program Fillarray method error :[ help is greatly needed pelase
By stan989 in forum New To JavaReplies: 5Last Post: 12-08-2011, 07:54 PM -
Java Calculator Help Would Be Greatly Appreciated
By TommyR in forum New To JavaReplies: 1Last Post: 03-14-2011, 09:30 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks