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
Sereri
Sep 30, 2008

awwwrigami


To be fair, those are from the mid to late 90s and haven't been updated ever since. There's stuff in it like "Avoid lines longer than 80 characters, since they're not handled well by many terminals and tools". :psyduck:

Adbot
ADBOT LOVES YOU

Sereri
Sep 30, 2008

awwwrigami

I am in posted:

You're trying to do it all at once. Break down the problem to smaller pieces, do one piece at a time and print out each piece's internal values to make sure they are what you intended.

OR ||

code:
		int kast[] = new int[6];
		Random generator = new Random();
		int z;

		for(int i = 0; i < 100; i++) {
				z = generator.nextInt(6);
				kast[z]++;
		}
I think you are over-complicating it. Do you actually need to save the numbers? Probably not.

Sereri
Sep 30, 2008

awwwrigami

HKBGUTT posted:

Yeah it is a school asignment, and the asignment says: Dont use If or Switch for some reason. And it also specifies that the random numbers must be added to a array.

use while(), be all :smug:

Sereri
Sep 30, 2008

awwwrigami

StickFigs posted:

I'm trying to use a StringTokenizer and I need it to count anything that isn't a character of the regular alphabet as a delimiter, for example:

"the, thr3e people."

will result in:

the
thr
e
people

Is there an easy way to set up this delimiter besides manually typing in every single possible non-alphabetic character?

Sound like something regular expressions were made for.

Sereri
Sep 30, 2008

awwwrigami

StickFigs posted:

Where would I find a list of regular expressions useful to my situation?

I'd combine this and this

The regex in case should be [a-zA-Z]

/E:

JavaDoc posted:

StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead.

Sereri fucked around with this message at 21:51 on Sep 27, 2010

Sereri
Sep 30, 2008

awwwrigami

hayden. posted:

I'm taking an intro to Java class right now, and I had a question my instructor and Google couldn't answer. We were reading a series of numbers from a text file and we had to use a try/catch. My question is, if we know that there won't be an error reading the file, why does Java require us to use exception handling?

How would you know that. What if the HDD/USB-drive you're reading from suddenly fails? What if your RAM suddenly shits itself? Maybe the OS says gently caress you, get your hands of that.

/E: For further clarity: The exceptions are not for the handling of the numbers but for the action of reading the file they're in. Lots of stuff can happen

Sereri fucked around with this message at 16:38 on Sep 30, 2010

Sereri
Sep 30, 2008

awwwrigami

Flobbster posted:

My beef with Javadoc is that their HTML generation hasn't changed since probably Java 1.0, and it shows. The HTML that gets generated is a horrible mess and it's impossible to style well.

Sure, there are other tools that probably do a better job, but it would be nice if Sun Oracle would bring the tool into the 21st century with better semantic HTML and classes on all the elements for easy CSS application.

Yeah that won't happen.

On the other hand the JavaDoc compilation usually happens in the IDE so really Eclipse/Netbeans are to blame.

Sereri
Sep 30, 2008

awwwrigami

Aleksei Vasiliev posted:

http://validator.w3.org/check?uri=h...oup=0&verbose=1
Errors found while checking this document as HTML 4.01 Transitional!
77 Errors, 2 warning(s)

You'd think they would base their HTML on standards instead of "just generate some random bullshit and hope it works".

Edit: How is the IDE to blame?
code:
<HEAD>
<!-- Generated by javadoc (build 1.6.0_21) on Mon Jun 28 16:43:23 PDT 2010 -->

To be fair, if you look at it closely the validator is full of poo poo.

And I kinda slipped up on the JavaDoc generation. Of course Sun Oracle's JavaDoc is generated by some Sun program. Nevertheless, the IDEs would easily be able to compile a correctly validating project JavaDoc.

Sereri
Sep 30, 2008

awwwrigami

Aleksei Vasiliev posted:

The official validator by the group that created what it is validating is full of poo poo? How so?

Almost all the errors are "tag closed but wasn't open in the first place". If you look at the html you can clearly see that it's wrong, the tags are even in the correct place.

Sereri
Sep 30, 2008

awwwrigami

MEAT TREAT posted:

Javascript isn't Java :(

You want to ask in the Web Design/Development Small Questions

I was thinking that he meant that too at first but I think the javascript is in an embedded browser within a java program.

Sereri
Sep 30, 2008

awwwrigami

fletcher posted:

If I do something with an array, and then I'm done with it and I need to do something else with an array, is it better to reuse the same pointer? How does it effect the GC? How about readability?

You shouldn't reuse the array for the following reasons:

a) Memory is cheap. Unless you try to run your program on a machine with 64MB ram you shouldn't have any problems with memory.

b) Variables should have a purpose, should be named accordingly and should only be used for this purpose. It greatly improves readability.

c) The GC mostly does not give a poo poo .

Sereri
Sep 30, 2008

awwwrigami

magicalblender posted:

Is it possible to make a decorator class that doesn't call super() on its superclass?

The way Java works with classes is to call super() all the way up to Object. Even stand-alone classes are basically '(public) class Yadda extends Object'. You can write the super() yourself or not but it will always be there. Sorry dude.

Sereri
Sep 30, 2008

awwwrigami

Keyboard Kid posted:

:words:

code:
        for( String word : al1 )
        {
            if( !al2.contains(word.toUpperCase))
            al2.add(word.toUpperCase);
        }
Like that ?

Sereri
Sep 30, 2008

awwwrigami

Use a string, parse it into an integer, increase it by one, convert it back into a String ( = ""+ number;) check length and fill the missing digits with zeros.

Sereri
Sep 30, 2008

awwwrigami

Just quickly looking over this I'd say it's because java arrays begin at 0 and your 'count' starts at 1. Go from there.

Sereri
Sep 30, 2008

awwwrigami

^^^^^ true, still not really what it's supposed to do I guess.

slyo posted:

I am confused about this method:

code:
    public boolean lineIsCommand(String s) {
        if (!s.startsWith("/"));
        return false;
    }
Won't this always return false?

Yes it does. The first semicolon is misplaced, an else -> return true; is also missing.

Sereri
Sep 30, 2008

awwwrigami

Harold Ramis Drugs posted:

This thread is amazing, thanks for all the help.

I have some extra credit available on one of my assignments for coding the Sieve of Eratosthenes for a given positive integer (taken from Scanner). The kicker on this assignment is that it has to be done without using any arrays. I don't even know where or how to do this without arrays, so I'm starting to think this might have been a typo on the assignment sheet. (there were other parts of the assignment where using arrays would have drastically simplified things)

Is it even possible to code this without arrays?

If arrays are off the table I guess lists and sets are too so maybe a string with comma separated integers.Use stringbuilder to write all the numbers one after another then remove them from the string if you match them. That's pretty ugly though, not sure why you'd want people to code that without arrays.

Sereri
Sep 30, 2008

awwwrigami

I am in posted:

Since that sounds like a homework problem and the most important point was already made (that strings are not very good data structures for this problem), I'm going to nitpick a bit about case-insensitivity.

German is a wonderful language. It was only 4 years ago that Unicode added ẞ as the capital ß, but according to the German Wikipedia the Council for German orthography says that sharp s doesn't have a majuscule at all.

So what should the result of the comparison be right now and, if in a few years the Council changes their mind and the Java library is updated, should the result of the comparison change?

To be fair, a capital ß has never existed and might as well never will. The only use is in words written entirely in capitals and even then you either write ß or SS.

Sereri
Sep 30, 2008

awwwrigami

DholmbladRU posted:

Here I am reading in a xml tag and adding it to a java object field. However the tag name 'location=' comes along with it. How can I remove this from the incoming string without taking up alot of resources?

So you have <abc location='xyz'> and want to remove the location='xyz' ? Why not simply remove the attribute from the object?

Sereri
Sep 30, 2008

awwwrigami



Off to a good start.

Sereri
Sep 30, 2008

awwwrigami


The problem is that you return the calculated hex instead of printing it. Also your method type is wrong, it's not supposed to return something in the first place.

Finally two things

Java code:
int counter = 1; // initialize counter to zero
:laugh:

And maybe if your professor is a dick he will cause an exception by entering 0 for the values per line var.

Sereri
Sep 30, 2008

awwwrigami

Joda posted:

In the arguments passed to the sort() method you've written, you're passing a double array, which you've written as sort(double array[]). Why this notation doesn't give you a compile-time error, I do not know, but you always put the brackets after the type declaration (e.g. int[], double[] etc.), as opposed to the variable name, when you're declaring an array.
It's because
type array[]
and
type[] array
are both valid ways to declare arrays in Java.

The second one is just preferred by most people I guess.

Sereri
Sep 30, 2008

awwwrigami

DholmbladRU posted:

yeah my bad, hard to see it had anything to do with java.. Are you telling me you guys haven't memorized all the available java packages syntax?...


Anyways if anyone is familiar with the jsoup package and wants to make some recommendations id appreciate it. Currently I am connecting to some website and pulling down data, eventually I will automate this so no one will be watching. At the moment I am not handling any connection, cookies, or referrer(no idea what this is) from the jsoup package. I am simply performing,

Document docRem = Jsoup.connect("http://www.google.com/stuff" + var).timeout(10*10000).get();

Is there a better way to handle this connection? Should I 'ping' it first with connect().execute() then check for a status 200?

Awful uses jSoup to parse the forum's html code but we handle all the network stuff (including getting the html) with the apache httpConnection library. On the other hand I have no ideas whether it's better for your case than just using jSoup.

Sereri
Sep 30, 2008

awwwrigami

Doctor w-rw-rw- posted:

Tip - you should take a look at Volley for making http requests. For one, it properly manages swapping between apache on the old and httpurlconnection on the new OSes.

It's on the list, like so many things. I guess I'll create a ticket for it.

Sereri
Sep 30, 2008

awwwrigami

I'm a bit stumped. I'm trying to turn an image I have as a Bitmap into a base64 String. The code I have works for small images but as soon as they get bigger than :psyduck: for example, this happens:



The base64 I get (just as the image I posted) comes to exactly 4096 bytes and I doubt that's a coincidence.

The code in question:

Java code:
public String onResponse(Bitmap response) {
	ByteArrayOutputStream baos = new ByteArrayOutputStream();
	response.compress(Bitmap.CompressFormat.PNG, 100, baos);
	byte[] buffer = baos.toByteArray();
	String base64 = Base64.encodeToString(buffer, Base64.DEFAULT);
	return base64;
}
I've googled around but the only thing I find are cases on SO.com with basically the exact same code where it (apparently) works.

This is on Android but that shouldn't really make a difference.

Adbot
ADBOT LOVES YOU

Sereri
Sep 30, 2008

awwwrigami

Imazul posted:

Haven't done this in a long time but it's your ByteArrayOutputStream that is only reading the first 4096 bytes. You need to read it in chunks.

Doctor w-rw-rw- posted:

To offer additional advice, if I were to fix it, I might wrap the BAOS (abbreviated b/c I'm on a tablet) with a Base64OutputStream and then supply that to the compress method. Your BAOS now has a compressed, base64-encoded image. Then do what you will to read it out.

Android makes a difference because it supplies the Base64OutputStream class.

But yeah, make sure you pay attention to how much data is actually being written/read.

Yeah that worked, thanks guys.

It now looks like that:
Java code:
public void onResponse(Bitmap response) {
	ByteArrayOutputStream baos = new ByteArrayOutputStream();
	Base64OutputStream b64os = new Base64OutputStream(baos, Base64.DEFAULT);
	return baos.toString();
}
Now to figure out why it I still get 4kb Strings when I store it and retrieve it from my HashMap :cripes:

before: after:

Sereri fucked around with this message at 22:46 on Jul 10, 2013

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