Results 1 to 3 of 3
  1. #1
    zlloyd1 is offline Member
    Join Date
    Nov 2012
    Posts
    57
    Rep Power
    0

    Exclamation While loop won't accept value from another class

    I am getting SOOOO tired of Java and its constant whining about incompatible types!! I mean every time I try to add anything to a program in Java it gets a red line underneath it.... My latest sagea of how ridiculous Java is comes from trying to introduce a while loop into my existing main class. I need to take a user inputted value (int), and create a loop that will iterate based on it, but every time I try to use the user input in the while loop it comes back that it is incompatible types!!!! Well that's enough griping, here is the code:
    Java Code:
    public class Pay {
        @SuppressWarnings("empty-statement")
        public static void main(String[] args)
        {   double T = 50000; //set value for base salary                
            double Amt; //declare variable for final pay
            int x = 1;
            ArrayList Who = new ArrayList();
            EvalNum Times = new EvalNum();
            
        while(Times >0){
         do{ 
             Scanner Empname = new Scanner(System.in);
             System.out.println("Enter employee name:");
             String Applicant = Empname.nextLine();
             Who.add(Applicant);
    or at least the block of code where the error is coming from. While loop won't accept value from another class-incompatibletypez.jpg
    It keeps saying, "Bad operand types for binary operator '>' First type EvalNum: Second type int" and I am completely frozen there, because the variable used, Times is an int also in the class EvalNum, see here:
    Java Code:
        public void main(String args[]){         
    int Evals;        
    Scanner EmpNum = new Scanner(System.in);
    I need for the main class file to call the EvalNum class file to retrieve a simple integer value entered by a user, but I also need that integer to mean something in the main program, so I need to create a loop based off of that number. My main boggle on this is HOW CAN THERE BE AN INCOMPATIBILITY between and int, and another int??
    In any case Let me add the entire code here, and expand on what it is I am trying to accomplish with this program:
    Java Code:
    package Annual;
    import java.util.*;
    import Annual.EvalNum;
    public class Pay {
        @SuppressWarnings("empty-statement")
        public static void main(String[] args)
        {   double T = 50000; //set value for base salary                
            double Amt; //declare variable for final pay
            int x = 1;
            ArrayList Who = new ArrayList();
            EvalNum Times = new EvalNum();        
        while(Times >0){
         do{ 
             Scanner Empname = new Scanner(System.in);
             System.out.println("Enter employee name:");
             String Applicant = Empname.nextLine();
             Who.add(Applicant);       
           try{ //validate user input
            Scanner percent = new Scanner(System.in);
            System.out.println("Please enter annual sales:");//request user input
            System.out.println("Please enter valid dollar value:");
            double yearly = percent.nextDouble(); //store user input
               if (yearly >= 0 && yearly <= 500000){ //if over $500,000.00 in sales
               if (yearly >80000){ //if 80% sales goal was not reached
            double YrPrcnt = yearly * .05; //value used for compensation set next
            Amt = T + YrPrcnt * 1.25; //set Amt to pay plus commissions
            System.out.print("The Annual Payrate for employee is: $");
            System.out.println(Amt);}
               else {
                 System.out.print("The Annual Payrate for employee is: $"); 
                 System.out.println(T);
                 System.out.println();}}
                  else {
               System.err.print("Sales too high, ");
               System.err.println("please see Human Resources.");}
               x=2; break;}
            catch(Exception e){ //if user input throws exception
                System.out.println("Try again please");}}while(x==1);
         CompensationChart C = new CompensationChart();
         CompensationChart.ListIt();
    }}}
    This is the main program file, Pay.java, for my project, which contains the main class and it is supposed to call two other files to complete a payroll commissions calculator for employees. The first of the other two files is called EvalNum.java, and contains external classes within the package, Annual. In EvalNum the user is prompted to enter a number of employees that will be evaluated by the program, and then this number is needed to create the number of times that the user prompts for further information will run in Pay.java, the main program. Here is that code:
    Java Code:
    package Annual;
    
    import java.util.Scanner;
    
    public class EvalNum
    {
         public void main(String args[]){         
    int Evals;        
    Scanner EmpNum = new Scanner(System.in);
    System.out .println("How many employees to be evaluated?");
    Evals = EmpNum.nextInt();
    System.out.println(Evals);}}
    This works correctly, and retrieves the user input soundly, but I am having a time getting the integer value collected by this program to be recognized as an integer in Pay.java for some reason that I cannot figure out for the life of me.... Anyway, assuming that I can EVER get the while loop to work in Pay.java, it will then, finally call a third program, CompensationChart.java, which returns a chart of potential annual salaries, including calculated commissions based on user inputted annual sales amounts. There is no need to include that code, because I am stuck in Pay.java before I can ever call that file. The expected output is a user prompt for a number of evaluations to be done, followed by a number of prompts to enter employee names, followed by yet another prompt to enter employee annual sales, all of which will be followed by a return of charted yearly pay for each employee evaluated, and followed by a chart of potential compensation amounts based on the employee salary ranges. Unfortunately, as complex as this all sounds, I cannot even get started with it because I cannot be allowed to call data from an external class, and use it for anything except just to sit there in my code and do nothing!! PLEASE HELP!!
    Last edited by zlloyd1; 11-25-2012 at 06:22 AM.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,528
    Blog Entries
    7
    Rep Power
    20

    Default Re: While loop won't accept value from another class

    Line #11 tells the compiler that Times is an EvalNum, you're even creating a new instance for it, but line #12 tells the compiler that you want to compare an EvalNum (Times) with an int (0); that definitely doesn't make sense and the compiler starts to complain ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    zlloyd1 is offline Member
    Join Date
    Nov 2012
    Posts
    57
    Rep Power
    0

    Question Re: While loop won't accept value from another class

    Quote Originally Posted by JosAH View Post
    Line #11 tells the compiler that Times is an EvalNum, you're even creating a new instance for it, but line #12 tells the compiler that you want to compare an EvalNum (Times) with an int (0); that definitely doesn't make sense and the compiler starts to complain ...

    kind regards,

    Jos
    I am not sure what you are saying about line 12 not making sense to be honest. That is why I need to import the value from EvalNum, to create a counter for a loop. As to creating a new instance of Times, that is NOT my goal there, I just want to call EvalNum to return an integer value that I can use to control a loop.

Similar Threads

  1. Program that must accept input redirection
    By static_rage in forum New To Java
    Replies: 1
    Last Post: 10-11-2012, 02:13 PM
  2. SSLServerSocket accept() Denial Of Service
    By frankAJ in forum Networking
    Replies: 0
    Last Post: 04-19-2011, 09:59 PM
  3. ServerSocket.accept() using 100%+ CPU
    By pagod in forum Networking
    Replies: 2
    Last Post: 05-28-2010, 09:42 AM
  4. Accept 2 values ....
    By danny000 in forum New To Java
    Replies: 1
    Last Post: 07-31-2007, 02:12 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •