Homework Question: Java

  • In the class I am taking, we are supposed to use a [ System.in.read() ] to imput a value to be used as a subscript of an array. We haven’t gotten to any of the string methods yet. My question is, how the f*** do you specify a variable type in Java? I keep getting a “subscript out of range” error, because the key that is pressed is the “3” key and the unicode value is passed to the array that only has 9 members. The textbook offers no hep at all, and the implication is that it it possible using only this method.
  • I did another quick test to try to understand what was happening: I input two values into two different variables, (the values were 2 and 3 ) and then displayed them each out using the System.out.print(), and each was the value it was suposed to be (IE,- “2” and “3”) but when I tried to add them together I get 101 or some shit. I don’t get this at all. I also noticed that I can declare ANY of the variables as ANY type, and the System.in.read() as ANY cast, and the result is still the (incorrect) same answer. All I can say is, what the fuck? Why do you specify what type any variable is if it will make its own cast assumptions anyway? The only way it works is to subtract 48 from each variable first, but I figured that out on my own, the book doesn’t say anything about it. I wanna add 2 and 3 and get 5, goddammit! - MC
  1. Perhaps if you posted the code, we might be able to understand what you’re doing, so we can see what you’re doing wrong.

  2. Have you talked to a TA? That is what they’re paid to do.

Uh…subtract 48 and you get the correct number? That means that you’re getting the ascii value of the number; or, rather, that you’ve got a character representation of the number ( How do I know that? Every one knows that ).

In other words, you want 5 ( the int ), and instead you’ve got ‘5’, the character.

Its java. It is easy. System.in takes a byte array. Use the byte array to construct a string ( String newString = String.valueOf( byteArray ) ought to work ). Use this string to create an integer:

Integer asANumber = new Integer.valueOf( newString ).

In fact, you can skip the middle bit.

Integer asANumber = new Integer.valueOf( String.valueOf( byteArray ))

Should create an object of type Integer from th byte array you passed to read().

In the old days, you would have simply deconstructed the byte array by subtracting 48, but you aren’t allowed to do that any more ( and should not have, then ). There is probably a faster way to accomplish this, but the steps I out lined above will work.

Yup. The valueOf() methods are some of the most useful for mixing and matching types–I’ll use them constantly, especially for converting to strings:

String myString = java.lang.String.valueOf(double d)

Java is a true-to-life object oriented language, so even the primitive types (int, double, float, boolean, etc.) have “wrapper” classes so that they can be handled as objects. Strings are not a primitive, so the need to be “instantiated” just like objects. The basic format, since you asked, is:

ObjectType newObject = new ObjectType();
newObject = x;

BTW, Strings are a read-only affair. Use StringBuffer to read and write. Hope this helps

I figured valueOf was probably a static method, so you could use String.valueOf( byte ) with out instantiating the String, but I was too lazy to test it to be sure.

As it turns out, String has a constructor that handles this, so you can skip the first valueOf -

Integer asNumber = new Integer( String( asByteArray ) );
( instantiate a String with the value of the byte array, then use that String to instantiate an Integer, using the (String) constructor of Integer. Which should cause anyone who maintains your code to hunt you down and shot you, but it does what you asked for ).

I don’t think you can use the int primative in this instance. Can one override operators in Java?

Agreed that valueOf() is a cool access method. Saves a lot of grief.

      • Thanks all. I did recognize that it gets the unicode value, I just couldn’t find any way to use unicode to do math or make a select statement work right. (And I tend to automatically struggle with it for a while, because if the textbook says that’s the way it should work, I tend to assume the book is right and I’m doing something wrong.)
  • There’s several problems here: the teacher sucks (he’s a part-timer who can’t answer anything off the top of his head, he always has to look it up), the string methods are in the next section of the same chapter (which implies that they aren’t needed for this program), and the class is once a week, and every class the assignments from the last class are due (tomorrow). -And I already looked, there’s only one course in Java this semester at this college, and this monkey is teaching it.

Well, it would take me a long, long time to get to the point where I’d be comfortable teaching anybody about Java without constantly having to look things up. I understand what it’s like in the beginning: most of the tutorials I’ve read have had to introduce topics (like Strings) prematurely in order to demonstrate code (“Hello, world!” gets old really quick). I ended up scratching my head and flipping to later chapters all too often. I don’t think I really started getting the hang of it until I actually had to use it, so you’re doing the right thing. Nothing like a little tete a tete with the compiler when it comes to programming. Good luck!

The previous posters covered it pretty well. I also wanted to point out the Integer.parseInt(String) method which returns an int rather than an Integer if that’s what you want.

Anyway, when you do need to look something up, the first place to go should probably be the java API. The following is for Java 1.3. 1.1 or 1.2 are easy to find as well.

http://java.sun.com/j2se/1.3/docs/api/index.html

I had to learn how to be competent with Java over the summer, my prevous experience being with C++. Since your professor doesn’t seem like a good resource of information, I’ll reccomend the book that was my Bible for some time: O’Reilly’s Learning Java. That will probably cover most stuff you’ll end up doing in your course. It does have a lot of stuff on how to interact with web pages which might not pertain to what you’re doing, but you never know.

I never used O’Reilly’s Java in a Nutshell but I can guarentee it’s a good resource. It may be more than you’ll need though.

In the absence of a good teacher, always have a good reference book. They’re invaluable.

How do you find out what anything does? For example, our textbook only shows-
variableX = (optional cast)System.in.read();
but the MS J++ book usually uses the form-
System.in.read(variableX);
which our textbook never mentions.
In the J++ IDE, where do I find out exactly what System.in.read() does, and how it can be used? Is there anywhere that shows examples of all, and what exactly they do? - MC

Keep in mind that J++ is different from pure Java. I don’t know how it’s different, I just know that it is.

That said, you have to remember that methods can be overloaded. This means that there can be different methods with the same name, but they have different parameter types.

My guess would be that System.in.read() returns whatever it read in, and chances are it’s just returning an Object. This would likely be why you would have to cast it to the type of variableX, but you don’t need to.

The way used in your J++ book, System.in.read(variableX) likely takes the variable you’ve given it, and assigns what was read in to that variable. Same result as above, differnt way of saying it. (I am not sure about all this, but from what I know, I think this is how it works.)

I’ve never used MS’ J++, I used Symantec’s Visual Cafe, so I can’t tell you where to look for the API in J++. Bumble around the menus until you find something that looks like it will give the class hierarchies and descriptions.