Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

Typically you would select the 0 when the user clicks in the field rather than erasing it. That way they can replace it by merely typing a value,but if they leave the field you don't have to reset it. You can find some examples of working with text selection here: http://www.exampledepot.com/egs/javax.swing.text/Selection.html

That site is very useful for finding short code examples for many things and is worth keeping bookmarked :)

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

Not everything on the internet is true!!!

If that is the case then I'm not sure I should believe you!

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

Where do all these lies about TV and the Internet come from?

Well, uh, hmm.... the internet...
:P

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

An example of converting text field inputs to ints

JTextField txtField = new JTextField();
String textValue = txtField.getText();
int value = 0;   // starting with a default of 0
try {
    value = Integer.parseInt(textValue);
} catch (NumberFormatException nfe){
    // you can either leave it zero or tell the user it's not a good value
    // or whatever you need to do if the text can't be parsed to long.
    System.out.println("bad value");
}
System.out.println("value="+value);
Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

Read the api doc on Integer.parseInt(). It indicates that it will throw a NumberFormatException if it cannot convert the string to an inv value. Your program needs to handle this. You can't perform math on nulls or empty strings, so it's up to you to decide what to do if that text can't be converted to an int.

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

If you have a decent creditcard it is charged to your bank account every month, and has a low limit that's no more than you can (normally, if you don't do stupid things) pay off each month.

Yes, many people get tons of credit cards to lead a high life on credit until the creditors catch up with them (at which point they have themselves defaulted, washing all that bad credit away instantly).
That shows it's too easy to get multiple cards, not that the cards themselves are "evil".
They serve a purpose, and most people can handle that purpose without getting themselves into trouble.

In the more than a decade I've had a creditcard I've never once been unable to settle the account before interest charges began (so always at the end of the running month).
That takes just a little bit of discipline, and just a little bit of common sense in your spending patterns.
Don't treat that credit limit as extra income (as some people do), treat it as a deduction from your income. Effectively treat your creditcard as pulling money out of your bank account immediately without it effecting the interest you earn on that bank account.

++

Exaclty. Used responsibly, credit cards are not evil at all and have many benefits over cash.

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

What have you been covering in class lately? I'm sure your instructor has given you some idea as to how he wants you to approach it.

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

That was merely an example of coverting a string, which you get from the text field, into an int value. You need to pass in your own string variable.

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

You can create an HTML table, but if you are wanting an interactive JTable you'll need to place that as a separate component.

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

waa.. i dont know understand about public String getText()...
can you give me a sample of it??

this is my action for the PRINT button:

public void actionPerformed (ActionEvent e)
{
  if(e.getActionCommand()=="PRINT")
  {  String temp="";
      temp=rice1.getText();        
      output.append(temp);
      rice1.setText("");
  }

not working though :(

That's because your button says "Print", not "PRINT". Your check on the action command is failing because of that.
Another way to determine which component is making the call is to use e.getSource() and compare that to your components

public void actionPerformed (ActionEvent e)
{
  if(e.getSource() == print)
  {  String temp="";
      temp=rice1.getText();        
      output.append(temp);
      rice1.setText("");
  }
Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

Well, "...." doesn't give much insight into what you are doing with your lists, but to perform math operations on the elements you will need to cast them to Integer types. If you are using Java 1.5 or 1.6, auto-boxing will let you add them directly. Otherwise you will need to use Integer.intValue() to convert the Integer objects to int primaries and then add them.

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

What is the future of JAVA ?

JAVA was overcoming Microsoft's .net because of it's platform independence.

As in coming couple of years , Microsoft is going to launch a version of .net that will support all platforms like windows, unix, linux etc.

Then will .net become trouble for JAVA in future ?

Please Reply.

/yawn
Hasn't this been hashed out over and over enough? It's really quite tired and you don't have any unique points of insight.

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

1.) Look at the API for File.
2.) Look at the API for BufferedWriter.

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

What components are in your button[] array? Are they not the JButtons? If they are, you aleady have the button reference - it's button.

If that's not what you have in button[], then you can call getComponentAt() on the frame or panel that contains the buttons to get the component at those coordinates. SwingUtilities has several additional methods for working with component coordinate spaces as well.

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

Just post your code and ask about the parts you don't understand. All you have posted here is your assignment.

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

Bleh, just realized I edited the code in peter's post instead of a separate code posting.

public class AvgArray
{
    // returns the average of an array as an int
    public static int averageInt(int[] array)
    {
        int sum = 0; //all elements together
        for (int i = 0; i < [B]array[/B].length; i++)
        {
            sum += [B]array[/B][i];
        }
        return sum / [B]array[/B].length;
    }

    // returns the average of an array as a double
    public static double averageDouble(double[] array)
    {
        double sum = 0; //add all of nums
        for (int i = 0; i < [B]array[/B].length; i++)
        {
            sum += [B]array[/B][i];
        }
        return sum / [B]array[/B].length;
    }

    /**Main Method*/
    public static void main(String args[])
    {
        // Array
        int[] a = {1,2,3,4,5,6};
        double[] b = {6.0,4.4,1.9,2.9,3.4,3.5};

        System.out.println("Average of array a = " + averageInt(a) );
        
        System.out.println("Average of array b = " + averageDouble(b) );
    }
}
Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

plz send me inventory management project in java immedietly

No.

Where do you get off demanding code from anyone? Do your own work or fail. No one here will shed a tear.

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

Closer, but still off by one. The methods are using a and b directly, instead of the array parameter :P

You probably don't even want to define those array at the class level. They aren't related to the calculations themselves. Just define them in main() and use the methods for the calculations. You only need them at the class level if they are state that the class needs to maintain.

Note I also changed the method names to something that describes what they do a little better. "averagea" isn't very clear about the purpose of the method.

I changed method avaragea to return double if you wish you can correct it back to int

public class AvgArray
{
    // returns the average of an array as an int
    public static int averageInt(int[] array)
    {
        int sum = 0; //all elements together
        for (int i = 0; i < [B]array[/B].length; i++)
        {
            sum += [B]array[/B][i];
        }
        return sum / [B]array[/B].length;
    }

    // returns the average of an array as a double
    public static double averageDouble(double[] array)
    {
        double sum = 0; //add all of nums
        for (int i = 0; i < [B]array[/B].length; i++)
        {
            sum += [B]array[/B][i];
        }
        return sum / [B]array[/B].length;
    }

    /**Main Method*/
    public static void main(String args[])
    {
        // Array
        int[] a = {1,2,3,4,5,6};
        double[] b = {6.0,4.4,1.9,2.9,3.4,3.5};

        System.out.println("Average of array a = " + averageInt(a) );
        
        System.out.println("Average of array b = " + averageDouble(b) );
    }
}
Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

Also, never throw exceptions from main().

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

You can change it to whatever you like. You just need to be sure that it starts off with an initial value. In your current code, if the result of your expression were say 11, grade would never get a value. Then your println would try to print something that had no value. You can start with any default value you like as well, you just need to make sure it has one.

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

If you first run the text through an expression that separates the uncommented code sections from the commented out sections, you can then parse only the uncommented sections.

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

Actually, it complains that grade might not have been initialized. "grade" is only given a value in the switch statement, which is a conditional. This means it's possible that grade might not get any value assigned and your later code which uses the variable may fail. The compiler warns you of this and requires that you have a definite value assignment somewhere before the value gets used.

If you assign it a default value before the switch the compiler will be satisfied.

char grade = 'F';
Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

I have read Coma, which I think is one of his books, and really liked it. I forgot about him until you mentioned his name. I definitely have to go and read some more of his books.

Yes, Coma is one of his earlier books. He has written a lot since.

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

Thnx for ur reply but I didn't get you I don't have any params in my applets

Yes, applets have parameters, like "code". Your tag is not written correctly and I showed you exactly where it was broken.
Using the applet Tag

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

You closed your applet tag before your parameters. Basically it's malformed HTML that is causing your problem.

<APPLET[B]>[/B]  code="chat.class" width=650 
height=600></APPLET>

Side note: don't bold your entire message for no reason.

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

jasimp, if you like Patteron and Crichton, you might want to check out Robin Cook as well.

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

Ok, you still haven't posted a question. We are not going to read all of the requirements and make sure that all of your code meets those requirements. That's your instructors job - grading your work.

If you have a question, state it clearly.

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

Well, I don't know your full requirements at this stage of the assignment. If you only need to display one current book item, then the text fields will be just fine. If you need to display the entire inventory list, you may still need the text area or a JList to display them.

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

You will definitely need to have the text fields for adding a new entry or editing an entry, so putting those in now would be just fine.

If you did want to keep the text area, you would just need a method that set the text to a string showing the currently selected book.

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

1. There is an example right near the top of using it to read from console input. I have a hard time believing your teacher has not covered input at all yet though if they have assigned this project.
2. Yes, a method is just like a formula. It can take inputs, do something with them, and return an output. This part should be glaringly simple if you have followed anything in class so far.

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

Ok, it's pretty close but missing one important step: actually showing something in your text area. The listeners change your displayBooks index, but you still need a method to show the current book. I would make a method that takes a book index value as a parameter and shows (or selects) that book. Since you aren't currently doing anything with the text area, I can't be more specific about what should go there when the selected book changes.

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

Ok, so what is the question?

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

What can't you figure out? You didn't actually ask anything specific - just "on what statement will I use", which is incredibly vague. I assumed you didn't know how to get the input, since the rest is almost no effort, so I pointed you to the Scanner for collecting the input.

If you have a specific question then state it clearly.

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

As far as i know getText doesnt have any parameters. you would just say

String x=box.getText();
Long l= Long.parseLong(s);

Well, yes, mostly correct, except you want "long" not "Long" and the parameter should be "x" instead of "s". It also needs to deal with the potential NumberFormatException

JTextField txtField = new JTextField();
String textValue = txtField.getText();
long value = 0;
try {
    value = Long.parseLong(textValue);
} catch (NumberFormatException nfe){
    // you can either leave it zero or tell the user it's not a good value
    // or whatever you need to do if the text can't be parsed to long.
    System.out.println("bad value");
}
System.out.println("value="+value);
Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

You just need to use the mod operator on your percentComplete calculation.

int chaptersRead = 1500;
int chaptersInBook = 1189;
System.out.println("total percent read = "+(chaptersRead/(float)chaptersInBook*100f));

// with mod operation to reduce to percent complete for the current reading
float percentOfBook = (chaptersRead/(float)chaptersInBook*100f) % 100f;
System.out.println("percentOfBook = "+percentOfBook);

// or a variation on the same calc
percentOfBook = (chaptersRead%chaptersInBook) / (float)chaptersInBook * 100f;
System.out.println("percentOfBook = "+percentOfBook);

Please use code tags when you post code like this, as it will make it much more readable with the formatting preserved. You should also consider making your variable names more clear instead of using abbreviations. It will help your own clarity of the program as well as others who are reading it.

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

Here it goes, we were told to make a Multiplication thingy that the user can Input something like 5x5 and then the output will be the answer. We were not taught on how to do it yet so I don't have any idea. I'm just hoping that someone can teach me on what statement will I use so that I can study it in advance and so that I can start it coz its out project. >.<

For input you will most likely use Scanner.

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

I understand what you are saying now. I just needed it explained differently, I'm a bit of an idiot.

Bah. My first comment was pretty brief, so asking for clarification is far from being an idiot. Glad you got it :)

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

Sorry, I don't understand what you're saying.

I'm saying that your incrementing methods are changing the state of your Time instance. All you need to do after you change that state is to display the new Time

System.out.println( "Time before incrementMinute method" );
        System.out.printf( "   %s\n", t2.toString() );
        t2.incrementMinute()
        System.out.println( "Time after incrementMinute method" );
        System.out.printf( "   %s\n", t2.toString() );

The logic to manage the state of your Time instance has nothing to do with displaying it. You already have a method for the display, so you just need to call it.

You could have made the increment methods return a formatted string that was ready to display, but then you would be mixing what really should be separate responsibilities: managing state and viewing it. Keeping those two separate, as they are now, keeps your design more flexible.

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

RETURN false

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

Well, you could develop an artificial intelligence program that suggests final year projects for students. We get posts like this all the time and our little environment would certainly benefit from your system :P

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

If you are running in Windows, try prefacing the command with "cmd /c start ";

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

There is no need for a mthod to retain it's own state separate from it's instance context. If you need to maintain state in an object, make it a class level variable. If you really feel that the method MUST retain it's own separate state then you probably have a case for a separate class instead of a method.

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

Search what? Your file system? The internet? Your closet?
Your question is too vague.

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

I also have a query about ISBN. Some books have one ISBN, SOME have two. Is it appropriate to use ISBN AS a primary key for a book table ( as I have seen it in one book).
What is ISBN. What can be the appropriate primary key for book & other products like laptops.

Please post such questions as a new thread so the original question is not derailed.

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

The incrementing methods don't necessarily need a return value. Just call the increment method and then show the new time toString().

Your set method may also need to throw an IllegalArgumentException when an our of bounds parameter is passed, like 61 minutes. Also, 60 minutes is actually 0 minutes and hour++. :)

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

Well, if you are sticking to the current list implementation, you just need a search method that locates the item. Then call remove(item) from the list. You do not need separate methods to removeArtist() and removeTitle() - just a search to find it so you can call remove. All you need to do to find the item is loop the linked list until you have a field match. If the user can supply either a title or artist and you have no way to know which they supplied then your search method will check against the values of both fields and return any item that matches.

If you are still confused by that, just let me know which part isn't making sense.

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

Joomla is a CMS not a BB.

Yeah, they have forum modules, but it might be overkill if just a simple forum is all that is needed. Haven't worked with it any myself :)

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

Try this instead

import java.applet.Applet;
 import java.awt.*;
 import java.awt.event.*; 
import java.lang.reflect.InvocationTargetException;
 import javax.swing.*;
 import java.util.*;

 public class Game extends Applet implements ActionListener{
   String textB="textB",output="",input="type here";
   JTextField inputBox;
   JTextArea mainBox;
   
   public void actionPerformed(ActionEvent e) {
    if (textB.equals(e.getActionCommand())) {
        Typewriter type = new Typewriter(inputBox.getText());
        type.start();
        inputBox.setText(null);
    }
   }
   
   class Typewriter extends Thread{
       String text=null;
       Random SEED = new Random();
        Random rand = new Random(SEED.nextInt(999999));
        
       public Typewriter(String text){
           this.text = text;
       }
        public void run() {
            char adder;
            for(int i=0;i<text.length();i++){
                 adder=text.charAt(i);
                 //JOptionPane.showMessageDialog(null,adder, "Button",JOptionPane.PLAIN_MESSAGE);
                 output+=adder+"";

               mainBox.setText(output);
                    
                try {
                    sleep(rand.nextInt(25)+100);          
                } catch (InterruptedException ex) {
                    ex.printStackTrace();
                }          
                 System.out.println(EventQueue.isDispatchThread());
            }
            output +="\n";
            mainBox.setText(output);            
        }
   }
   
   public void init() {
       inputBox = new JTextField();
       mainBox  = new JTextArea();
       JScrollPane bottomScrollPane = new JScrollPane(mainBox);
       mainBox.setEditable(false);
       inputBox.setText(input);
       //inputBox.select(0,input.length());
       inputBox.setActionCommand(textB);
    inputBox.addActionListener(this);
     setLayout(new BorderLayout());
      add(bottomScrollPane,BorderLayout.CENTER);
      add(inputBox, BorderLayout.SOUTH);
   }
   
 }

I believe you were getting that behavior because you were sleeping directly in the AWT Event Dispatch thread.

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

Joomla seems to be well regarded also.

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

Perhaps it may be too late into your assignment to consider this, but do you really need to keep two separate lists that only vary by sort order. As you are finding with your current task, having multiple copies of the data means that all operations on that data must be kept in sync. If you later want to be able to sort by genre, you are forced to add another list and more methods to support that list. You can see how such a design becomes more difficult to deal with as it grows.

I think you would be better off keeping a single list and providing methods to resort it as needed.

Is there a requirement that you implement your own linked list and not use a built in collection such as ArrayList? Collections.sort() provides an easy mechanism for sorting based upon any Comparator that you wish to use. Also the collection classes that are part of the Java Collections Framework are already fully featured and tested. It looks like your current remove(int index) still has a bug where it does not update the "next" reference after deletion here

Node curr=prev.getNext();

I know that is a lot of potential change to throw at you after you have gotten this far, but you would have an easier time managing a single ArrayList collection in the long run.