I am a beginner in learning java. I don't know how to write code to create a class to get user input. I have written the rest of the program, but I know I am missing the class that would make the program recognize the user input. I am using netbeans to create the program. I have gotten as far as starting a new class in the library I created, but I'm not sure what to type to create the class and give it attributes to get the user information. Is there a class I can import to accomplish this? This is the way my code looks without the class:
Code
/* Payroll.java
* Calculates employees weekly pay.
* Shannon Singleton 10/19/08 12:00PM
*/
package payroll;
/**
*
* @author shannon.singleton
*/
import java.util.Scanner;//program uses class Scanner
public class Payroll {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// Create Scanner to retrieve user input
Scanner input = new Scanner(System.in);
String name;//employee name
double number1;//hourly rate
double number2;//hours worked
double pay; //hourly rate times hours worked
System.out.println( "What is the employee's name?"); //prompt
name = input.nextSring(); //read employee name from user
System.out.println( "What is the employee's hourly rate?"); //prompt
number1 = input.nextDouble(); // read employee's hourly rate from user
System.out.println( "What is the employee's hours worked?");//prompt
number2 = input.nextDouble(); //read employee's hours worked from user
pay = number1 * number2; //Multiply hourly rate by hours worked
System.out.printf( "%s made $%.2f this week.\n", name, pay);
//displays name and pay)
}//end method main
}//end class Payroll
/Code
Getting user string inputPayroll homework
Page 1 of 1
10 Replies - 38113 Views - Last Post: 20 February 2012 - 08:28 AM
Replies To: Getting user string input
#2
Re: Getting user string input
Posted 24 October 2008 - 03:44 PM
you have already imported the scanner class and created a new scanner object and taken input from the user, what is the problem now??
#3
Re: Getting user string input
Posted 24 October 2008 - 03:56 PM
Also remember to your code inside a code block like this...
your code here
#4
Re: Getting user string input
Posted 24 October 2008 - 04:05 PM
When it runs, it just stops after the first question, which is "What is the employee's name?" It looks like it's waiting on input from a user. I try to type something in, but it doesn't let me. I'm assuming I need to import some other class to get it to work.
#5
Re: Getting user string input
Posted 24 October 2008 - 04:13 PM
I went back to Netbeans and ran it again. I didn't realize I had to double click for it to put in user information! The problem I have now is that it only let me type in one character for the string "What is the employee's name.
#6
Re: Getting user string input
Posted 24 October 2008 - 04:26 PM
you dont need an extra class for it, just the scanner...but i dont know about the nextString() method for input, i always use nextLine()..give it a try and also if you have a problem with netbeans, try to run the program from the command prompt
#7
Re: Getting user string input
Posted 24 October 2008 - 05:11 PM
I believe the nextLine() method in place of nextString() should solve all your problems.
#8
Re: Getting user string input
Posted 24 October 2008 - 06:28 PM
That did the trick. Thanks. Now I'm working on putting a while loop in place to have the user continuously enter the info until he enters stop to quit. I also have to add a prompt that tells the user to enter a positive number if they try to enter a negative number for hours worked and hourly pay. I tried to impliment my while loop, but it's telling me I am using I can't use != to compare strings. If I can't use that then what do I use?
CODE
public static void main(String[] args) {
// Create Scanner to retrieve user input
Scanner input = new Scanner(System.in);
String name;//employee name
String stop;//initializes stop
double number1;//hourly rate
double number2;//hours worked
double pay; //hourly rate times hours worked
System.out.print( "What is the employee's name?"); //prompt
name = input.nextLine(); //read employee name from user
while(name != stop)
{
System.out.print( "What is the employee's hourly rate?"); //prompt
number1 = input.nextDouble(); // read employee's hourly rate from user
System.out.print( "What is the employee's hours worked?");//prompt
number2 = input.nextDouble(); //read employee's hours worked from user
pay = number1 * number2; //Multiply hourly rate by hours worked
System.out.printf( "%s made $%.2f this week.\n", name, pay);
//displays name and pay)
System.out.print( “What is the employee’s name?” ); //prompt
}//end while
}//end method main
}//end class Payroll
CODE
public static void main(String[] args) {
// Create Scanner to retrieve user input
Scanner input = new Scanner(System.in);
String name;//employee name
String stop;//initializes stop
double number1;//hourly rate
double number2;//hours worked
double pay; //hourly rate times hours worked
System.out.print( "What is the employee's name?"); //prompt
name = input.nextLine(); //read employee name from user
while(name != stop)
{
System.out.print( "What is the employee's hourly rate?"); //prompt
number1 = input.nextDouble(); // read employee's hourly rate from user
System.out.print( "What is the employee's hours worked?");//prompt
number2 = input.nextDouble(); //read employee's hours worked from user
pay = number1 * number2; //Multiply hourly rate by hours worked
System.out.printf( "%s made $%.2f this week.\n", name, pay);
//displays name and pay)
System.out.print( “What is the employee’s name?” ); //prompt
}//end while
}//end method main
}//end class Payroll
#9
Re: Getting user string input
Posted 24 October 2008 - 07:01 PM
nope, you cant compare strings like that "!=" can only be used to compare primitive data types..to compare Strings you will have to use String methods..
this method is a boolean method, it returns true if the 2 Strings are equals and false otherwise..note that this method is case sensitive.. for more info about String methods, check the API
String s1; String s2; s1.equals( s2 ); // returns true if s1 equals s2 and returns false otherwise
this method is a boolean method, it returns true if the 2 Strings are equals and false otherwise..note that this method is case sensitive.. for more info about String methods, check the API
This post has been edited by mostyfriedman: 24 October 2008 - 07:05 PM
#10
Re: Getting user string input
Posted 26 October 2008 - 08:03 PM
while(name != stop)
This will always be true
- the String pointer name does not point to the same String as stop which is not initialized
You'll have tp
String stop = "stop";
name = input.nextLine();
while(!name.equals(stop)) {
...
This will always be true
- the String pointer name does not point to the same String as stop which is not initialized
You'll have tp
String stop = "stop";
name = input.nextLine();
while(!name.equals(stop)) {
...
#11
Re: Getting user string input
Posted 20 February 2012 - 08:28 AM
Hey sorry if this too late but ive been mucking baout with this way of getting input, maybe you can gleen something from this.
Make a project in Netbeans and call it "MyWriteFile" to save you some hassles.
and then copy this in to see how you can read in a string of any length (practically) and it will copy the whole thing and repeat it in the console below.
I see that you could adapt this to intake a string from an input scenario and do something with it, be it write it to file, or store in a db , i dunno.
I hope this helps in some small way.
Cheers Gruffy
wo just seen how old that post is , whoops
Make a project in Netbeans and call it "MyWriteFile" to save you some hassles.
and then copy this in to see how you can read in a string of any length (practically) and it will copy the whole thing and repeat it in the console below.
I see that you could adapt this to intake a string from an input scenario and do something with it, be it write it to file, or store in a db , i dunno.
I hope this helps in some small way.
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package mywritefile;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.Scanner;
import java.util.Locale;
/**
*
* @author MuthaLoad
*/
public class MyWriteFile {
/**
* @param args the command line arguments
*/
public static void main(String[] args)
throws IOException {
Scanner s = null; //create scanner storage var
System.out.println("welcome to the copier\n\tWould you like to input something\n\tand see it copied?");
try
{
s = new Scanner (System.in);// construct scanner object s and...
//...tell it from where to scan from.
//use the .hasNext() method to identify that another character is stored after the last
while (s.hasNext()) //while text in s continues/is true
{
//use the .next() method to tumble out the words stored in s
System.out.println(s.next()); //print out what is stored in s
}
//The following "finally" block
//method cleans up and then closes the PrintWriter
} finally //clean up and clear out the printer
{
if (s != null) //if s not equal to null
{
s.close();//close down scanner s
}
}
}
}
Cheers Gruffy
wo just seen how old that post is , whoops
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote





|