File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Mixed bag

 
Rick Gentry
Ranch Hand
Posts: 63
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm rewriting a C++ UIL test into Java code and I'm coming up with some problems that just won't go away. Since UIL has been so kind as to leave us in the dark this year as to what the tests are like I need to rewrite some of these questions.
1. Java does not have a find() method for strings. Please tell me I'm wrong.
2. The apstring arrays in C++ that allowed you to reference a single character are not in Java. (i.e. string[num]) Is there a way to reference a single character using substring and casting to a character? I only have so much room to write the code in on the test.
3. int comparisons automatically change to Booleans make 'long' int comparisons impossible. What now?
4. int's cannot be put into a standard stack. Is there another option?
Thanks for any help you can give. It's a lot of questions.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
0. What's "UIL" ?
1. find() => String.indexOf()
2. [] => String.charAt()
3. Huh? No idea what you mean here.
4. That's what the wrapper classes like java.lang.Integer are for.

Interestingly enough, the forthcoming JDK 1.5 includes "autoboxing"
which basically does this "wrapping" for you, automagically.
 
Jeff Bosch
Ranch Hand
Posts: 805
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Rick -
I believe the Java String class API documentation will answer your questions. I'll give an overview below. I hope it helps.
1. Java does not have a find() method for strings. Please tell me I'm wrong.

The String class has indexOf(), which will tell you where in the string a character is or a string starts.
2. The apstring arrays in C++ that allowed you to reference a single character are not in Java. (i.e. string[num]) Is there a way to reference a single character using substring and casting to a character? I only have so much room to write the code in on the test.

Yes, the String class has these methods.
3. int comparisons automatically change to Booleans make 'long' int comparisons impossible. What now?

Could you elaborate? I don't understand the question.
4. int's cannot be put into a standard stack. Is there another option?

Yes, you can construct a wrapper class called Integer around that int:

Now you can push and pop this onto one of the standard stacks. Also, writing your own stack for a primitive value (int, char, short, etc.) is not very difficult.
Regards,
Jeff
 
Rick Gentry
Ranch Hand
Posts: 63
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry for taking so long to reply, I've been kind of busy. I've gotten a lot out of all of that, thanks.
First question...UIL is the University Interscholastic League. They're in charge of competitions amongst schools and handing out prizes in the form of scholarships. They do a good job of not really doing much.
I have a few more questions before I'm done translating this test over to java. First one is I meant lenghty int comparisons. I put 'long' because I had a 'short' vocabulary day. For example, 3<7<9 becomes true<9 at runtime in Java and gives an error. Is there any way around this?
Second question is a bit more fun and probably belongs in the intermediate forum, but I'm already here. *shrug*

Nevermind what the code does, I have java.util.* imported and the compiler points at new when I try to initialize Character wrapper. Is there a reason I can't initialize an object here?
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Although 3<7<9 will compile in C++, it won't do the right thing; 3<7 will reduce to 1 for true, and thus the whole expression will reduce to 1<9. Although this is correct, it's only by accident. Compare 10>8>5; this will give you 1 > 5 -- obviously false, and wrong. So you can't chain comparisons together like this in either language. In both languages, the only sensible thing you can do is to write, for example, (3 < 7 && 7 < 9) .
As to the second part: java.util is irrelavant here, as Character is in the java.lang package. The error is because Character needs a constructor argument; you can't construct a Character without specifying what the character is. But you don't need an object here; you're just calling a static method toUpperCase to you can write
output += Character.toUpperCase(month.charAt(x));
As per your invitation, I won't worry about what this code does, since it does nothing except print its argument. I can't help but point out the off-by-one error in the loop index, though: x<month.length()-1 is incorrect; you want x<month.length() (all values up to, but not including, the length).
 
Jason Menard
Sheriff
Posts: 6450
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nevermind what the code does, I have java.util.* imported and the compiler points at new when I try to initialize Character wrapper. Is there a reason I can't initialize an object here?
The Character class does not have a "no argument" constructor, only a constructor that takes a char primitive.
Also, what you want here is a StringBuffer, not a String "output".
Paying no attention to what the code is actually supposed to do, I might make the following changes to your code:

[ September 02, 2003: Message edited by: Jason Menard ]
 
Rick Gentry
Ranch Hand
Posts: 63
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think I'm going about asking this the wrong way. How do you reference and change just one character in a string? i.e. "ABCD" -> "ABLD"
The original question goes like this.
22. What will be the output when the function at right is called with date(“ReAsOn” ?
A. rEaSoN
B. rEaSon
B. ReAsOn
D. REASON
E. REASOn

Oh yeah, and I meant java.lang.*;. I've been compiling all of these questions to make sure they work and I have a long list of imports so I got a little confused.
 
Rick Gentry
Ranch Hand
Posts: 63
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Double posts are evil but I have enough time to fit this in...

I get a bool,int error on the &&. I've tried changing y to a boolean, but that doesn't work. Now what?
 
Anupam Sinha
Ranch Hand
Posts: 1090
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rick
Rick : I get a bool,int error on the &&. I've tried changing y to a boolean, but that doesn't work. Now what?
In java you can not compare ints with booleans and vice versa so
y=(x>0)&&(++z);
This make no sense to the JVM.
 
Rick Gentry
Ranch Hand
Posts: 63
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, I see what's happening, I changed it to y=(x>0)&&(++z=0); as a test. I still have problems with changing one character in a string though.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java strings are immutable. You can't change just one character. You might want to rewrite the example using a char[] instead of a String.
 
Rick Gentry
Ranch Hand
Posts: 63
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, I changed it to a char Array and now getLength() isn't working.

cannot resolve symbol, symbol: variable Array. Isn't that the right way to use a static function?
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The java.lang.reflect.Array class is used for something else altogether. To get the length of a Java array "month" you just say "month.length" .
 
Rick Gentry
Ranch Hand
Posts: 63
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was looking for a function the whole time. Thanks. That answers all of my questions.
Big thanks to everyone who offered help.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic