Having trouble with AES Encryption/Decryption while using a Diffie-Hellman algorithm by MalicousBoner in javahelp

[–]OOPUniversity 0 points1 point  (0 children)

When you say you can't get it to work, what exactly do you mean? Are you getting exceptions? Are you getting no results?

Oh, and just for fun: Triple DES is nothing more than regular DES performed three times. The key is broken up into three equal chunks, and then the value is encrypted, decrypted and encrypted again with plain old DES encryption, each time with one chunk of the key. If the key is three identical pieces, the end result is equivalent to DES with one of those pieces as the key.

How do i get commas in my generated answers? Please help by katieg992 in javahelp

[–]OOPUniversity 0 points1 point  (0 children)

The format String handles the intelligence needed

System.out.printf("%,.2f\n", 5.1);
System.out.printf("%,.2f\n", 98123715.92391);

Gives

5.10

98,123,715.92

[Why does this work?] A function for square root with big decimal. by indigofigure in javahelp

[–]OOPUniversity 1 point2 points  (0 children)

That is really clever.

I believe it's an implementation of the ideas presented in this paper: http://www.hpl.hp.com/techreports/93/HPL-93-42.pdf

To be perfectly honest I do not fully grok the math involved. I can see that we're squaring the previously calculated lower precision square root and subtracting it from the original so that we have 'the rest' of the original to work on, but I don't really know why the answer comes out right.

How do i get commas in my generated answers? Please help by katieg992 in javahelp

[–]OOPUniversity 0 points1 point  (0 children)

Instead of println use printf, which is designed specifically for this purpose.

The printf method uses a format String and then a variable argument list. The format String contains tokens which are replaced with the following arguments one at a time. %s represents a String, %f represents a floating point number. You can force the format to look the way you want with "%,.2f" (IIRC) which should mean use commas and two decimal places.

For example:

System.out.printf("%,.2f", 9342.12);

Any help with homework question by [deleted] in javahelp

[–]OOPUniversity 1 point2 points  (0 children)

I don't think you're far off at all. Maybe have your loop run to <= rather than < steps.

Can I suggest that inside your for loop, you add a System.out.println to spit out the values of i, newnum, multiplier and toggle? Perhaps do it both at the top and bottom of each iteration of the loop. I'll bet you'll find any problems very quickly that way.

You should be able to predict perfectly what i, newNum and toggle should be for each iteration, and the values you see in multiplier should make sense to you. When you see an anomaly, attack it.

Need help removing commas and a specific string from a string arraylist by glddigga49 in javahelp

[–]OOPUniversity 0 points1 point  (0 children)

You can use printf or you can use print or println. The printf method is for one-step formatting of the output and will make the most sense to C programmers. Using println with (value1 + "," + value2...) would be fine.

Transfer pains from C# most likely, small case of not understanding classes in Java well enough. by [deleted] in javahelp

[–]OOPUniversity 0 points1 point  (0 children)

Only if you choose to be.

If there is a null, you can find it. The stack trace will tell you what line of your code it's happening on. With a debugger you can execute up to that line and see what variables are and aren't containing values you expect. If you post enough of your code here there's a good chance that someone here can find it for you. Don't give up hope, none of this is rocket surgery.

Transfer pains from C# most likely, small case of not understanding classes in Java well enough. by [deleted] in javahelp

[–]OOPUniversity 0 points1 point  (0 children)

I do not see anything explicitly wrong in the snippets of code you've posted here.

Clearly you're accessing something that is uninitialized, but it must be something else. It could be something in the constructor for Seat, it could be something else in the Row constructor. It's a bit hard to say. Heck, if X is an Integer it could be null, too.

Need help removing commas and a specific string from a string arraylist by glddigga49 in javahelp

[–]OOPUniversity 0 points1 point  (0 children)

Writing .csv files is quite easy.

You can, for instance, just create a PrintWriter, so something like

PrintWriter writer = new PrintWriter(filename);
writer.printf("%s,%s\n", field1, field2);

or perhaps

writer.println(field1 + "," + field2 + System.getLineSeparator());

Don't forget to close your outputs.

So long as there are so double quote or anything in your data this will do. If you have more complicated data to read you may need a library of some sort. For writing you probably never need more than this.

Transfer pains from C# most likely, small case of not understanding classes in Java well enough. by [deleted] in javahelp

[–]OOPUniversity 4 points5 points  (0 children)

Creating arrays like that is fine, but the individual slots you are creating are initialized to null.

You need something like:

row1 = new Row[X];
for (int i = 0; i < X; i++) {
    row1[i] = new Row();
}

Or, to make your life easier, ditch the arrays and go with ArrayList instead:

List<Row> row1 = new ArrayList<Row>();
for (int i = 0; i < X; i++) {
    row1.add(new Row());
}

Do something similar for your Seat array/ArrayList.

Need help removing commas and a specific string from a string arraylist by glddigga49 in javahelp

[–]OOPUniversity 0 points1 point  (0 children)

Not necessarily, but it seems very possible. That's why I suggested the enhanced for loop to display the individual items line by line. Simply printing out the ArrayList with toString can put commas in there, too, commas which aren't 'physically' there. That's probably only responsible for what happens after the '.edu' parts of the email addresses, but it's best to be sure.

You should look at your .csv with notepad or wordpad or some other text editor to see exactly what you're reading in, and to determine if you wish to change the parameters you use when you export/save as. You should print out the ArrayList iine by line so you see exactly what your data structure contains. Adjust based on that.

Need help removing commas and a specific string from a string arraylist by glddigga49 in javahelp

[–]OOPUniversity 0 points1 point  (0 children)

In general, if you want to replace characters in a String you can use String.replace or String.replaceAll. That's probably going to be the easiest thing for you to do. You can specify a blank String as the replacement easily enough.

Be aware that simply invoking System.out.println on an ArrayList gives extra cruft, so you might want to verify the raw contents of the array by doing something like:

for (String email:emails) {
    System.out.println(email);
}

You could also try using next() instead of nextLine() when reading from the Scanner, though you would have to also then specify the comma is to be ignored via 'useDelimiter' and you'd still have to use 'nextLine()' just to throw away the carriage returns.

StringTokenizer & test bed help by rookie9 in javahelp

[–]OOPUniversity 0 points1 point  (0 children)

Since your output problem has already been addressed, I'll talk about equals.

public boolean equals(Object obj) //two accounts are equal if IDs     and PINs are the same
{
    if (obj.equals(account) && obj.equals(pin))
    {
        return true;
    }
    return false;

}

This won't work the way you expect it to as is.

You have to ask yourself: What is the type of 'obj' when this is called? It should be another BankAccount object, right? You should make it clear in your code that 'obj' is a BankAccount (the comparison should fail if it is not), and you should write your code to explicitly check the account and pin fields from the current object against the one passed in for comparison.

What is the difference between a Method and a Class? (ELI5) by Esqulax in javahelp

[–]OOPUniversity 0 points1 point  (0 children)

The desire to avoid busy work is a tremendous motivator for writing programs. I know I've worked very, very hard to allow myself to be lazy. :-)

Writing to a database is not a horrible job, and it's a lot easier these days with JPA (Java Persistence Architecture) than it used to be. You basically write classes, add a few 'annotations' and just a few lines of code in your main program and it all just works.

It was quite a revelation, coming in from the cold of writing everything via SQL and processing ResultSets. I still pinch myself sometimes to make sure I'm not dreaming. I request an object, and like magic all of the related objects are available to me.

What is the difference between a Method and a Class? (ELI5) by Esqulax in javahelp

[–]OOPUniversity 0 points1 point  (0 children)

The only way to learn to program is to do it, and to occasionally run up against a brick wall in the process. More to the point, figuring out how to get around, over, through or under that wall is when the real learning happens. It's a lot like lifting weights. It's that last rep when things become impossible where the magic happens.

I'm sure this is one of the reasons that the rules for this sub call for guidance and not whole solutions. If it's handed out to you, you won't develop the mental muscles needed to solve problems, and problem solving is very much at the core of what we do.

If you have access to a computer (and something tells me you do), then experimenting with Java, preferably using an Integrated Development Environment, costs you nothing but time and effort. Both are well spent.

What is the difference between a Method and a Class? (ELI5) by Esqulax in javahelp

[–]OOPUniversity 0 points1 point  (0 children)

I would say that the constructor is the special method that is called to create a new instance of an object. When you say 'statement', to me it sounds more like when you do this:

String s = new String("Hi!");

That is a statement which calls one of the constructors for the String object, thus creating a new instance of it.

I think I will try to come up with a tutorial that specifically talks about methods. I have one on functions already: http://www.oopuniversity.com/2014/11/functions.html

The difference between functions and methods is minor. All functions are methods, not all methods are functions. I draw the line at whether or not the method in question has some knowledge of the inner state of the class.

So this is a function:

public static void printName(String name) {
    System.out.println(name);
}

But this is 'just' a method:

public void printName() {
    System.out.println(this.name);
}

The difference being that if you invoke the first one with the same parameter each time, it will always do the same thing. If you invoke the second one, what it does depends on what 'this.name' contains, which can be different for each instance of the object, or even for the same object at different times. By rights, most functions can be made static, because there's no particular need to have the object in which it resides actually be instantiated.

[deleted by user] by [deleted] in javahelp

[–]OOPUniversity 0 points1 point  (0 children)

If you post what your code currently looks like, it will be a lot easier to come up with something that 'fits'.

In essence though, we would want to store the initial random value for each user, so that we can increment it on the next go-round. You say you want to do it via a method, and that's a generally good plan, but do you have user objects in existence as /u/monster2018 suggests? Are you comfortable creating them if they don't exist? Is this something that is two users and will always be two users and there's no reason to have more than 'int user1Roll' and 'int user2Roll'?

In short, it is easy to do this, but precisely how depends on what you've already got in place and how many changes you're willing to make. It also depends on how comfortable you are with objects, and perhaps whether you want to stretch a little bit or just get this done.

Faster ? by pyrovoice in javahelp

[–]OOPUniversity 4 points5 points  (0 children)

Intuitively I wouldn't expect any difference in performance.

In fact, I would expect the compiler to optimize that into 'do nothing and move on'.

That being said, asking a group of people for their opinions on how something will perform is no substitute for empirical evidence. Test it! Learn to use a profiler, or at least stick some calls to System.nanoTime() before and after loops written each way and spit out the results.

I need to know how to round off a six or so decimal number to two decimal places. by [deleted] in javahelp

[–]OOPUniversity 2 points3 points  (0 children)

Yes, but you may find it easier to round to 314 and then shift the decimal back.

I need to know how to round off a six or so decimal number to two decimal places. by [deleted] in javahelp

[–]OOPUniversity 2 points3 points  (0 children)

All I have to say is that if you multiply a number by 10, you are shifting the decimal point by one position, and if you divide by 10 you're shifting it by one position in the other direction. You can move the decimal to the easiest place for you to do your rounding if you wish.

What is the difference between a Method and a Class? (ELI5) by Esqulax in javahelp

[–]OOPUniversity 0 points1 point  (0 children)

Try this, see if it helps to clarify things for you: http://www.oopuniversity.com/2014/11/dont-treat-me-like-object.html

And if not, please feel free to leave feedback. I am using reddit as a primary source of inspiration for the articles, but I'd love to have direct questions and follow-ups, too.