|
Can anyone explain to me why an individual 'scanner' has trouble moving from scanning doubles and strings? This, for example, did not work. code:
Newf fucked around with this message at 22:36 on May 24, 2011 |
# ¿ May 24, 2011 22:34 |
|
|
# ¿ Apr 25, 2024 16:49 |
|
I'm trying to write a class of objects that has ArrayLists as fields. It isn't going so well.code:
|
# ¿ Jul 22, 2011 03:10 |
|
Well I've done that and it works, but I'm completely confused. Isn't the "thing" that you put in theses <> doodads the 'type' of object that the arraylist will store/retrieve? aren't 'double's a thing in java, while 'Double's aren't a thing? Anyway thanks If I wanted an ArrayList of integers, would it be ArrayList<Integer> or ...?
|
# ¿ Jul 22, 2011 03:23 |
|
Jeez, be more helpful guys. Thanks both.
|
# ¿ Jul 22, 2011 03:36 |
|
I was laying on the floor just now thinking about the line of text that I'd been instructed to enter at the beginning of every program: public static void main(String[] args) THIS IS BECAUSE the main method (is the method that runs without getting called?) (potentially) takes any number of string arguments, which are in fact (this is my guess) command line arguments?
|
# ¿ Jul 22, 2011 22:45 |
|
Internet Janitor posted:This may have helped, or it may have made you more confused. A little of both. Thanks very much
|
# ¿ Jul 23, 2011 00:22 |
|
OK, so more trouble from the resident java noob. I'm trying to make a program that uses scanner to take a positive integer as keyboard input, but instead of crashing at faulty input (like a string, for example) it asks for the input again. When I run the following: code:
code:
code:
|
# ¿ Jul 24, 2011 18:21 |
|
I certainly haven't, but wouldn't it be covered by importing java.util.Scanner? How else would the first example program there have thrown the error? e: well this seems unanimous, and thank yous, it worked, but my-oh-my I have no idea how java works at all.
|
# ¿ Jul 24, 2011 18:25 |
|
I don't think I know how to use BufferedReader.code:
|
# ¿ Jul 24, 2011 23:13 |
|
I eventually got my original code to work. The problem was that in my main method header I didn't have "throws IOException". I have no idea what that does or why it needs to be there. Why would it be a bad idea for a method like that to call itself recursively? It just works until it's been given a legitimate input file.
|
# ¿ Jul 25, 2011 17:52 |
|
from http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/Integer.html: toString() - Returns a String object representing this Integer's value. toString(int i) - Returns a String object representing the specified integer. The Integer class has a no arg toString method. But doing something like this code:
code:
What am I misunderstanding about the no-arg method?
|
# ¿ Dec 9, 2011 03:49 |
|
I'm not sure I understand exactly what's going on with 'int' not being the same as 'Integer', but thanks for the discussion. Things to think about! Another question, this one purely debugging: code:
Any suggestions on how I'm misreading this?
|
# ¿ Dec 9, 2011 17:23 |
|
I see! Thanks.
|
# ¿ Dec 9, 2011 18:08 |
|
I've been reading the Newbie Interview thread and a while back someone suggested:code:
code:
So I wanted to rewrite my print function using the printf command and HOLY LORD I don't know how printf works. I could get around this using another value to keep track of the number of digits in count, and then a loop to print the appropriate number of spaces, but printf seems pretty powerful and useful so I'd like to learn how it works! tl;dr Given an int 'digits', how can I use printf to print the int 'count' inside a 'space' that's 'digits' long? ex: code:
|
# ¿ Dec 16, 2011 15:52 |
|
http://docs.oracle.com/javase/1.4.2/docs/api/javax/swing/JOptionPane.html May be what you're looking for. ex: code:
Newf fucked around with this message at 16:19 on Dec 16, 2011 |
# ¿ Dec 16, 2011 16:02 |
|
Internet Janitor posted:Newf: Have you read the documentation for the Formatter? It contains some examples of doing similar things. Yeah, that's where I'm at. So %[argument_index$][flags][width][.precision]conversion is the format for a number in a 'formatted string'. So given that (most) of those arguments are optional, I'm not sure how to make an entry where it recognizes that the argument I'm inputting is for the width option? I had tried something like: printf("%digits", count); which prints (if count=7 for example) "7igits". I believe I see now that this is printing count as a decimal integer, and then going on to print "igits", but I guess I'm restuck now.
|
# ¿ Dec 16, 2011 16:17 |
|
baquerd posted:Well it's right there in the specification. OK, so part of my problem was that I hadn't payed enough attention to the format there to see that inputs aren't ambiguous. For example, flags are specified with a character set that's disjoint from the ints that specify ints, and an int without being followed by a $ will be interpreted as width instead of as an index. The thing is that I can't put that 5 in there for width. The width was meant to correspond to a variable "digits", which is input-dependent. I got it now though: printf("%"+digits+"d", count); Thanks for the pointers. Newf fucked around with this message at 16:38 on Dec 16, 2011 |
# ¿ Dec 16, 2011 16:32 |
|
I'm gonna apologize in advance because I expect to be making GBS threads this thread up a lot over the next few days. I'm trying to use my Christmas break to get as much programming in as possible, and my biggest current goals are to build on my understanding of how OO programming and Java in particular work, and also to get a better understanding of some basic data structures and associated algos by writing them myself. I figure I can do these things in combination. My plan is to start from this Node class: code:
By the time I go back to class in January, I'd like to make my own 'package' that I can use in the future for my own academic type things, which includes all of my homemade classes. I guess my only question for the moment is whether what I'm doing is totally misguided, or if I'm going about it in a fundamentally stupid way. Thoughts?
|
# ¿ Dec 20, 2011 16:00 |
|
Yeah, to clarify, I understand and expect that pretty well all of this will exist entirely as an exercise, but I mean to say that I'd like to continue on updating and refining my own library of code as I learn new things. I'm in trouble already though, and it's with a dumb thing that's going to betray my total lack of understanding: code:
super.Node(e); --> cannot find symbol method Node(E) and some other stupider things. Is there some special way to use a constructor with generics? Or some particularly good reading on how to work generics? Should I, before getting into any of this, make myself understand Interfaces and use them throughout my adventures?
|
# ¿ Dec 20, 2011 18:20 |
|
Aleksei Vasiliev posted:public class SLinkNode<E> extends Node<E>, I think Correcto. Thanks.
|
# ¿ Dec 20, 2011 18:35 |
|
Understand that I am a Java/programming child, and my answers to these and any questions are almost certainly not the best answers available. That said, I think they can 'work'.Dr. Gitmo Moneyson posted:1.) How do I create a new .txt file in Java? 1), 2), and 4) can all be accomplished with the FileWriter and PrintWriter objects. To write, for example, to a file whose name is input by a user, you can do something like: code:
code:
I don't really have any immediate ideas about questions 3) and 5). Newf fucked around with this message at 16:17 on Dec 21, 2011 |
# ¿ Dec 21, 2011 14:37 |
|
I'm trying to write some basic server/client functionality using sockets, but I'm not able to get any communication to between the server and client to occur until the socket between them is closed. http://pastebin.com/89T5tfkY is my server, and http://pastebin.com/PC9pVTq6 is my client. The client does print "Hello World", but only when the line sock.close() is reached at the server side, so the info transfer only happens at the point of disconnection, as far as I can see. I need my client to receive information from the server and then be able to return requested information to the server, though. (You know, in general). Any pointers?
|
# ¿ Jan 19, 2012 18:17 |
|
I'm building a little something for school where a system has a number of different types of accounts (eg: admins, mods, shmoes) and my wish is to create a default 'Account' class and then to extend it to the various other classes, but I think I may be cornering myself by being too eager to use inheritance. Question: If my Account class implements serializable so that I can write Account files, would my read/write methods in the parent class be properly inherited by the subclasses? ie, reading an Account object from its file in the parent method would require typing the object as an Account - so would typing a Shmoe as an Account destroy his attributes which are distinct to Shmoes? eg, from the 'Account' class, code:
|
# ¿ Feb 3, 2012 21:21 |
|
Luminous posted:You may want to consider an account as, well, just an account that has f.ex, a username and a role or a list of roles, where a role is an admin, a mod, or a schmoe (use inheritance here). Yes, I think you're right. Thanks.
|
# ¿ Feb 6, 2012 15:40 |
|
It's likely that this is under my nose and I'm an idiot, but am I able to download the whole java API documentation so that I can reference it sans internet? In a simple way? (javadocs.zip?) I mean this website: http://docs.oracle.com/javase/6/docs/api/java/util/ArrayList.html
|
# ¿ Feb 29, 2012 14:22 |
|
Thanks!
|
# ¿ Feb 29, 2012 14:53 |
|
I very nearly posted the stupidest question in the history of the thread but I've just figured it out. Now I have to post it anyway because I've had a good laugh at the situation and I'm on a real streak of INCREDIBLY DUMB programming errors (an hour where corresponding files weren't in the same directory, an hour shouting at a program that wouldn't run because it didn't have a main method, and now this): Me, last night posted:Why won't eclipse auto-import this? Even trying the imports manually I can't get it to work??? Does day-to-day programming ever get over this kind of boneheadedness? I'm sure that I spend half of my 'coding time' on these types of errors.
|
# ¿ Mar 9, 2012 14:02 |
|
TRex EaterofCars posted:Usually the solution is to stop using eclipse. But I just started last night! AHHHHHH!
|
# ¿ Mar 9, 2012 16:57 |
|
I am in posted:The one marked with red Oh god thanks for that.
|
# ¿ Mar 9, 2012 19:28 |
|
I'm still lovely at java/programming, but I think I've nailed down a question concerning a particular situation which, if clarified, might make me a little less lovely. I'm trying to learn about file system management in java so I've been writing a small test class to experiment with the java.io.File object's methods. This is how far I've gotten: code:
code:
Newf fucked around with this message at 18:52 on Mar 22, 2012 |
# ¿ Mar 22, 2012 16:58 |
|
I feel less lovely already! Thanks!
|
# ¿ Mar 22, 2012 18:55 |
|
I need some help with java sockets and BufferedReaders/BufferedWriters. I'm trying to set up a server/client so that after connecting, input entered by the client will be displayed server side, until the client inputs a null string. After the null input, the roles reverse, until the server inputs a null string, at which point the connection terminates and the server readies itself for another client. My problem is getting my BufferedWriter to 'flush' the user's input. Upon establishing a connection, both the Server and Client create BufferedReader/BufferedWriter as follows: code:
code:
At this point the server is in its own loop: code:
My first attempt didn't have the line marked *, and the result was that the server never ever printed anything until the client process was violently killed, at which point it prints everything that the client has input. When I include the line *, the server will print the first input from the client, but when the client makes their second input, an error is returned since clientOut is destroyed. When I include the construction of clientOut inside the while loop (this seems excessive to me but may, for all I know, be standard practice), the server again successfully prints the first input from the client, but the second client input produces an error since (much to my surprise) clientOut.close() seems to close clientSocket (since clientOut invokes clientSocket in its construction I guess?) Really could use some pointers. Thanks.
|
# ¿ Apr 18, 2012 15:56 |
|
Ahhh, drat, thanks. I essentially replaced clientOut.write( line ) with clientOut.write( line + "\n" ) (probably poor form) and things smoothed out from there. Thanks again.
|
# ¿ Apr 18, 2012 17:53 |
|
How much have you played around with this? Try some smaller values for your input 'k' and see what you get. Something likecode:
Newf fucked around with this message at 20:28 on Sep 18, 2012 |
# ¿ Sep 18, 2012 20:24 |
|
This is less a java question than an Eclipse question. How do I get the Eclipse intellisense to find/read/display the documentation on regular java language features? For example, mousing over the 'println' token in System.out.println("Hi"); producesquote:Open Declaration void java.io.PrintStream.println(String arg0) I'm failing at google with this problem (getting instructions on how to use Eclipse's javadoc generation instead of what I want), so I figured I'd just ask some humans who are sure to know the answer.
|
# ¿ Apr 27, 2014 16:02 |
|
rhag posted:Shift+Left Click on println and click Attach Source on the page that appears. Then select src.zip from the JDK home (you have a JDK installed, right?, not only a jre). Thanks!
|
# ¿ Apr 27, 2014 19:08 |
|
return aCollection.contains(anObject) && aCollection.size() == 1; ?
Newf fucked around with this message at 14:43 on Jul 2, 2014 |
# ¿ Jul 2, 2014 14:40 |
|
FateFree posted:Its funny but I can't help look at this and say the size check should be first for performance reasons to short out the contains. Do you guys consider stuff like this to be premature optimization? In my view, as long as you aren't doing 'extra' work under the pretense of optimization, then I would just consider swapping to be the objectively correct action. All work is extra work! As for whether this is premature optimization - it's a little bit contextual. Is there a possibility in this system of this collection being terribly large? It's likely that even the five seconds of typing to swap the order of those checks is longer than the cumulative performance hit of the inefficiency over the entire lifetime of the system. In general, I tend to build first and optimize when I'm given some indication that there would be a derived benefit.
|
# ¿ Jul 3, 2014 13:09 |
|
I'm looking to run javadoc on this file: https://github.com/ankidroid/Anki-Android/blob/master/api/src/main/java/com/ichi2/anki/FlashCardsContract.java I haven't used java in five years or more so I'm not sure whether I can run something like javadoc.exe myFile.java and have it spit out an myFile.html. The windows examples link on the Javadoc FAQ is a 404. I'm not even sure how to tell if I have the java sdk installed on my machine. java.exe, javaw.exe, and javaws.exe are available in my command prompt, but javac isn't. Any directions to give? (A generous person could also feel free to generate the doc and pastebin it or something if it's a one-step procedure).
|
# ¿ Sep 1, 2018 17:18 |
|
|
# ¿ Apr 25, 2024 16:49 |
|
I figured it out. Noticing the long-unclicked Eclipse icon on my taskbar, I guessed that some version of the java sdk must exist on the machine. I had earlier tried using windows explorer and the start menu to find javac.exe, but they failed. Of course, it was in the first place I looked (program files (x86)/java/sdk/bin). Eclipse's export javadoc worked out for me. Such pangs of nostalgia at running Eclipse...
|
# ¿ Sep 1, 2018 18:08 |