Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
lamentable dustman
Apr 13, 2007

🏆🏆🏆

rileylolz posted:

For class, we've been assigned to make an 8x8 board for various pieces to move around on. I've run into a problem with user input, specifically dealing with how to handle tokens. I'm using Scanner to break up the input, but the problem is the largest command will have 5 tokens and the shortest will only have one. The way it's set up now is that it needs a command that will use all 5 tokens or else it crashes.

code:
		while (sc.hasNext())
		{
			cmd = sc.next();
			i = sc.nextInt();
			j = sc.nextInt();
			token4 = sc.next();
			token5 = sc.next();
			break;
		}
How can I break this up so it checks to see if there are any more tokens in the string before crashing? I tried if(sc.hasNext()) after each one, but it didn't work.

I'm still fairly new to java, any help would be appreciated.

And just an example of what the user input would look like:
"create 4 3 fast flexible" with fast and flexible being different types of pieces. It works fine with a command like that, but crashes with a command like "print"

Lazy way would be to catch the exception and ignore it. The more proper way would be to have a count=0 outside of the switch statement. Then have a switch statement based off what count it is. After the switch increment the count.

You could probably get rid of the loop also with a series of ifs as well. You would check hasNext() and if true whatever equals next()

Adbot
ADBOT LOVES YOU

lamentable dustman
Apr 13, 2007

🏆🏆🏆

Keeper of Bees posted:

image stuff

If you are using a BufferedImage you should be able to get a Grapics2D object out of it and IIRC it has some rotate methods.

If all you are doing is what is in your example though it should be hard to do manually. From your example, get the dimensions of your array using array.length and array[0].length. From that create a new array flipping the dimensions. Fill the second array using the standard double for loops just do array2[j][i] = array[i][j]. Or something to that effect.

lamentable dustman
Apr 13, 2007

🏆🏆🏆

I don't normally write applets so I need some help with this badly. I have an applet that is hosted in in Tomcat that needs to communicate with a servlet on the same server. The servlet feeds the applet with the required data it uses for processing.

Here is how I connect it :

code:
URLConnection conn;
try{
   URL url = new URL("http://123.123.123.123:8080/webcontent/servlet");
   conn = url.openConnection();
   conn.setDoOutput(true);
   conn.setDoInput(true);
   ... (other setupcode, on secure system so can't c/p it)
   
   OutputStream out = conn.getOutputStream(out);
   ObjectOutputStream oos = new ObjectOutputStream(out);
   oos.writeObject(*data*);
   //receive data and close all connections
}catch(Exception e){

}
This all worked fine on my local machine. The URL pointed to htpp://localhost:8080 but now I am integrating with the server and I'm getting all kind of problems. I'm getting an error "java.security.AccessControlException: access denied (java.net.SocketPermission <url> connect, resolve)".

Now I understand all the restrictions with applets. I know they can't communicate with just any server and can't modify local files. But they are suppose to be able to communicate with a remote server IF the applet was downloaded from there, right? Any ideas? I got to get live data to the applet to be displayed.

edit: the error is thrown at "conn = url.openConnection();"



edit2: gently caress me.... figure it out.... when I was testing poo poo locally again I set the URL ip to my machines IP instead of localhost. When I tried to connect to it from my local Tomcat server I reached it via localhost:8080 instead of with my IP address. Not only can't applets communicate with remote servers, the url have to be the same I guess. So if you hard code a IP into the URL object and try to access the page it sits in via a DNS address the applet will break, right? So I need to use JSP or my web.xml file to pass the url context incase it moves or for we set up a dns for it.

I wish it was smarter then that.

lamentable dustman fucked around with this message at 16:50 on Mar 10, 2009

lamentable dustman
Apr 13, 2007

🏆🏆🏆

It wasn't so much whining as me bitching because of my own stupidity and taking a couple hours to figure it out. Mostly because I wrote a long post then figured it out 5 minutes later, as happens whenever I post here.

Anyways, what I do now is write a little jsp to do the following

code:
<param name="url" value="<%out.print(request.getScheme()+"://"+request.getServerName()+":"request.getPort());%>">
to pass the root url to the applet, from there form the path to the corresponding servlet.

lamentable dustman
Apr 13, 2007

🏆🏆🏆

Sorry, missed understood what you meant there. Didn't know there was a method for that. I'll update that, thanks for the tip.

Like I said, I don't do applets generally, I just happened to make a mistake of showing my boss some of the features of the library I was using (JFreeChart). I was originally going just server some static pictures, from a servlet, representing weather data. I showed him some of the applet and dynamic features of it and next thing I know I'm making applets. Just trying to hack my way trough this poo poo and be done with applets for good and focus on backend stuff again.

lamentable dustman
Apr 13, 2007

🏆🏆🏆

ayb posted:

stuff

When arrays are declared they have to have a set size because sequential memory is allotted to them when instantiated. They can not grow past that size otherwise you will get an array out of bounds error.

If you need the array to be dynamic and grow based on the amount of items you will either need to use a different data structure (ArrayList). You could also create a function that returns an array of the correct size with the new item in it (google java growing array).

Obviously use an ArrayList assuming advance data structures are allowed in your class.

lamentable dustman fucked around with this message at 23:33 on Mar 26, 2009

lamentable dustman
Apr 13, 2007

🏆🏆🏆

ayb posted:

I understand now. I emailed the professor and he said to just use a fixed size and not do anything more advanced yet. I have it set and I can enter my numbers but I still can't get it to sum up or tell me what's the biggest

take a look at these lines:

code:
for (j=0; j<k; j++){
         x[k] = input.nextInt();
     }

lamentable dustman
Apr 13, 2007

🏆🏆🏆

To add what Mill Town said, from looking at your code snippit I don't think you know what an interface really is. Think of it as a template for functions for your code. When you implement the interface you must include your implementation of those methods in your code.

Look at the jdoc for Comparable ( http://java.sun.com/javase/6/docs/api/java/lang/Comparable.html ). You see how the only method listed is 'int compareTo(T o)'? That means you must must include a method called compareTo that returns an int somewhere in your code.

That is why you are getting the exception.



As for how to implement the function, nobody is going to do your homework but here is a hint. What primitive type makes up a String object? And what other kind of primitive type can it be represented as?

lamentable dustman
Apr 13, 2007

🏆🏆🏆

Mill Town posted:

He doesn't even need to get that dirty, there's a perfectly good compareTo function already in String that he can use to do the dirty work...

yea, i know that of course, but judging by the fact that the instructor wants him to implement the Comparable interface I think he is suppose to roll his own

lamentable dustman
Apr 13, 2007

🏆🏆🏆

As far as I know you can't upload more then one file at a time in JavaScript/HTML. There are AJAX hacks around this but it is still but you still need to open that dialog box for every single file you want uploaded. If you want to do a batch upload (whole folders or whatever) you would need to use a signed applet

lamentable dustman
Apr 13, 2007

🏆🏆🏆

zootm posted:

It's a pity to have to spin up a JVM for such a small use-case but as dvinnen points out it's something that browsers simply can't do.

HTML 5 (lol) should fix that when/if it ever comes out



1337JiveTurkey posted:

For the purposes of making a file uploader, a JNLP-launched applet would work. JNLP allows a slightly larger set of permissions than plain applets such as to create a file open dialog.

https://jdk6.dev.java.net/plugin2/jnlp/

You would still need to sign it but JNLP is cool and a good way to package an applet. Makes using extensions like Java3D a breeze.

lamentable dustman fucked around with this message at 19:13 on May 27, 2009

lamentable dustman
Apr 13, 2007

🏆🏆🏆

almostkorean posted:

Can someone help me understand how java passes in objects into functions? My previous understanding was that everything is passed by value and you can't pass by reference. But I'm working on this program where I'm using a union find data structure (it basically just holds a hashmap) and I'm doing something like this:

code:
UnionFind uf = new UnionFind();
im = connectComponents(width,height,uf);
im = drawImage(width,height,uf);
the uf that is passed into drawImage still holds all the information that was set in connectComponents. Can someone explain what's going on here?

Also, is there a good book or resource for how to make java code fast? I have a decent understanding of how to speed up code in c++ but not with java

simply put, objects are by reference and primitives are by by value

not exactly but close enough

lamentable dustman
Apr 13, 2007

🏆🏆🏆

karuna posted:

I had them in separate files within a package, I just created a new package and is compiling fine now.

My threads don't seem to be running though, the list[] output is 100 zeros.

Shouldn't the run method in PopulateArray be executed for each thread and apply to the list[]? So why isn't the list been populated with random numbers?

take a look at the java order of precedence

lamentable dustman
Apr 13, 2007

🏆🏆🏆

oval office.exe posted:

code:
	//Print array
	for(int i : list){
		System.out.print(list[i] + " ");
		
	}
Take another look at this section.

why? that is fine, it is printing out the same number 50 times because he is passing an int as x and only gets set once per thread

lamentable dustman
Apr 13, 2007

🏆🏆🏆

tef posted:

Any recommended json libraries or such?

I use the one included with DWR but not sure how it compares to anything else, just wanted to make a GeoJSON library for a GIS project and was already using DWR for some AJAX poo poo. It is more geared for building JSON then parsing it though.

(there is a bug in the DWR 3.0 RC1 distribution with JSON, if you go that route build a nightly)

lamentable dustman
Apr 13, 2007

🏆🏆🏆

FunOne posted:

Ok, so I'm building an application that needs to store a time of day (IE, 2:00pm) and it links back to a database so I should use java.util.Date rather than constructing something.

This should be easy, but I'm having a hell of a time with it. How do I just use Date() to represent not July 1, 1978 2:00 pm /EST but 2:00pm in the afternoon.

Should I just use Date as a proxy for a numeric (2+12)*60*60*1000 and do the translate in and out myself?

There is java.sql.Date that is pretty much that. A thin date object for for jdbc that handles the translations.

lamentable dustman
Apr 13, 2007

🏆🏆🏆

look here for the error:

code:
// now that we have verified the input, we can move onto the main program logic
int answer = posInt / base;
while (answer > base)
{
	answer = posInt / base;
}
still won't spit out the right answer but you should be able to figure it out

e: yea, forgot to mention that, your style is horrible. Stay way from bracket-less statements

lamentable dustman fucked around with this message at 17:28 on Jan 25, 2010

lamentable dustman
Apr 13, 2007

🏆🏆🏆

GregNorc posted:

OK so I'm guessing it's something relating to scope? (I was getting 4 for the first test sin ce 45/10 = 4.5 and since the result is an int, it gets truncated to 4, hence the appearance it was working?)

I'm still completely stumped - if I put the answer variable inside the while statement, then I have nothing to test for.

(I tried using a counter, but I always got a value of zero but I'm pretty sure that's not how it should be done anyways)

Edit: But if that's as much help as you can give without telling me the answer, that's ok, I'll risk the crazy TA... I want to try and figure this out on my own if possible.

You are thinking to far ahead. Focus on one error at a time. In the iteration of the code you posted the biggest error in the code is the infinite loop that is causing the "hanging."

Follow the debugging steps rjmccall posted and remember to take things one at a time. Using sys outs or a debugger to pinpoint your problems couldn't hurt either.

lamentable dustman
Apr 13, 2007

🏆🏆🏆

Several ways to do it I guess. I would probably do a nextInt() mod 2. If 0 multiply the random double by -1, else just leave it positive

lamentable dustman
Apr 13, 2007

🏆🏆🏆

Internet Janitor posted:

The simple way is to do ((Math.random() * 2) -1).

yea, this is much better, don't answer in this thread while on the phone...


fake edit: if you think about it this would be less random then Random is already is because the least significant digit will always be even, theoretically (in my mind) at least.

lamentable dustman
Apr 13, 2007

🏆🏆🏆

Azerban posted:

I've got an applet embedded in a webpage that works perfectly on Windows machines, but absolutely nothing shows up on Linux machines, just a blank page. Any idea what could be causing this?

Did you set up Firefox or whatever to use the Java plugin? about :plugins to check

You have to do it manually in linux

lamentable dustman
Apr 13, 2007

🏆🏆🏆

Ferg posted:

So I haven't touched Java since college and I'm diving back in for an Android project that is coming up for work. Being that this is my first time doing legit Java work (and since Java 1.4) can anybody explain to me the proper naming conventions for packages?

Here's my setup: I've got the Android app frontend, a set of classes that define the backend logic (interfacing with a web service), and then a series of utilities. Ballparking it, I figured I'd use this convention:

com.myapp.frontend
com.myapp.backend
com.myapp.utilities

doesn't really matter as long as it meets your companies guidelines. Just make sure it is logically placed

lamentable dustman
Apr 13, 2007

🏆🏆🏆

Azerban posted:

I've got an applet that needs to connect to a MYSQL database that's sitting behind a firewall, maybe two. What is the best, and/or easiest, way to get this done?

e: for reference, the method the network admin. suggested was to use a set of PHP pages to query the database indirectly, but I'm not completely sure how that would work?

Is the server that is serving the applet able to reach the database? If so you can use a servlet to do connect to the database. If they aren't on the same server then the applet has to be signed.

In the applet you would do something similar to:
code:
URL url = new URL(pathToServlet);
URLConnection conn = url.openConnection();
//use HttpsURLConnection for a https connection
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setUseCaches(false);
conn.setRequestProperty("Content-Type","application/x-java-serialized-object");


//TO servlet
OutputStream out = conn.getOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(out);

oos.writeObject(yourObject);
//this would be parameters for your query


//recieve object back from servlet
InputStream instr = conn.getInputStream();
ObjectInputStream inFromServelt = new ObjectInputStream(instr);
Object obj = inFromServlet.readObject();


//clear connection
oos.flush();
oos.close();
out.flush();
out.close();
inFromServlet.close();
instr.close();
Servlet code would look like this;
code:
//from applet
InputStream instr = request.getInputStream();
ObjectInputStream inFromApplet = new ObjectInputStream(instr);
Object obj = inFromApplet.readObject();


//and back to object
response.setContentType("application/x-java-serialized-object");
OutputStream out = response.getOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(out)
oos.writeObject(rtnObject);

//close connection
oos.flush();
oos.close();
out.flush();
out.close();
instr.close();
inFromApplet.close();
probably some errors in there as I had to hand type it from my secure machine


e: A couple things, you probably need a time out in the applet in case the servlet is down as it won't do anything till it gets a response from the servlet. If you need to pass a primitive to the servlet just use one of the wrapper classes. You can also only pass serializable objects back and forth.

lamentable dustman fucked around with this message at 18:17 on Feb 11, 2010

lamentable dustman
Apr 13, 2007

🏆🏆🏆

fletcher posted:

Looking for a way to generate some charts in Java and output to an image. JFree looks pretty sweet, anything else I should check out before diving in?

Used it before, it works really well but the documentation sucks and is built around buying his book. Probably worth it but if you're cheap like me (or rather my company...) use a recompiled to look around the source of his demo app to learn alot

lamentable dustman fucked around with this message at 02:47 on Feb 12, 2010

lamentable dustman
Apr 13, 2007

🏆🏆🏆

i don't think those functions exist in Swing but i could be wrong

lamentable dustman
Apr 13, 2007

🏆🏆🏆

Try printing out in.readObject().getClass().getName() and see what kind of object you actually are getting back

lamentable dustman
Apr 13, 2007

🏆🏆🏆

for what is worth I prefer yatagan's way of writing it but would of had a else so it lines up

:3:

lamentable dustman
Apr 13, 2007

🏆🏆🏆

this is java, can't bitch about being verbose when using java

lamentable dustman
Apr 13, 2007

🏆🏆🏆

Partyworm posted:

Does anyone have any experience with Java certification exams? Are they worth going for or just a waste of time next to practical ability?

Never did them, probably a waste of money and time unless work is paying for it (and time to study for it)

lamentable dustman
Apr 13, 2007

🏆🏆🏆

epswing posted:

If you've never done them, why do you think your opinion that they're a waste of money is worth anything?

I'm not intentionally trying to be a dick, but saying something is a waste of money with zero backup isn't helpful to anyone.

Everyone that I know who has taken them says they are a waste, there was a thread on reddit yesterday were everyone said they were a waste also


i guess they might be padding if you don't have a degree or something



e: also just accepted a new job, during my hunt never saw anything that said "NEED JAVA CERTIFICATION", they all said you need a degree and some experience.

lamentable dustman fucked around with this message at 16:27 on May 5, 2010

lamentable dustman
Apr 13, 2007

🏆🏆🏆

epswing posted:

I have a textfile in my src folder, like jdbc.properties or whitelist.xml, and when I compile it ends up in my bin folder (or more specifically, my war/WEB-INF/classes folder since I'm in tomcat-land). How can I access whitelist.xml?

I thought I'd done this before in a previous life (job), but now I think that was packaging images for JButton icons into a jar, which isn't the same thing. I think I used some System.something call for that, but I can't find it now, and I'm not sure what to search for.

I can't reliably snatch whitelist.xml out of the filesystem, because when I start the tomcat server with tomcat/bin/startup.sh the current working directory is wherever I execute that command, obviously, so I can't get to it relatively, and trying to get to it absolutely is just dumb.

Edit: speling


getClass().getClassLoader().getResourceAsStream("whitelist.xml") should get the file as a stream from the classpath root

lamentable dustman
Apr 13, 2007

🏆🏆🏆

epswing posted:

You could doesn't and take a look at how pircbot does things.

Was going to suggest that, I've played around with it before. Very easy to use, runs its own irc client in main.

lamentable dustman
Apr 13, 2007

🏆🏆🏆

Internet Janitor posted:

WalltBeef: It's probably not the fastest solution, but if all you care about is 2d there's always the AWT Polygon class. Check out the contains() methods.

When working on some GIS stuff I tried that at first, got annoyed and ended up just making my own stuff.

lamentable dustman
Apr 13, 2007

🏆🏆🏆

YouAreCorrect posted:

I have a question.

My current employer wants me to get my Sun Certified Java Programmer certificate. This exam costs a poo poo load of :10bux:, so I really want to pass it the first time.

People in my office say it is very hard, some have failed 3-5 times now. This makes me terrified just at the thought of spending $1000 dollars on a loving certificate.

Can someone point me to a good source to prepare? Thanks.

Never met a recruiter or manager who cared about that cert but if my employer wanted me to get it and expected me to pay for it I would laugh in their face

lamentable dustman
Apr 13, 2007

🏆🏆🏆

Jick Magger posted:

I'm about to graduate with a CS degree, and I can't write a while loop properly.
:saddowns:

no worries, most programmers you meet on the job can't either

lamentable dustman
Apr 13, 2007

🏆🏆🏆

MEAT TREAT posted:

In Netbeans ALT-SHIFT-F will make you happy.

ctrl+shift+f in eclipse

lamentable dustman
Apr 13, 2007

🏆🏆🏆

Jewbert Jewstein posted:

I'm at work right now and I'm working on pulling some data from Google Maps using their geocoding API. When I enter a url (example: http://maps.google.com/maps/api/geocode/xml?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=false) it displays a page written in xml format. How do I extract data from this using java? Specifically I'm looking for the formatted address and the lat/long coordinates.

Any help at all would be much appreciated.

Read it in and use XPath to get what you want out of it.

lamentable dustman
Apr 13, 2007

🏆🏆🏆

Well that depends how you are accessing the data. If you are accessing it inside a program make a URL object out of it and open a stream to it and feed the stream into DocumentBuilder (iirc). If you just want to save what is in your browser go to file and save as.

lamentable dustman
Apr 13, 2007

🏆🏆🏆

hayden. posted:

To clarify, why can't I just let the program crash if there's a problem reading the file rather than being forced to specify what to do with an exception (I know this is retarded)? Is it just because the people designing Java decided to not let us do that?

That is why it is a throw/catch

If you don't give a gently caress you just throw it and are done with it, if you want to try and fix what happened you catch it and handle the error.


Just add "throws Exception" in your function declaration if you give no gently caress and never deal with try/catches

Adbot
ADBOT LOVES YOU

lamentable dustman
Apr 13, 2007

🏆🏆🏆

IMlemon posted:

I want to execute my algorithm in steps so that I can display the results nicely. User presses MoveButton, something is done and something is diplayed. He does that again, again something happens. Repeat. I don't want to completely block the application, user should still be able to select items and click buttons and whatnot.

If I am reading right, I would have the MoveButton iterate a global count then disable the button with whatever function does that. The rest of the function would be a switch statement (or if/else block) that would call the correct step in your algorithm. After the last step reset the count or whatever. One it falls out of the switch renable the button.

Things to keep in mind though is that unless you are threading the algorithm will run on the same thread as the GUI causing the app to act frozen till it is done. SwingWorker is a good class to help handle the threading.

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply