-
April 13th, 2013, 03:24 AM
#1
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...)
Code:
import java.util.*;
import java.io.*;
public class readDB
{
public static void main(String args[])throws Exception
{
Scanner v = new Scanner(System.in);
Scanner i = new Scanner(new File("info.txt"));
String s,salarytype;
StringBuffer result = new StringBuffer();
double ph=0;
Boolean wr=false;
System.out.println(" ");
System.out.print("Enter Employee Code: ");
s = v.nextLine();
while (i.hasNextLine())
{ //first while loop start brace
n = i.nextLine();
String ven[] = n.split(";");
if (s.equals(ven[0]))
{
System.out.println(" ");
System.out.println("Name: " + ven[1]);
System.out.println("Age: " + ven[2]);
System.out.println("Address: " + ven[3]);
System.out.println("Salary Type: " + ven[4]);
result.append(ven[4]);
String mynewstring = result.toString();
char[] cArray = mynewstring.toCharArray();
switch(cArray)
{
case 1: ph=200;
System.out.println("Salary per Hour: "+ph);
break;
case 2: ph=300;
System.out.println("Salary per Hour: "+ph);
break;
case 3: ph=500;
System.out.println("Salary per Hour: "+ph);
break;
}
wr=true;
} else if (wr==false)
{
System.out.println ("Invalid Code ");
}
} //first while loop end brace
}
}
-
April 17th, 2013, 11:40 AM
#2
Re: How to insert the value inside an array's cell into a char variable?
Hi,
You can't switch on char arrays, and if you are using a JRE version prior to Java7 not even on Strings.
So, you can't do:
Code:
switch (cArray) {[....]}
Look at this site
Try something like:
Code:
if (mynewstring.equals("1")) {
// whatever
} else if (mynewstring.equals("2")) {
// whatever
} else {
// whatever
}
Hope this helps.
Regards,
Albert.
Please, correct me. I'm just learning.... and sorry for my english :-)
-
April 21st, 2013, 05:37 PM
#3
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?
Besides, aren't switch statements a lot clearer than embedded "if-else" statements?
-
April 23rd, 2013, 12:31 AM
#4
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?
Last edited by nuzzle; April 23rd, 2013 at 12:38 AM.
-
April 23rd, 2013, 03:24 AM
#5
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
Code:
int mynewint = Integer.parseInt(mynewstring);
Would it mean that my program would be written like this now?
Code:
System.out.println(" ");
System.out.println("Name: " + ven[1]);
System.out.println("Age: " + ven[2]);
System.out.println("Address: " + ven[3]);
System.out.println("Salary Type: " + ven[4]);
result.append(ven[4]);
String mynewstring = result.toString();
int mynewint = Integer.parseInt(mynewstring);
char[] cArray = mynewstring.toCharArray();
switch(cArray)
{
case 1: ph=200;
System.out.println("Salary per Hour: "+ph);
break;
case 2: ph=300;
System.out.println("Salary per Hour: "+ph);
break;
case 3: ph=500;
System.out.println("Salary per Hour: "+ph);
break;
...
Otherwise, where in the source code should I place the line you suggested? And what other lines do I need to remove to make the code work?
Thanks for replying by the way..... :-)
-
April 24th, 2013, 04:18 AM
#6
Re: How to insert the value inside an array's cell into a char variable?
Originally Posted by PC_flea
Otherwise, where in the source code should I place the line you suggested? And what other lines do I need to remove to make the code work?
Come on. You've been led to the waterhole but you'll have to do the drinking yourself.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|