stuck at formatting string/integer


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: stuck at formatting string/integer

  1. #1
    Join Date
    Jan 2009
    Posts
    3

    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);
    
    	}
    }

  2. #2
    Join Date
    Jan 2009
    Posts
    11
    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));
        }
    }

  3. #3
    Join Date
    Jan 2009
    Posts
    3
    thank you for the input!! ill give that a shot!

  4. #4
    Join Date
    Oct 2005
    Location
    Maady
    Posts
    1,819
    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)"
    Programmer&Cracker CS
    MyBlog:Blog.Amahdy.com
    MyWebsite:www.Amahdy.com

Similar Threads

  1. Copying TextPane formatting to Word
    By compscigeek in forum Java
    Replies: 0
    Last Post: 05-29-2008, 08:43 PM
  2. Replies: 1
    Last Post: 06-04-2007, 04:19 PM
  3. Reporting Services 2005 Formatting issue
    By asifhameed1 in forum Database
    Replies: 2
    Last Post: 09-25-2006, 11:29 PM
  4. Restoring a database gets stuck in (loading) mode
    By Marc Pflieger in forum Database
    Replies: 0
    Last Post: 09-13-2002, 01:39 PM
  5. Formatting Text with XML
    By Liz in forum XML
    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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center