The Artima Developer Community
Sponsored Link

Java Answers Forum
help with debug

4 replies on 1 page. Most recent reply: Dec 16, 2002 12:57 PM by Matt Gerrans

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 4 replies on 1 page
uh

Posts: 5
Nickname: chickenman
Registered: Nov, 2002

help with debug Posted: Nov 25, 2002 1:13 PM
Reply to this message Reply
Advertisement
<pre>
/* writting a program that accepts a check value like 99.99
and prints out ***99.99 basically protecting the value so no one can change it to like 299.99 etc.. also if a value like 2 is entered it will print ****2.00. i think i have the basic code below but i need help figuring out how to put together the **** with 99.99

the way i have it below give me this error
---------- Run Program ----------
java.lang.NumberFormatException: For input string: "null3.99"
at java.lang.NumberFormatException.forInputString*/

import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import java.text.NumberFormat;
import java.util.Locale;

class labB extends Object
{ int len,c = 8;
double num;
char charArray[];
String tmp, d,p;

public labB( String b)
{
len= b.length(); //get length of string
charArray = new char[len];// create size accordig to string
b.getChars(0,len,charArray,0);//copy string to charArray
int N= c - len; //subtract max - string lenght;
for(int count =0; count<charArray.length; count++)
tmp+= charArray[count];

for(int star =0; star < N; star++)//fill empty places
d+= "*";
/*help here*/
p= d+ tmp;
num= Double.parseDouble(tmp);

//d.concat(tmp);
}


public String toString()
{ NumberFormat money =
NumberFormat.getCurrencyInstance(Locale.US);
return money.format( num );
/* end help*/
}
}

public class checkVal extends JFrame
{
private JTextField input;
private JTextArea outputArea;
private int r;

public checkVal()
{

super("Check Protection Program");
getContentPane().setLayout(new FlowLayout() );
JLabel prompt= new JLabel("Enter Check Amount");

getContentPane().add(prompt);
input = new JTextField(10);
getContentPane().add(input);
JButton b = new JButton(" Protect ");


b.addActionListener(new ActionListener()
{
public void actionPerformed (ActionEvent event)
{
labB s = new labB(input.getText());
outputArea.append(s.toString()+ "\n");
}
}
);
getContentPane().add(b);

outputArea = new JTextArea(10,20);//width,height panel
outputArea.setEditable(false);
getContentPane().add(new JScrollPane(outputArea));
setSize(275,260);// width,height Win grey
show();
}

public static void main(String args[])
{
checkVal window = new checkVal();

window.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent windowEvent)
{
System.exit(0);
}
}
);
}
}</pre>


Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: help with debug Posted: Nov 25, 2002 5:36 PM
Reply to this message Reply
   String padThis( String text, int lenny )
   {
      if( lenny < 0 )
         throw new IllegalArgumentException( "Length can't be negative!" );
      StringBuffer buffy = new StringBuffer( text );
      buffy.insert(0,"*");  // Should always have at least 1 asterisk.
      while( buffy.length() < lenny )
         buffy.insert(0,"*");
      return buffy.toString();
   }

uh

Posts: 5
Nickname: chickenman
Registered: Nov, 2002

Re: help with debug Posted: Dec 16, 2002 8:11 AM
Reply to this message Reply
the code as it stands now is working however it's printing 6.00 instead of printing *****6.00. i don't think it's calling my class method toString().

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.text.DecimalFormat;
 
 class labB extends Object
{ int len;
  String tmp, d;
   
	
	public labB( String b)
	{   tmp=b;
		len= tmp.length(); //get length of string
		
	}
 
   //bring string and length in
   String padThis( String text , int lenny) 
   {  	   
	   int max = 9; //max space in check
	   
	   if( lenny < 0 )        
	     throw new IllegalArgumentException( "Length can't be negative!" );      
     
	 StringBuffer buffy = new StringBuffer( text );      
      //protect check
   while( buffy.length() < max )         
		{  
	      buffy.insert(0,"*");
		
		}
        return buffy.toString();  
   
   }
    
  public String toString()
 {
   return padThis(tmp,len);
 }
	
	
}
 
public class checkVal extends JFrame
{
	private JTextField input;
    private JTextArea outputArea;
    private double p;
 
	public checkVal()
	{
 
     super("Check Protection Program");
     getContentPane().setLayout(new FlowLayout() );
     JLabel prompt= new JLabel("Enter Check Amount");
 
      getContentPane().add(prompt);
       input = new JTextField(10);
        JButton b = new JButton(" Protect ");
 
     
       b.addActionListener(new ActionListener()
  {
     public void actionPerformed (ActionEvent e)
	  {	 
		 p= Double.parseDouble(input.getText());
                
	  
	  //if user enter's 5  convert to 5.00 or 5.8 to 5.80
      DecimalFormat twoDigits = new DecimalFormat("0.00");
	     String h=  twoDigits.format(p);
		 String y=  h;  
	  
      
	  outputArea.append(y.toString()+ "\n");
    }
 }
);
getContentPane().add(input);
getContentPane().add(b);
 
outputArea = new JTextArea(10,20);//width,height panel
outputArea.setEditable(false);
getContentPane().add(new JScrollPane(outputArea));
setSize(275,260);// width,height Win grey
show();
}
 
public static void main(String args[])
{
   checkVal window = new checkVal();
 
  window.addWindowListener(new WindowAdapter()
{
   public void windowClosing(WindowEvent windowEvent)
{
   System.exit(0);
}
}
);
}
}

uh

Posts: 5
Nickname: chickenman
Registered: Nov, 2002

Re: help with debug Posted: Dec 16, 2002 12:30 PM
Reply to this message Reply
hmm never mind i found out that i never actually call labB

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: help with debug Posted: Dec 16, 2002 12:57 PM
Reply to this message Reply
Hmm... Well you've created this class called "labB" for some reason, instead of just adding a method to your existing class. You've also changed the padThis() method to ignore its second parameter, which is a pretty silly thing to do.

In any case, you never even call the padThis() method, so that's why the text is not getting padded.

By the way, what the heck happened with the formatting? Do you really work with your code in that scattered state, or does it just look this way when posted into the message?

Anyway, here is a somewhat repaired version of your post:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.text.DecimalFormat;
 
public class checkVal extends JFrame
{
   private int fieldWidth = 9;
   private final String entryFormatMessage = "Please enter only valid " +
                                             "numbers, without letters " +
                                             "or other symbols.";
   private JTextField input;
   private JTextArea outputArea;
   private double p;
   public checkVal() 
   {
      super("Check Protection Program");
      getContentPane().setLayout(new FlowLayout());
      JLabel prompt = new JLabel("Enter Check Amount");
      getContentPane().add(prompt);
      input = new JTextField(10);
      JButton b = new JButton(" Protect ");
      b.addActionListener( new ActionListener() 
                           {
                              public void actionPerformed(ActionEvent e)
                              {
                                 try
                                 {
                                    p = Double.parseDouble(input.getText());
                                    
                                    //if user enter's 5  convert to 5.00 or 5.8 to 5.80   
                                    DecimalFormat twoDigits = new DecimalFormat("0.00");
                                    String h = twoDigits.format(p);
                                    String y = padThis(h, fieldWidth);
                                    outputArea.append(y.toString() + "\n");
                                 }
                                 catch( java.lang.NumberFormatException nfe )
                                 {
                                    JOptionPane.showMessageDialog( checkVal.this, 
                                                                   entryFormatMessage );
                                 }
                              }
                           });
      getContentPane().add(input);
      getContentPane().add(b);
      outputArea = new JTextArea(10, 20); //width,height panel
      outputArea.setEditable(false);
      getContentPane().add(new JScrollPane(outputArea));
      setSize(275, 270); // width,height Win grey
      show();
   }
   private String padThis(String text, int lenny)
   {
      if(lenny < 0)
         throw new IllegalArgumentException("Length can't be negative!");
      StringBuffer buffy = new StringBuffer(text);
      buffy.insert(0, "*"); // Should always have at least 1 asterisk.      
      while(buffy.length() < lenny)
         buffy.insert(0, "*");
      return buffy.toString();
   }
   public static void main(String args[])
   {
      checkVal window = new checkVal();
      window.addWindowListener(new WindowAdapter() 
      {
         public void windowClosing(WindowEvent windowEvent)
         {
            System.exit(0);
         }
      });
   }
}

Flat View: This topic has 4 replies on 1 page
Topic: about SNMP Previous Topic   Next Topic Topic: Can't figure out this recursive problem

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use