• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

User Input

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I didn't want my first post on this site to be homework related but it is, I'm not asking for you anyone to do my homework, I'm just stuck. I need to get my user to input data, so I wrote the code in the beginning to import the scanner, like this.
mport java.util.Scanner;

class SalesInput
{
// Create a Scanner object for keyboard input.
Scanner keyboard = new Scanner(System.in);
}

When I need the user to input data I wrote:

{
System.out.println ("Enter annual sales: ");
sales = keyboard.nextDouble();
}

But it's telling me (netbeans) that it cannot find the symbol keyboard. I want the scanner to be the keyboard to input information, I don't want a new class or field for it do I?

I read another post from 2011 not to use netbeans if you are a beginner, unfortunately this is what the class requires, any advice would be greatly appreciated.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
we can't really say without seeing more of your code. Did you create a SalesInput object? is your second block in the SalesInput class, or in some other driver class?
 
Andi Lindenmayer
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sure, I didn't post the entire code because I didn't want it to appear as if I was asking for someone to do my homework. Here is the entire code:

import java.util.Scanner;

class SalesInput
{
// Create a Scanner object for keyboard input.
Scanner keyboard = new Scanner(System.in);
}
/**
* This program calculates a salesperson's compensation in addition to salary
* @author andrealindenmayer
*/
public class SalespersonCompensationCalculator {
private static Object input;
public double sales; // user input sales
private double salary, // monthly salary
rate, // rate of commission
commission, // amount of commission
CalculatePay; //amount to pay
private double setRate;
private double s;
private double c;
private double pay;


/**
* The constructor uses two parameters to accept arguments.
* c is assigned to the sales field for commission and s is assigned to the
* salary field.
* @param args the command line arguments
*/

public SalespersonCompensationCalculator (double c, double s, double n)
{commission = c;
salary = s;
sales = n;
CalculatePay();
}
public static void main(String[] args) {
//This method displays the salesperson's fixed salary
double s;
s = 120000;

System.out.println ("The Salesperson earns a fixed monthly salary of sales + commission.");

/**
* The setRate method is based on the amount of sales.
*
*/
}
public class sales {

public sales() {
}

{
System.out.println ("Enter annual sales: ");
sales = keyboard.nextDouble();
}

}

private void setRate ()
{
rate = 0.06;
setRate = sales * rate;
}

/**
* The calculatePay method calculates the salesperson's actual pay, which
* is commission and salary.
*/


private void CalculatePay ()
{
setRate();
commission = setRate;
CalculatePay = s + c;
}
/**
* The getPay method returns the pay field
*/

public double getPay()
{
return pay;
}

/**
* The getCommission method returns the commission field.
*/

public double getCommission()
{
return commission;
}

/**
* The getSales method returns the sales field.
*/

public double getSales()
{
return sales;
}

}
 
Greenhorn
Posts: 4
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
why don't you try creating a Scanner inside the method sales,



this may fix the problem.
 
Andi Lindenmayer
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I changed it, and at least I'm not pulling any more errors, but it is not actually prompting me to input anything. I also changed the Scanner to s at the beginning of the code to see if that would fix it, but it didn't.

import java.util.Scanner;

class SalesInput
{
// Create a Scanner object for keyboard input.
Scanner S = new Scanner(System.in);
}
/**
* This program calculates a salesperson's compensation in addition to salary
* @author andrealindenmayer
*/
public class SalespersonCompensationCalculator {
private static Object input;
public double sales; // user input sales
private double salary, // monthly salary
rate, // rate of commission
commission, // amount of commission
CalculatePay; //amount to pay
private double setRate;
private double s;
private double c;
private double pay;


/**
* The constructor uses two parameters to accept arguments.
* c is assigned to the sales field for commission and s is assigned to the
* salary field.
* @param args the command line arguments
*/

public SalespersonCompensationCalculator (double c, double s, double n)
{commission = c;
salary = s;
sales = n;
CalculatePay();
}
public static void main(String[] args) {
//This method displays the salesperson's fixed salary
double s;
s = 120000;

System.out.println ("The Salesperson earns a fixed monthly salary of sales + commission.");

/**
* The setRate method is based on the amount of sales.
*
*/
}
public class sales {

public sales() {
Scanner S = new Scanner(System.in);
System.out.println("Enter annual sales: ");
sales = S.nextDouble();
}

}

private void setRate ()
{
rate = 0.06;
setRate = sales * rate;
}

/**
* The calculatePay method calculates the salesperson's actual pay, which
* is commission and salary.
*/


private void CalculatePay ()
{
setRate();
commission = setRate;
CalculatePay = s + c;
}
/**
* The getPay method returns the pay field
*/

public double getPay()
{
return pay;
}

/**
* The getCommission method returns the commission field.
*/

public double getCommission()
{
return commission;
}

/**
* The getSales method returns the sales field.
*/

public double getSales()
{
return sales;
}

}
Screen-Shot-2013-06-07-at-11.57.09-AM.png
[Thumbnail for Screen-Shot-2013-06-07-at-11.57.09-AM.png]
 
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Andi Lindenmayer wrote:I changed it, and at least I'm not pulling any more errors, but it is not actually prompting me to input anything.


Where do you call the sales() method from ?
And please UseCodeTags if you are going to post any more code. It makes it a lot more readable.
 
fred rosenberger
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is your code formatted correctly with code tags. See how it is easier to read?



Here is your main method, boiled down to its bare essentials:


So, when you run things, this is what executes. All it is going to do is print out one line. That's probably not what you want.

In my humble opinion, you should consider starting over. You have 100 lines of code that don't even come close to doing what you want. trying to go back and fix all of it is almost certainly going to doom you to failure.

Never write more than 2-3 lines of code before you compile and test. and test a LOT. When you write a new method, your first iteration should have it do nothing more than print "i'm in <new method name>". Make sure you can call it. Then (if needed) change it to pass in parameters, printing them out. compile and test. Then have the method start to do stuff - but just a piece at a time. the more often you compile and test, and the fewer changes made between iterations, the better off you will be.
 
Andi Lindenmayer
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, I didn't know how to make it look like that. I'll start over tonight and see what I can get, I was instructed to use an example out of the book we are assigned to read. I'll try again, thanks for your help.
 
reply
    Bookmark Topic Watch Topic
  • New Topic