Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: How to assign an array cell value into a switch statement

  1. #1
    Junior Member
    Join Date
    Apr 2013
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to assign an array cell value into a switch statement

    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
    }
    }

    [/#]


  2. #2
    Junior Member
    Join Date
    Apr 2013
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to assign an array cell value into a switch statement

    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...rormessage.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
     }            
    }

  3. #3
    Member
    Join Date
    Sep 2012
    Posts
    128
    Thanks
    1
    Thanked 14 Times in 14 Posts

    Default Re: How to assign an array cell value into a switch statement

    Java 7 allows a switch on a String, so you don't need to use a CharArray.

    A few points:
    • Names of classes by convention start with a capital letter.
    • Try to use descriptive variables. 'wr', 'ph', 'n' and 'v' are a bit cryptic for those trying to read your code.
    • Close your scanners: Scanner (Java Platform SE 7 )
    • I'm being a bit pedantic now, but if the file is data, then I would use .dat extension.
    • Just cut and paste error messages. You don't need pictures.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How to assign an array cell value into a switch statement

    Please copy the full text of the error message and paste it here. No images.

    Please edit your post and wrap your code with code tags:
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Apr 2013
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to assign an array cell value into a switch statement

    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...)
    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
     }            
    }

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How to assign an array cell value into a switch statement

    I get error messages when I attempt to do so. (the compiler says they're incompatible data types).
    Please copy full text of error message and paste it here.
    The message should show the source with a ^ under the location of the error.
    Here is a sample from the javac compiler:
    TestSorts.java:138: cannot find symbol
    symbol  : variable var
    location: class TestSorts
             var = 2;
             ^
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Apr 2013
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to assign an array cell value into a switch statement

    Quote Originally Posted by Norm View Post
    Please copy full text of error message and paste it here.
    The message should show the source with a ^ under the location of the error.
    Here is a sample from the javac compiler:
    TestSorts.java:138: cannot find symbol
    symbol  : variable var
    location: class TestSorts
             var = 2;
             ^
    I'm using the freeware JCreator LE 3.50. Whenever an error message shows up, it displays it in a box below the source code (as can be seen in the link in my very first post on this thread). I tried doing a copy and paste of it but all I get from it is the filename of my source code. Pardon if I am unable to comply with what's required regarding the error message. The best that I can do regarding that is to copy and paste the screenshot of the error message in question...

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How to assign an array cell value into a switch statement

    Without good error message text its hard to solve the problem. A problem with images is that the text of the error message can not be copied to here to make comments on.
    Also many images are too small to be able to read the text.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Member Chris.Brown.SPE's Avatar
    Join Date
    May 2008
    Location
    Fort Wayne, Indiana
    Posts
    190
    Thanks
    1
    Thanked 31 Times in 31 Posts

    Default Re: How to assign an array cell value into a switch statement

    Your problem is tha tyou cant switch on an array itself. You mention that the value of ven[4] is a single char and that is what you want to switch on. You have a couple options, take a look at them and let me know if you have any questions or if one of them fits your situation.

    1. Switch on the string ven[4] if you're using Java 7
    2. Switch on the specific char by either using ven[4].charAt(0) or mynewstring.charAt(mynewstring.length()-1) or cArray[0]
    Writing code is your job, helping you fix and understand it is mine.

    <-- Be sure to thank and REP (Star icon) those who have helped you. They appreciate it!

Similar Threads

  1. [SOLVED] A Loop statement and a switch statement issue
    By sternfox in forum Loops & Control Statements
    Replies: 13
    Last Post: March 7th, 2013, 04:19 PM
  2. Changing strings in a switch statement n stroing it in an array
    By ajw1993 in forum What's Wrong With My Code?
    Replies: 25
    Last Post: February 20th, 2013, 05:25 PM
  3. Replacing an If statement with a Switch statement
    By logi in forum Loops & Control Statements
    Replies: 9
    Last Post: February 4th, 2013, 12:21 AM
  4. Do you add objects to an array when using switch statement with a For loop?
    By TheWhopper858 in forum Collections and Generics
    Replies: 2
    Last Post: November 13th, 2011, 01:28 PM
  5. switch statement
    By Tank314 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 9th, 2011, 01:23 PM

Tags for this Thread