A friendly place for programming greenhorns!
Big Moose Saloon
Search
|
Java FAQ
|
Recent Topics
Register / Login
Win a copy of
Arduino in Action
this week in the
General Computing
forum!
A special promo:
Enter your blog post or vote on a blogger to be featured in an upcoming Journal
JavaRanch
»
Java Forums
»
Java
»
Beginning Java
Author
Formating issues
Clayton Blankinship
Greenhorn
Joined: Feb 14, 2013
Posts: 14
posted
Feb 14, 2013 15:25:08
0
So this program runs with this data file, I am just wondering if there is anyone that can help me format the output so that it looks clean?
The first three columns are good, but the last does not line up properly. Also any suggestions or pointers on the coding would be much appreciated.
Here is the data if you want to give it a try:
1000 1 1200
1001 2 800
1002 3 300
1003 6 1100
1004 2 -100
1005 3 783
1006 1 1550
1007 5 950
1008 1 750
1009 2 1050
1010 2 600
1011 4 700
1012 3 1200
1013 3 0
0 0 0
Main.java
public class Main { public static void main (String[] args) { readfile r = new readfile(); r.openFile(); r.readFile(); r.closeFile(); } }
readflie.java
import java.io.*; import java.util.*; import java.text.NumberFormat; public class readfile { int clientNum=0; int type=0; int acres=0; double amountDue=0; private Scanner x; public void openFile() { try { x = new Scanner(new File("spraying.txt")); } catch(Exception e) { System.out.println("could not find file"); } } public void readFile() { while(x.hasNext()) { clientNum = x.nextInt(); type = x.nextInt(); acres = x.nextInt(); amountDue = calculateBill(type,acres); NumberFormat fmt = NumberFormat.getCurrencyInstance(); if(clientNum==0) { return; } else { if (acres <0) { System.out.println( clientNum+" "+type+" "+acres+" bad input data"); } else { if (amountDue == 0) { System.out.println( clientNum+" "+type+" "+acres+" bad input data"); } else { System.out.println( clientNum+" "+type+" "+acres+" " +fmt.format(amountDue) ); } if (clientNum == 0) { System.out.println( clientNum+" "+type+" "+acres+" " +fmt.format(amountDue) ); } } } } } public static double calculateBill(int type,int acres) { double amountDue=0; if (type == 1) { amountDue = acres*1; if (acres >1000) { amountDue = amountDue * 0.95; } } else if (type == 2) amountDue = acres *3; else if (type == 3) amountDue = acres*4; else if (type == 4) amountDue = acres*6; else if (type > 4 || type < 1){ amountDue = 0; } if (amountDue >1500){ amountDue = 1500+((amountDue - 1500)*0.9); } return amountDue; } public void closeFile() { x.close(); } }
Winston Gutkowski
Bartender
Joined: Mar 17, 2011
Posts: 4905
7
I like...
posted
Feb 14, 2013 15:44:46
0
Clayton Blankinship wrote:
So this program runs with this data file...
Clayton,
You don't get better answers by simply opening a new thread on
the same subject
under a different title.
If you don't like the answers you got there, then explain why.
Locking this thread.
Winston
Isn't it funny how there's always time and money enough to do it WRONG?
I agree. Here's the link:
http://zeroturnaround.com/jrebel
- it saves me about five hours per week
subject: Formating issues
Similar Threads
Unreachable statement
Reading Array values from a file
How to call data from a file in a while loop?
A Beginner's Program - Comments?
All times are in JavaRanch time: GMT-6 in summer, GMT-7 in winter