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. (Kindly refer to the attached screenshot.)
http://i429.photobucket.com/albums/q...program_00.jpg
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. As you can see in the code's screenshot, 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)
http://i429.photobucket.com/albums/q...is_cropped.jpg
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). Kindly see the attached photo for reference...
http://i429.photobucket.com/albums/q...message_00.jpg
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...)
#[#]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
}
}
[/#]