You are viewing our Forum Archives. To view or take place in current topics click here.
Variable Changing help - Java
Posted:

Variable Changing help - JavaPosted:

FR0NTLinealpaca
  • 2 Million
Status: Offline
Joined: Feb 26, 201212Year Member
Posts: 365
Reputation Power: 15
Status: Offline
Joined: Feb 26, 201212Year Member
Posts: 365
Reputation Power: 15
Hi there,

I am currently working on a program where I am reading a mix of words and numbers from a file and storing them in an array.

in part of the program i need to compare 2 numbers that are stored in the array. So i pretty much have this:

Boolean error = false;
        int classSize = words[7];
        int roomSize = words[6];
        if(roomSize < classSize){
         error = true;
        }
        else{
         error = false;
        }
        return error;


but it doesn't work because words[6] and words[7] cannot be converted to int.

Any help would be much appreciated. and maybe a little bit of rep as well
#2. Posted:
Hacz
  • Christmas!
Status: Offline
Joined: Mar 04, 201014Year Member
Posts: 2,891
Reputation Power: 150
Status: Offline
Joined: Mar 04, 201014Year Member
Posts: 2,891
Reputation Power: 150
From the error, I assume "words" is a string array. If that is true, then you need to convert the string to an int using Integer.parseInt(str)

So the new code should be

Boolean error = false;
int classSize = Integer.parseInt(words[7]);
int roomSize = Integer.parseInt(words[6]);
if(roomSize < classSize){
   error = true;
} else {
  error = false;
 }
return error;


but I would create a smaller function like so:


private boolean checkSizes(int roomSize, int classSize) {
    return (roomSize < classSize);
}


And you'd call it like so:

checkSizes(Integer.parseInt(words[6]), Integer.parseInt(words[7])); //returns true/false based on values



Although if your words array are all integers, I'd recommend changing it from a string array to an int array. But it's hard to tell what you're trying to accomplish without seeing the full code.
#3. Posted:
FR0NTLinealpaca
  • V5 Launch
Status: Offline
Joined: Feb 26, 201212Year Member
Posts: 365
Reputation Power: 15
Status: Offline
Joined: Feb 26, 201212Year Member
Posts: 365
Reputation Power: 15
Thank you so much, I believe that should work perfectly in my program. Will be doing some testing tonight to see if it works.

The file I am reading in is mostly words or numbers that I dont need to really touch once they're read in and the numbers that get read into the 6 and 7 positions in the array are the only numbers in the file that need to be used as numbers to calculate something.
#4. Posted:
-Deano
  • Rated Awesome
Status: Offline
Joined: Aug 19, 201014Year Member
Posts: 5,243
Reputation Power: 532
Status: Offline
Joined: Aug 19, 201014Year Member
Posts: 5,243
Reputation Power: 532
The simplest way would be with the individual function Hacz has shown above.

Out of curiosity, what is the rest of the data that is being read in, student names/marks?
#5. Posted:
Clunge
  • TTG Senior
Status: Offline
Joined: Dec 31, 200914Year Member
Posts: 1,095
Reputation Power: 202
Status: Offline
Joined: Dec 31, 200914Year Member
Posts: 1,095
Reputation Power: 202
Thought I would add something to the below that someone suggested:

int classSize = Integer.parseInt(words[7]);
int roomSize = Integer.parseInt(words[6]);


This is correct, but dangerous. If the element at word[7] or words[6] cannot be cast as an integer, your program will crash.

Be sure to add some guardians in to catch the exception and handle the situation elegantly, like below:

try
{
    int classSize = Integer.parseInt(words[7]);
    int roomSize = Integer.parseInt(words[6]);
}

catch (NumberFormatException nfe)
{
    // handle exception here
}


Note on the above, I believe this is a NumberFormatException, but can't remember so be sure to check this.
Jump to:
You are viewing our Forum Archives. To view or take place in current topics click here.