• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Question about Arrays

 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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));
 
Sheriff
Posts: 3036
12
Mac IntelliJ IDE Python VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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".
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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).
 
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Greg Charles
Sheriff
Posts: 3036
12
Mac IntelliJ IDE Python VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 )
 
Are you okay? You look a little big. Maybe this tiny ad will help:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic