JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Hi stevanity. His code uses nextInt, which returns an int or throws an E.
Yes, he can change it to read a String then do his own parsing, but why bother? Especially since he's already having so much difficulty with basic program flow and logical expressions.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Please re-read the template. You changed it so that after catching an exception you go ahead and execute the rest of the code anyway.
line 10 still has the error that has been pointed out to you many times now. Are we wasting our time?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Last time: if it's a letter then nextInt() will throw an exception, which you catch. nextInt returns an int, so its logically impossible for it to return a letter.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

I gave you a pseudocode template for all this yesterday

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

You need to test the index to be in the right range BEFORE you use it as an array index. You don't need to check for it being a number because you use getInt and it will throw an E if its not.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

You can read/write a RandomAccessFile using your own code to convert your objects to/from bytes of data.
You can use Java ObjectOutputStream / ObjectInputStream to write/read simple or complex objects (including arrays & collections) with a single statement.
You can use XMLEncoder /XMLDecoder to encode/decode simple or complex objects (including arrays & collections) as XML text that you can write/read to file.

ps Classes (names starting with a capital letter) above are fully documented in the JAVA API doc.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

@soham.m17: How about an update on this? What did you do in the end? How did it work?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

OK, I just nticed you changed the order of the if as well as the operators

if (c[nChoice] == null || nChoice < 0 || nChoice > c.length | nChoice>m.length) {

This version executes c[nChoice] before testing whether nChoice is a valid index, hence outofbounds.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

In that case, here's a slightly harder to understand, but much faster algorithm:

for i = array.length-1; i>=1; i--
   get random int r = 0 to i
   swap element i with element r

(this is the algorithm used in Collections.shuffle)

This implementation traverses the list backwards, from the last element up to the second, repeatedly swapping a randomly selected element into the "current position". Elements are randomly selected from the portion of the list that runs from the first element to the current position, inclusive. This method runs in linear time

Source: JavaDoc for Collections

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Do you still get that problem after fixing your boolean operators?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster
Create an array of doubles.
loop quite a few times 
  generate two random ints 0 to (array.length-1)
  swap the array elements at those two indexes

result: a shuffled array of doubles.
What's so hard?

ps Or use Collections.shuffle with your doubles wrapped in Doubles

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

you cannot shuffle double numbers

Would you like to justify that somewhat surprising statement?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Please do not try to make me do all the work by saying "still an error". Do you think I am telepathic? This is the last such post I will respond to unless it has a complete description of the error, complete with full text of error messages or exception stackTraces as appropriate. :icon_wink:
Anyway:
nChoice<0 && nChoice>=c.length
must always be false, no value can less than zero AND >= length at the same time. Time to revise your binary operators???

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Here's a little pseudocode template that shows the general approach. It will loop asking for input and diagnosing exceptions and failed validation tests until good input is received

boolean inputIsOK = false;
while (! inputIsOK) {
  prompt user
  try {
    get input
    if (input < 0 && input >= size)  print error message
    else inputIsOK = true;
  } catch(Exception e) {
    print error message
  }
}
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Here's a simplified version of your code that shows adding days to today's date and printing them out...

public static void main(String[] args) {

      Calendar ca1 = Calendar.getInstance();

      int nights = 8;
       
      ca1.add(Calendar.DATE, nights);
       
      System.out.println((ca1.get(Calendar.DATE))
      + "/"
      + (ca1.get(Calendar.MONTH) + 1)
      + "/"
      + ca1.get(Calendar.YEAR));
       
   }

Run that, confirm it works, then put the rest of your code back into it until you find out where it breaks.
ps I have to go out in 5 minutes from now...

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

I just copied and ran your original code (with the latest change to the MONTH line) and it works here.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

So, it's working now?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Look at the suggestion I coded and look at your code. Think about it. I'm here to help you learn to be a Java master, not to do it for you :-)

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

You put the brackets in the wrong place

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

That's odd - please post the relevant code in its latest version

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

ca1.get(Calendar.MONTH + 1)
Calendar.MONTH is an int constant signifying "month", so MONTH+1 is another int constant signifying (I don't know what), so it get's the wrong field. You need
(ca1.get(Calendar.MONTH) + 1)

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Yeah, I can't remember now where I found the getProtectionDomain bit - that's the most non-obvious thing, but I always stash stuff like that away in case it comes in useful later. I use it to display the jar file creation date when there are problems - a kind of automatic version number check.
Let me know when you get the whole thing working?
Cheers
J

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Those are variables not methods. They are (correctly) private, so you can't access them from outside the class anyway. You need to use the public methods.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Don't ask me - ask the compiler! It's good that you put the bok. in, but what follows must be the name of a method. Have another look at the Konto class to check the method names.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Excellent! Is this thread "solved" now?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Is there a space between -jar and "%1" ?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

I don't think you can use getResource... for things outside the jar/classpath (hopefully someone will correct me if I'm wrong...).

However, this little snippet from deep in my old code box will give you a File reference to the jar that the class in which you execute this code came from...

File jarFile = new File(this.getClass().getProtectionDomain().getCodeSource().getLocation().toURI());

then from that File object you can extract the containing directory (getParentFile()) and use that to make the full path to your text files

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Why would that possibly work? There's no object called "get" and there's no method called Kontonummer1. Making random changes is never the answer.

You correctly call getNavn method just one line earlier. Can you see how similar getKontonummer is?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

cannot find symbol
symbol : variable JOPtionPane Java is case-sensitive JOPtionPane != JOptionPane

getKontonummer() is not a method of that class, it's in class Konto

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

java -jar myjar - sounds like you may have missed the -jar???
(Going offline now - back tomorrow. Good luck)

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

That's weird. That error normally means the code was compiled for a particular target version of the JRE (eg 1.6), but the actual JRE you are using is an earlier version (eg 1.4.2). But if you have done a proper install of a current JDK on your machine then the compiler and JRE versions should match. Anyway, check the compiler and JRE versions, and reinstall with the latest & greatest from Oracle (1.7) if in doubt.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

java,exe takes the name of your class, not the name of your class file...
so that should be simply
java main

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

You can't "return the println" - that makes no sense. println is a method, not an object. Your usual options would be either to return null or to throw an exception if the find fails to find anything.
ps: Remember that you can return from anywhere in a method, including inside an if test, and you can have multiple different return statements depending on what conditions you find...

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

@mKorbel: snap! J

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Sleeping in a Swing GUI usually stops everything from working by blocking the Event Dispatch Thread ("Swing thread") on which Swing handles everything. Its never a good idea. Use a javax.swing.Timer to implement delays and repeated processes.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Line 111. Shouldn't that be props.put("mail.smtp.host", host); ?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Yo Norm.
You don't need to do that by hand - to quote the Java Lang Ref

each enum type has a static values method that returns an array containing all of the values of the enum type in the order they are declared.

so all you need is: Value aVal = Value.values()[1];

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

No, the enums are great - try something like:

ArrayList<Card> pack = new ArrayList<Card>(52);
for (Suit s : Suit.values())
  for (Value v : Value.values())
    pack.add(new Card(v, s);

now you can draw a card realistically with something like

pack.remove(rand.nextInt(pack.size() - 1));
Bladtman242 commented: Solved my problem :) +3
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

In real life card games, cards are drawn from one or more shuffled packs, so a more realistic code design would be to create a new "pack(s) of cards" which was a collection (eg ArrayList) of n*52 Cards (n of each), then the draw method just takes a random card from the pack, until there are none left (via a random int, 0 .. (size of deck -1)).

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Floating point numbers are represented in binary fractions. (1/2 + 1/4 + 1/8 + 1/16 etc). You fractional value is 0.3, ie 3/10. There is no exact way to represent 3/10 using only binary fractions, so the answer has to be slightly wrong. This is true of all floating point arithmetic on all binary computers.
When using floating point you must always be aware of this, and be ready to round your output to the right number of decimal places when you print it.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

What Norm says is true. IMHO it's a no-brainer because approach (1) deals with the objects themselves, while approach(2), while it updates the image OK, doesn't address the question of what exactly was the thing you moved.

Just for fun I knocked up this little demo of approach 1 with draggable objects (sprites) and lines connecting them. Feel free to have fun with it.

import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;

import javax.swing.*;

public class Demo {

   public static void main(String[] args) {
      new Demo();
   }

   JFrame frame = new JFrame("drag demo");

   ArrayList<Sprite> sprites = new ArrayList<Sprite>();
   ArrayList<Line> lines = new ArrayList<Line>();

   Demo() {

      frame.setMinimumSize(new Dimension(400, 300));
      frame.setLocationRelativeTo(null);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      DrawPanel drawPanel = new DrawPanel();
      drawPanel.setLayout(null);
      frame.add(drawPanel);

      Sprite sprite = new Sprite("drag me");
      sprite.setBounds(10, 10, 50, 15);
      sprite.setForeground(Color.blue);
      drawPanel.add(sprite);
      sprites.add(sprite);

      sprite = new Sprite("and me");
      sprite.setBounds(100, 40, 50, 15);
      sprite.setForeground(Color.red);
      drawPanel.add(sprite);
      sprites.add(sprite);

      sprite = new Sprite("and me too");
      sprite.setBounds(300, 90, 65, 15);
      sprite.setForeground(Color.green);
      drawPanel.add(sprite);
      sprites.add(sprite);

      Line line = new Line(sprites.get(0), sprites.get(1));
      lines.add(line);
      line = new Line(sprites.get(1), sprites.get(2));
      lines.add(line);

      frame.setVisible(true);
   }

   class Sprite extends JLabel implements MouseListener, MouseMotionListener {
      
      // A sprite is a movable draggable object

      public Sprite(String text) {
         super(text);
         addMouseListener(this);
         addMouseMotionListener(this);
      }

      public Point getConnector() {
         // returns point where connecting line(s) should meet this sprite
         return new Point(getX() + getWidth() / 2, getY() + getHeight() / 2);
      }

      @Override
      public void paintComponent(Graphics g) {
         // can do custom drawing here...
         super.paintComponent(g);
      }

      int startDragX, startDragY;
      boolean inDrag = false;

      @Override
      public void mouseEntered(MouseEvent e) {
         // not …
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

You may find this thread interesting & relevant...
http://www.daniweb.com/software-development/java/threads/375002

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

I think you are going to have great difficulty doing D&D on parts of a single graphic. Maybe you should consider drawing each individual draggable thing in its own JLabel (setOpaque(false)), then it's reasonably straightforward to handle clicks & drags for each JLabel.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Use the string method charAt(int index) in a loop to get the chars one at a time. The Char class then has methods to tell you if a char is a letter or a number or whatever.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

If you look at the correct solution you will notice that each pair consists of an element and a second element that comes later in the array, ie

for each element i in codesArray
  for each element j that appears after i in codesArray
    pairThese(i,j)
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

You need to post your code (in code tags) for anyone to answer that.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Have a look at the Integer class in the API doc
http://download.oracle.com/javase/6/docs/api/
you will find there a method called parseInt(String s)
which takes a String and parses it and returns the value as an int (provided the String represents a valid int). That's the method you need to convert your String to an int
Read the API doc for all the details.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

You declare int Value[] so value on line 134 represents an int.
ints (like booleans and floats) are "primitives", not Objects, so they do not have methods. That's why your code Value.equals... gives an error - yuo try to call the equals(..) method on a primitive that does not have any methods.

Even if that was OK it still wouldn't work because Value is an int and search_a_value is a String.
You could convert the String in search_a_value to an int value, then just use == to compare that with value.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

You could easily separate the SQL part (around lines 52-68) into a method with the signature
public ResultSet getDataFor(String user, String password)
and that method could be in a different class. That would make the code easier to understand and maintain.

But ideally though it would be better to separate the GUI aspects from the SQL aspects completely. How this usually is done is by looking at the "meaning" of the data that is being returned - for example the result set may represent a number of Students, or CompactDisks, or Vehicles or whatever. In that case there will usually be a class that represents those things. Now you can have an SQL-based class that does the select and creates (eg) Student objects from the rows of the result set
public List<Student> getStudents(String userID, String password)
the GUI class can call that method, then populate the table with the data in those returned Student objects.
That way the SQL is kept entirely in one class, the GUI is kept entirely in another, and what links them is the Student class, which is independent of both the SQL and the GUI.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Note also that if you declare a class as being in package xxx then Java expects to find that class's files in a directory called xxx.
So if you add a package spec to a class, you also need to add the corresponding sub-directory and move your files into it.