File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Question about Arrays Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Question about Arrays" Watch "Question about Arrays" New topic
Author

Question about Arrays

jon ladd
Ranch Hand

Joined: Feb 11, 2003
Posts: 53
I just read the topic a few down about arrays, they confuse me some what. I have nums as a reference for my array of a byte value. I then want to know how many negative numbers are within my array reference by nums display my results in the form of "nn negative numbers". Ok I understand that i can use a byte as my array value.
So this kind of right

byte nums
myarray = int nums % "2";
system.out.println(nn negative numbers(+ nums));
Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2841
    
  11

Jon,
I guess I am somewhat confused too. I'm not sure exactly what you're trying to do. I'll try to give some suggestions anyway. That should get you past "somewhat" to "totally" confused. It's my specialty.
bytes cannot be negative. They range from 0 - 255.
If you want to declare an array of bytes you need to have brackets: byte [] num;
It also would be a good idea to put some data in the array:
byte [] num = {2, 4, 3, 127, 18 };
It should be num % 2, not num % "2".
num % 2 == 0 as an expression would be true if num is even, not if it's negative.
That's probably enough to get you going. You want to start slow with programming. Arrays are a difficult concept. You should make sure you understand the earlier principles first. It's frustrating, but worth it in the long run, to give yourself a solid foundation on which to build. Good luck!
jon ladd
Ranch Hand

Joined: Feb 11, 2003
Posts: 53
Here let me reword the question I am sorry was not very clea. I assume nums is the reference for an array of byte values. Then I want to code a statement needed to determine how many negative numbers are within the array referenced by nums and display in the result in form "nn negative numbers".
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Originally posted by Greg Charles:
bytes cannot be negative. They range from 0 - 255.
bytes can be negative. The range of bytes is from -128 to 127. Java does not have unsigned integer types (unless you consider char an integer type).


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451

bytes can be negative. The range of bytes is from -128 to 127.

That fact comes thru load and clear the first time you cast a byte like 0xA2 to an int and try to start shifting bits and wonder what the hell is going on.
Michael Morris


Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9044
    
  10
Originally posted by jon ladd:
I assume nums is the reference for an array of byte values. Then I want to code a statement needed to determine how many negative numbers are within the array referenced by nums and display in the result in form "nn negative numbers".

I suggest you use a for loop to count the number of negative elements in the array.


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2841
    
  11

Oops, you're all right. Bytes can be negative. I must have a byte type from some C++ library stuck in my head.
Sorry, about that Jon. The other things I said were right though. In order to do this program, you will have to first understand arrays, loops, if statements, conditional operators (in particular the "less than" operator), and of course how to make a working Java program in the first place. Why don't you post some sample code, and we'll give you pointers. (Now watch, somebody will jump on me saying there are no pointers in Java )
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Question about Arrays
 
Similar Threads
An object to hold several arrays
Signed value in Byte array to int problem
(re)assigning arrays?
array of random numbers
locking objects that only read fields