7 Replies - 260 Views - Last Post: 01 August 2013 - 02:55 PM Rate Topic: -----

#1 JohnSalx1028  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 01-August 13

Problem: New to this, and cannot figure this one out.

Posted 01 August 2013 - 12:53 PM

import java.util.Scanner;
public class RevisedPayroll {
    
/**
* main method begins execution of Java application
*/

public static void main(String[] args) {
Scanner reader = new Scanner (System.in);               
        
        
double PayRate;
double RegHours;
double WeeklyPay;
double RegPay;
char a;
      
System.out.println("Employee name: ");
String Name = reader.nextLine(); // read text
        
if (a == 'n')
{
System.out.println( "Thank You! Have a nice day!" );
}
else if (a == 'y')
{
        
System.out.print("Rate of pay: ");//user input prompt
PayRate = reader.nextDouble();//read numbers

System.out.println("Reg hours worked: ");//user input prompt
RegHours = reader.nextDouble();//read numbers
        
RegPay = PayRate * RegHours;//calculation
        
WeeklyPay = RegPay;//calculation

while( WeeklyPay <= -1) {

	System.out.println( "Payroll cannot be a negative number" );
}
        
System.out.println(Name);//prints text
        
System.out.printf("$%.2f\n", WeeklyPay);

System.out.println( "Would you like to do another employee? y/n");

a = reader.next().charAt();
        

   } }//end method main

}//end class RevisedPayroll



Ok so my problem is that on line 49 the a = reader.net().charAt(); line I get an error saying 'Method charAt in class String cannot be applied to given types;
a = reader.net.().charAt();
^
required: int
found: no arguments
reason: actual and formal argument lists differ in length
1 error

So what I am asking is what I can do to fix this? Also on a side question On the line where I put 'System.out.println( "Payroll cannot be a negative number" );, how can i get my program to stop there? Like just exit the program, at the moment it just keeps repeating the phrase.

Is This A Good Question/Topic? 0
  • +

Replies To: Problem: New to this, and cannot figure this one out.

#2 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2657
  • View blog
  • Posts: 11,207
  • Joined: 20-September 08

Re: Problem: New to this, and cannot figure this one out.

Posted 01 August 2013 - 01:04 PM

So, what do you think charAt does, or should do?
Was This Post Helpful? 0
  • +
  • -

#3 JohnSalx1028  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 01-August 13

Re: Problem: New to this, and cannot figure this one out.

Posted 01 August 2013 - 01:10 PM

Hello g00se,

To be entirely honest with you, I am still practicing with this and I was looking up a tutorial and this is how he described would be the best way to do it, when I asked why this way or how it should work, I received no replies at all.
Was This Post Helpful? 0
  • +
  • -

#4 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2657
  • View blog
  • Posts: 11,207
  • Joined: 20-September 08

Re: Problem: New to this, and cannot figure this one out.

Posted 01 August 2013 - 01:18 PM

Well i asked you a specific question ... ;)
Was This Post Helpful? 0
  • +
  • -

#5 JohnSalx1028  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 01-August 13

Re: Problem: New to this, and cannot figure this one out.

Posted 01 August 2013 - 01:25 PM

Hello g00se,

From what I recall I think it resets a value or something like that, pretty sure that is not correct though.
Was This Post Helpful? 0
  • +
  • -

#6 JohnSalx1028  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 01-August 13

Re: Problem: New to this, and cannot figure this one out.

Posted 01 August 2013 - 01:48 PM

I changed my code up a bit, but am getting a whole new error.

Here is the new code:
import java.util.Scanner;
public class RevisedPayroll {
    
/**
* main method begins execution of Java application
*/

public static void main(String[] args) {
Scanner reader = new Scanner (System.in);               
        
        
double PayRate;
double RegHours;
double WeeklyPay;
double RegPay;
double Response;
      
System.out.println("Employee name: ");
String Name = reader.nextLine(); // read text
        
if (Response == '2')
{
System.out.println( "Thank You! Have a nice day!" );
}
else if (Response == '1')
{
        
System.out.print("Rate of pay: ");//user input prompt
PayRate = reader.nextDouble();//read numbers

System.out.println("Reg hours worked: ");//user input prompt
RegHours = reader.nextDouble();//read numbers
        
RegPay = PayRate * RegHours;//calculation
        
WeeklyPay = RegPay;//calculation

while( WeeklyPay <= -1) {

	System.out.println( "Payroll cannot be a negative number" );
}
        
System.out.println(Name);//prints text
        
System.out.printf("$%.2f\n", WeeklyPay);

System.out.println( "Would you like to do another employee? 1 = yes, 2 = no");
Response = reader.nextDouble(); //Read Reply

        

   } }//end method main

}//end class RevisedPayroll




The error I get says that the variable response might not have been initialized

If (Response == '2')
^ Error
Was This Post Helpful? 0
  • +
  • -

#7 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2657
  • View blog
  • Posts: 11,207
  • Joined: 20-September 08

Re: Problem: New to this, and cannot figure this one out.

Posted 01 August 2013 - 02:17 PM

Quote

From what I recall I think it resets a value or something like that, pretty sure that is not correct though.


a. you can't write code whose function and meaning is unclear to you - completely pointless
b. if you're unsure, why don't you consult the api docs? http://docs.oracle.c.../api/index.html
Was This Post Helpful? 0
  • +
  • -

#8 schutzzz  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 136
  • View blog
  • Posts: 338
  • Joined: 22-April 13

Re: Problem: New to this, and cannot figure this one out.

Posted 01 August 2013 - 02:55 PM

View PostJohnSalx1028, on 01 August 2013 - 08:48 PM, said:

I changed my code up a bit, but am getting a whole new error.

Here is the new code:
import java.util.Scanner;
public class RevisedPayroll {
    
/**
* main method begins execution of Java application
*/

public static void main(String[] args) {
Scanner reader = new Scanner (System.in);               
        
        
double PayRate;
double RegHours;
double WeeklyPay;
double RegPay;
double Response;
      
System.out.println("Employee name: ");
String Name = reader.nextLine(); // read text
        
if (Response == '2')
{
System.out.println( "Thank You! Have a nice day!" );
}
else if (Response == '1')
{
        
System.out.print("Rate of pay: ");//user input prompt
PayRate = reader.nextDouble();//read numbers

System.out.println("Reg hours worked: ");//user input prompt
RegHours = reader.nextDouble();//read numbers
        
RegPay = PayRate * RegHours;//calculation
        
WeeklyPay = RegPay;//calculation

while( WeeklyPay <= -1) {

	System.out.println( "Payroll cannot be a negative number" );
}
        
System.out.println(Name);//prints text
        
System.out.printf("$%.2f\n", WeeklyPay);

System.out.println( "Would you like to do another employee? 1 = yes, 2 = no");
Response = reader.nextDouble(); //Read Reply

        

   } }//end method main

}//end class RevisedPayroll




The error I get says that the variable response might not have been initialized

If (Response == '2')
^ Error


Response can't equal 2 if you have it only declared and not initialized. It's also bad practice to name variables starting with a capital letter. use camelCase. In this situation it should just be response. Also if you're trying to see if it's equal to a double in this case you don't need ' it should just be if(response == 2)

Again though, it needs to be initialized before you try to use it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1