If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register or Login
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

How to insert the value inside an array's cell into a char variable?

I have this program in Java to read a database written in a .txt format (file name is "info.txt").

It has the values record number, name, age, state, and salary class written in it. I've named the array to read that database as "ven[]". The program will load that text-file database into that array to display on the screen when it is run. When it reaches the array cell that has the salary class value, it displays on the screen the corresponding amount. Array cell ven[4] contains a single entry that's just one character long.(ven[0] is the where the record number or "R11-1000" is stored)

That character corresponds to the value assigned to it. That is, if the value in it is "1", it corresponds to $200 per hour. Hence, on the screen the line, "Salary per Hour" will have the amount, $200 dollars next to it. I read that switch statements can only allow integers or characters as values in it. So, I've been trying to assign that array cell in question into a char-declared variable - as you can see in the photo of the few lines before reaching the switch statement. Unfortunately, I get error messages when I attempt to do so. (the compiler says they're incompatible data types). The error message, by the way, says "error: incompatible types - readDB.java - line 37"...

I intend to use the entry inside ven[4] as a value for the switch statement in the code. I'm not quite a beginner but I'm not expert on Java either.
I have no idea how to go around this problem - any idea how? That is, how to transfer an array's value into

The code I've written is down below...(line 35 is where the error is...)

Re: How to insert the value inside an array's cell into a char variable?

If in case I get the new JRE, would I still have to use the code lines you suggested? Or is there some way of getting
the value of that specific array cell to be a switch value with a new JRE installed?

Re: How to insert the value inside an array's cell into a char variable?

Originally Posted by PC_flea

I read that switch statements can only allow integers or characters as values in it. So, I've been trying to assign that array cell in question into a char-declared variable -

Well, a char array isn't a char. You'll have to single out a char from the array. If the char you want to switch on is firts in the array it would look like this,

Code:

switch(cArray[0]) // first char
{
case '1': // '1' is a char

Another option is to turn the String into an int (assuming the String holds a number). Then you switch over that int as in the current code. You can use the parseInt() method of the Integer class, like

Code:

int mynewint = Integer.parseInt(mynewstring);

By the way why are you appending to result? Since result is declared outside the loop it means ven[4] values will be added on to each other in each iteration of the loop and accumulate. Is that what you want?

Re: How to insert the value inside an array's cell into a char variable?

From member, nuzzle -

By the way why are you appending to result? Since result is declared outside the loop it means ven[4] values will be added on to each other in each iteration of the loop and accumulate. Is that what you want?

My reply - not really. I just thought that would be the answer to my problem. I really am focused on putting that very value into the switch statement. I could use nested if-else loops but I'm concerned about debugging them later on. For me, switch statements are cleaner - they're a lot easier to understand.
As for the line