-
stuck at formatting string/integer
I'm currently trying to run a program in which computes grades per weighted percentages.. I've gotten everything right all the way up til formatting the string to 2 decimal places. In the red towards the bottom is where im guessing im messing up.. also I know my code is a little choppy... so any help would be greatly appreciated!! thank you, tony
Code:
import javax.swing.JOptionPane;
public class Assign3
{
public static void main(String[] args)
{
String hw = JOptionPane.showInputDialog ("Enter homework weight percentage:");
String midterm = JOptionPane.showInputDialog ("Enter midterm weight
percentage:");
String finalex = JOptionPane.showInputDialog ("Enter final exam weight
percentage:");
int p1 = Integer.parseInt(hw);
int p2 = Integer.parseInt(midterm);
int p3 = Integer.parseInt(finalex);
int p4 = 10;
String hws = JOptionPane.showInputDialog ("Enter homework score: (0-600
points)");
String mts = JOptionPane.showInputDialog ("Enter midterm score (0-100 points):");
String fs = JOptionPane.showInputDialog ("Enter final score (0-100 points):");
String labs = JOptionPane.showInputDialog ("Enter lab participation score (0-100
points):");
int x1 = Integer.parseInt(hws);
int x11 = (x1 / 6);
int x2 = Integer.parseInt(mts);
int x3 = Integer.parseInt(fs);
int x4 = Integer.parseInt(labs);
int output1 = (((p1*x11)+(p2*x2)+(p3*x3)+(p4*x4)) / 100);
String output = Integer.toString(output1);
output = String.format("%.2d", output);
JOptionPane.showMessageDialog(null, output);
System.out.println(output);
}
}
-
Try this. 
Code:
public class TestFormat {
public static void main(String[] args) {
int a = 1;
float b = 2;
double c = 3.4354365;
long d = 43241;
// use precision 2, minimum width 5
System.out.println(String.format("%1$5d, %2$5.2f, %3$5.2f, %4$5d", a, b, c, d));
}
}
-
thank you for the input!! ill give that a shot!
-
Code:
1] int output1 = (((p1*x11)+(p2*x2)+(p3*x3)+(p4*x4)) / 100);
2] String output = Integer.toString(output1);
3] output = String.format("%.2d", output);
4] JOptionPane.showMessageDialog(null, output);
1] to get a decimal support for the division, you should not use int, indeed float for example
2] no need at all to convert the variable, the String.format can deal with float, double, int .. etc
3] the correct format string for float here should then be "%.2f", here the second parameter should be a float variable, the whole method could later be converted to string or whatever ur needs.
4] yes here the second paremter could be something like "Float.toString(output1)"
Similar Threads
-
By compscigeek in forum Java
Replies: 0
Last Post: 05-29-2008, 08:43 PM
-
By JeTmAn in forum ASP.NET
Replies: 1
Last Post: 06-04-2007, 04:19 PM
-
By asifhameed1 in forum Database
Replies: 2
Last Post: 09-25-2006, 11:29 PM
-
By Marc Pflieger in forum Database
Replies: 0
Last Post: 09-13-2002, 01:39 PM
-
Replies: 4
Last Post: 07-18-2000, 04:21 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
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks