Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

Help with what? You haven't posted a question, and it's certainly not urgent to anyone here.

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

Just run it like any other java file, from either the right-click menu or the Run menu. Netbeans will start it in the Applet Viewer.

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

All of the above suggestions are valid and using a regex pattern is another option. Regex will allow a pretty complex input string to be validated rather simply.

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

Ok, yeah, Notepad needs a "\r\n" carriage return and line feed pair. Using just the newline shows up as a little square symbol. Wordpad will handle the newlines by themselves just fine. If you want to avoid having to mess with knowing which to use when, you can get the system-specific line separator from the system properties like so

String newLine = System.getProperty("line.separator");
StringBuffer sb =new StringBuffer ();
sb.append("First line");
sb.append(newLine);
sb.append("this should go to second line");
PrintStream.println(sb.toString());
darkagn commented: Excellent explanation and good solution to the problem +1
Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

Not really sure why you are getting that behavior. Your writeToLog() code wrote a StringBuffer with newline characters in it to multiple lines just fine here in a tiny test program.

Are you viewing the file with an editor that's okay with just linefeeds or does it expect carriage return and linefeed pairs?

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

But when I get into the second while loop, and try to access the information read in the first while loop (case 14), it is no longer there. That's when I get the "The local variable tokens2 may not have been initialized." message.

That is because you only assign the variable a value inside a conditional expression, so the compiler warns that it may not be initialized when you later try to access it. Give it an initial value and the compiler will be happy.

Does the array in the first while loop just disappear upon termination of the while loop?

No, it's still there because you declared it outside of that loop. It would only lose scope if you declared it within that loop.

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

You can "Flag Bad Post" on it and ask that it be moved to a different forum.

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

It's a little thin, but here is the Sun tutorial link on this: http://java.sun.com/docs/books/tutorial/2d/geometry/strokeandfill.html

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

Buttons that don't react to any events are pretty useless wouldn't you say? Maybe you should use JLabels instead?

If you are wanting a praticular behavior from the buttons, you should describe it a little better, because what you have posted here isn't clear at all.

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

Your images are not appearing on the panel because this code is so riddled with errors that it won't even compile, much less run correctly.

Post some working code and then perhaps your question can be addressed.

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

Yes, good point, I would certainly agree there. The poster didn't give much detail on his process, so it's tough to say what the most efficient arrangement would be.

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

If you want reasonable answers from adults, you need to make the effort to communicate like one.

Do you want career advice from teens only?

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

Why do you need to use a StringBuilder or StringBuffer prior to writing the data to a text file? Is there a reason you can't just use a BufferedWriter?

StringBuilder and StringBuffer are the preferred way to construct long strings from many smaller strings or characters, instead of a large number of concatenations, but if you're writing it to a file, a BufferedWriter should serve the same purpose.

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

Ok, the only thing preventing your loop from working is that that the call to hasNextInt() does not actually read (advance) past the input, it just determines what is there. If you enter a number then your code reads it with the nextInt() call, but if something other than an int is entered, the input is never read and the scanner stays at that position, so every hasNextInt() call still says "Nope, the input isn't an int". To fix that, you need to read off the invalid input before asking again, like so

while(num1==false){
System.out.print("Enter an Integer: ");
    if (input.hasNextInt())
    {
            number1=input.nextInt();
            num1=true;
    }
    else {
        System.out.println("Invalid Input!");
        [B]input.next();  // This will advance the scanner[/B]
    }
}

redZero's code is completely invalid for what you are trying to do, so just disregard it. It doesn't use Scanner, it has no loop to re-query after invalid input, and throws IOException from main() rather than dealing with it in a try-catch block, which is a really bad idea. Exceptions should never be thrown by main().

Something you might want to consider is putting the code that gets the int input into a separate method. You have repeated the same code twice to get two numbers and repetition like that should stand out as a candidate for it's own method.

esy928 commented: AWESOME!!! +1
Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

You only need to know failure or success now. And then later you'll need to know something else and after that something else will come up...
Just start with static final int constants for those return code from the start and even if you don't need to add more, reading this

System.exit(PROCESS_FAILURE);

is a lot clearer than

System.exit(2);
Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

Be sure to define named constants for any error codes you plan on returning and use those instead of the int values. You don't want to to have to go searching through code looking for '16' to figure out what happened with the code.

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

Sure, you can return any int you want from System.exit() and the convention is that a non-zero value is abnormal termination. You can return a specific int for any condition you wish to pass back to the script and report the specific problems or take further action based on that info.

I would recommend a highest-level catch for Throwable and reserve a specific int return code for that. If you hit that catch block, something blew up that all of your other handlers failed to address. You probably want to capture the stack trace and cause information for that Throwable and log it to an error log file.

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

I haven't messed around with styled text panes much, so there may be a more appropriate way to do this, but I did get it to alternate the alignment with the following listener:
(txtInput is just a text field component and I pass the listener a reference to the text panes getStyledDocument() result)

class StyledInsertListener implements ActionListener {

    boolean alignLeft = true;
    StyledDocument doc;

    public StyledInsertListener(StyledDocument doc) {
        this.doc = doc;

        Style defaultStyle = StyleContext.getDefaultStyleContext().
          getStyle(StyleContext.DEFAULT_STYLE);

        Style styleRef = doc.addStyle("left", defaultStyle);
        StyleConstants.setAlignment(styleRef, StyleConstants.ALIGN_LEFT);

        styleRef = doc.addStyle("right", defaultStyle);
        StyleConstants.setAlignment(styleRef, StyleConstants.ALIGN_RIGHT);
    }

    public void actionPerformed(ActionEvent e) {
        String input = txtInput.getText()+"\n";
        try {
            if(alignLeft) {
                doc.setLogicalStyle(doc.getLength(), doc.getStyle("left"));
                doc.insertString(doc.getLength(), input, doc.getStyle("left"));
                alignLeft = false;
            } else {
                doc.setLogicalStyle(doc.getLength(), doc.getStyle("right"));
                doc.insertString(doc.getLength(), input, doc.getStyle("right"));
                alignLeft = true;
            }
        } catch(BadLocationException ex) {
            ex.printStackTrace();
        }
    }
}

The insertString() call by itself would not respect the alignment of the style, so I presume that it was treating it as within the same paragragh. Adding the setLogicalStyle() call did the trick, though it seems redundant to specify that style twice. I couldn't find a mechanism to force a new paragraph prior to insertion, but I would assume there is a way to do that. I just didn't have time to delve much deeper into it.

sciwizeh commented: quite useful info +1
Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

Microsoft has one as well: http://msdn.microsoft.com/en-us/data/aa937724.aspx

As masijade said, this info takes mere seconds to find with Google - use it. If you encounter difficulties finding an answer then post a question.

We certainly aren't going to "write some sample code" for you, if you can't even take the time to search on it.

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

You can either print it directly through the printing APIs: http://java.sun.com/docs/books/tutorial/2d/printing/index.html

or use a report engine like the Pentaho Reporting Engine (formerly JFreeReport)

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

Thanks for the help I was able to get this to work. Is there a list on the Sun site that shows all the different types of exceptions?

Any method that you call on the JDK API will document the exceptions that it can throw and the conditions under which they will be thrown. Those that extend Exception are "checked exceptions" and the compiler will make sure that you have try-catch blocks around those or that your method declares that it throws that exception. Exceptions that extend RuntimeException are considered errors. They are "unchecked exceptions" and the compiler does not require try-catch or "throws" declarations for those.

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

Yes, just read on serialization itself and you should understand why some of those are not serializable and why it's not appropriate for them to be.
Here's a good place to start: http://www.codeguru.com/java/tij/tij0116.shtml

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

itdupuis, you might find this thread over on Ars Technica interesting: http://episteme.arstechnica.com/eve/forums/a/tpc/f/770002407831/m/487008072931

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

Well, unless your BaseConversion.stackreturn() method throws that exception, nothing else in that code will.

It looks like you are actually wanting to catch NumberFormatException from the parseInt() call. Read the API doc on that method: http://java.sun.com/javase/6/docs/api/java/lang/Integer.html#parseInt(java.lang.String)
There's a lot of useful into in those docs - use them.

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

It's just an extended break ("relaxation") until you can begin another cycle as described :)

twomers commented: The vicious... I mean beautiful circle! +6
Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

In this order (ordered by difficulty of problem to be tackled) -- do nothing out of ordinary, music, tea, coffee, beer, Irish coffee, straight whiskey. Normally do a linear iteration through the set and stop when the relaxing method meets the challenge.

Or when you pass out :P

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

can anyone give me another solution?

Why would they? You haven't explained what you have tried, what problems you encountered, your thoughts on potential solutions, or anything demonstrating any effort on your part. Programming involves thinking for yourself and "give me another solution" certainly doesn't imply that you have done any of that.

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

You should be able to step through the string pushing the tokens onto the stack (with appropriate operand and operator validation) and when you encounter a closing paren, pop from the stack back to the last open paren, evaluate that result, and then push that result value onto the stack and proceed forward parsing.

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

Well, from what I read you might be out of luck with an unlimited nesting level. Unless you just have to use regex, a stack-based parsing is probably your best bet.

From my limited playing around with patterns for it,

(5 + 7*3*(x^2-9))

is do-able with a recursive application of the pattern "\\((.+)\\)" (the double backslashes only needed for the Java string - not the desired regex pattern) against the match group, but expressions like

(5 + (7*3)*(x^2-9))

break down when the second pass against the fist match group yields

7*3)*(x^2-9

Perhaps that could be dealt with by using a secondary pattern, but I didn't have time to play with it exhaustively.

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

Technically none of those will work as written, but your original is only wrong by one detail

MySuperType mytype = null;
if (sub_type) {
     mytype = new MySubType (); // this is fine
     mytype.setD(4); // these will fail to compile
     mytype.setE(5);
} else {
     mytype = new MySuperType (); 
}

mytype.setA(1);
mytype.setB(2);
mytype.setC(3);

If your variable is of MySuperType, then you can only call MySuperType methods on it. To call MySubType methods you will need to cast to MySubType for those calls, like so

MySuperType mytype = null;
if (sub_type) {
     mytype = new MySubType (); 
     ((MySubType)mytype).setD(4); // just fine
     ((MySubType)mytype).setE(5);
} else {
     mytype = new MySuperType (); 
}

mytype.setA(1);
mytype.setB(2);
mytype.setC(3);
Ezzaral 2,714 Posting Sage Team Colleague Featured Poster
Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

hi
I am an engineering student as well , i jus did my second year exams , i dont know abt .NET but java is very helpful , i am doing java this summer. Anyways it all depends on the type of engineering u doing , if its electronic then i dont think u really need .NET but if its software then u shud learn as many languages as u can.

Please make an effort to use proper English and not "text speak" per forum policies:
http://www.daniweb.com/forums/faq.php?faq=daniweb_policies#faq_keep_it_clean

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

You could certainly define a Runnable or Callable task that takes a list of IDs to update and queue multiple FutureTasks on a thread pool Executor.

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

Returning results whether they match or not defeats the entire purpose of regex. If you want to split input into groups, you obviously have an idea as to how you want that data broken out. Those decisions drive the design of those capturing group patterns. They can be very broad or very narrow, but matching some pattern within the input has to drive the process of extracting groups.

There are plenty of tutorial on using regex:
http://www.google.com/search?q=regex+java+tutorial&btnG=Search&hl=en&sa=2
but it still always comes down to some trial and error to tweak the pattern to your needs.

If you can post a specific example of your input and what you are trying to capture from it, perhaps we can give you some specific pointers on it.

I've posted a small regex testing form here: http://www.daniweb.com/forums/post392585-8.html that I use sometimes to test and tweak patterns. It's pretty limited in it's intent, but perhaps you may find it useful in playing with your patterns and capturing groups.

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

That depends entirely on what determined the previous state of the table. Your cell renderer just needs enough info to determine, based upon it's value and location, how you want to render it. Not knowing a thing about how you are storing or updating the data that drives that decision, I can't tell you exactly how that should be coded.

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

it is also created with electricity and water, which is quite eco-friendly (depending on how the electricity is generated)!

You're better off leaving the electricity needed for hydrolysis as electricity rather than expending it to create a less-efficient energy form.

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

I don't know who this "u" person is, but simply put that condition in your cell renderer. If you can update it once, you can certainly write a condition that determines if it should be updated again or stay the same.

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

String.split("-")

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

How did it come about that you are supposed to solve a problem with this method for your thesis, yet you don't even know what it is?

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

You need to start with the most basic "getting started" tutorial: http://java.sun.com/docs/books/tutorial/getStarted/cupojava/win32.html

And do not post new questions to the end of other people's old threads. Post a new thread of your own if you have a question.

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

Use whichever layout manager you wish and place them just like you would place components in any container.
http://java.sun.com/docs/books/tutorial/uiswing/layout/index.html

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

Place your components on a JPanel instead and override the paintComponent method of that JPanel to paint the image

ImagePanel = new javax.swing.JPanel(){
    Image img = new ImageIcon(getClass().getResource(
      "images/blackX.gif")).getImage();

    public void paintComponent(Graphics g){
        g.drawImage(img, 0, 0, getWidth(), getHeight(), this);
    }
};
Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

Try iText for PDF processing.

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

If you are wanting to capture speech and process it, read through the tutorial on using the Java Sound API, especially the audio capture portion: http://java.sun.com/docs/books/tutorial/sound/TOC.html

Converting speech to text is not exactly trivial and will involve quite a lot of complicated signal processing and analysis unless you have an existing speech-to-text engine available. None of that is present in the Java APIs.

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

Then ask for explanation on the point that is confusing you rather than asking for direct corrections to your code. You are the one studying and if you actually want to learn the material then you need to make the effort to clarify the points that you are having problems with.

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

Which is why the constructor should just call add() twice with the strings you want to add. Why code a method to manage the array additions and then not use it?

Just because a method is public does not mean that it's not useful within the class is well. Having the code that manages that operation defined in a single place and using it for all additions to your array minimizes the changes needed to alter how that operation works and ensures that if you change that behavior you don't have to make related changes elsewhere in the class.

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

On which line? The stack trace provides the line number and specifics of the error.

"adv" is not defined in addAdvice(), which is most likely the source of your error.

Also, I would point out that the method

public void addAdvice (String advice)
    {
           
       if (index < advice.length) 
       {
           advice[index] = adv;
           index++;
       }
                   
    }

gives you plenty of room to create errors. You have named the string parameter the same as your array instance variable. I would recommend changing that parameter name to something like "entry" to avoid ambiguity and scope issues.

sktr4life commented: thanks for all the help... +1
Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

Well, replace selection only replaces selected text. Why are you not using setText()?

You may want to consider grouping the text entry components into a collection (List or Map perhaps) to make mass operations like that easier to handle.

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

Most of the example I can find use a small inner class for the Authenticator, such as

private class SMTPAuthenticator extends javax.mail.Authenticator {
        public PasswordAuthentication getPasswordAuthentication() {
           String username = SMTP_AUTH_USER;
           String password = SMTP_AUTH_PWD;
           return new PasswordAuthentication(username, password);
        }
    }

The full example is here: http://www.rgagnon.com/javadetails/java-0538.html

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

They can share the same UndoableEditListener. Example

import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.JComponent;
import javax.swing.KeyStroke;
import javax.swing.event.UndoableEditEvent;
import javax.swing.event.UndoableEditListener;
import javax.swing.undo.CannotUndoException;
import javax.swing.undo.UndoManager;


public class UndoDmeo extends javax.swing.JFrame {
    private javax.swing.JTextField txt1;
    private javax.swing.JTextField txt2;

    final UndoManager undo = new UndoManager();

    final UndoableEditListener editListener = new UndoableEditListener() {
        public void undoableEditHappened(UndoableEditEvent evt) {
            undo.addEdit(evt.getEdit());
        }
    }; 
    
    public UndoDmeo() {
        initComponents();
        getRootPane().getActionMap().put("Undo",
          new AbstractAction("Undo") {
              public void actionPerformed(ActionEvent evt) {
                  try {
                      if(undo.canUndo()) {
                          undo.undo();
                      }
                  } catch(CannotUndoException e) {
                  }
              }
          });
        getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
           KeyStroke.getKeyStroke("control Z"), "Undo");
        
        txt1.getDocument().addUndoableEditListener(editListener);
        txt2.getDocument().addUndoableEditListener(editListener);
    }

    private void initComponents() {

        txt1 = new javax.swing.JTextField();
        txt2 = new javax.swing.JTextField();

        setDefaultCloseOperation(EXIT_ON_CLOSE);
        getContentPane().setLayout(new java.awt.FlowLayout());

        txt1.setPreferredSize(new java.awt.Dimension(100, 20));
        getContentPane().add(txt1);

        txt2.setPreferredSize(new java.awt.Dimension(100, 20));
        getContentPane().add(txt2);

        pack();
    }

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new UndoDmeo().setVisible(true);
            }
        });
    }
}