I am working on a project for school. at this point i'm just going over board, I would like to run the class bookstoreCreditPersonal if none of the following conditions are true, but I cant get it to work. any suggestions?
import java.util.Scanner;
public class bookstoreCreditPersonal {
public static void main(Object o) {
String studentNamePers;
String userType;
double studentGPAPers;
double bookstoreCreditPers;
Scanner input = new Scanner(System.in);
System.out.print("Please enter 'S' if you are the student, 'T' if you are the teacher, or 'P' if you are the Parent: ");
userType = input.nextLine();
if (userType.equals("S")) {
System.out.println("Greetings student...");
Scanner Sinput = new Scanner(System.in);
System.out.println("Please enter your(The students) first and last name :");
studentNamePers = input.nextLine();
Scanner SSinput = new Scanner(System.in);
System.out.println("Please enter your(The student's) GPA :");
studentGPAPers = input.nextDouble();
bookstoreCreditPers = studentGPAPers * 10;
System.out.println(studentNamePers + ", your GPA is " + studentGPAPers + ", and you have an available bookstore credit of $" + bookstoreCreditPers);
} else if (userType.equals("T")) {
System.out.println("Teacher");
} else if (userType.equals("P")) {
System.out.println("Parent");
} else {
System.out.println("Lets try that again, one character, in capital form only please.");
//created a class that reruns this class
runClassBSCP.call(null);
}
}
}
Here is the class runClassBSCP:
public class runClassBSCP {
public void call() {
bookstoreCreditPersonal.main(null);
}
}
You need to instantiate/create an object of the class. Then you can call the desired method with the object.
runClassBSCP bscp = new runClassBSCP();
bscp.call();
Also, your class names should always start with an uppercase letter: RunClassBSCP, rather than `runClassBSCP'. For more info, check out Code Conventions for the Java Programming Language.
Related
The code below gets name and age of the animal and displays it. When I give 1 for dogs, for the first time the code runs correctly and when I give 1 for second time, i.e. when count is changed to 2, the code gets only the age and skips the name.
This happens for both Dog and Cat classes. And also the IDE shows that there is a resource leak problem near the Scanner scan. Please give me a solution
NOTE: This is the first time I'm programming in java, so I can't explain the problem clearly. So please excuse my flaws.
/**
*
*/
package pets;
import java.util.Scanner;
/**
* #author Karthic Kumar
*
*/
public class pets {
public static int total = 0;
public class Dog
{
int age;
String name;
int serial_no=1;
Scanner scan = new Scanner(System.in);
public void get_name()
{
System.out.println("enter the name of the dog: ");
name = scan.nextLine();
}
public void get_age()
{
System.out.println("enter the age of the dog: ");
age = scan.nextInt();
}
public void display()
{
System.out.println(serial_no+". the name of the dog is "+ name +" and his age is "+ age+"\n");
}
public void total_display()
{
System.out.println("total animals = "+total);
}
}
public class Cat
{
int age;
String name;
int serial_no=1;
Scanner scan = new Scanner(System.in);
public void get_name()
{
System.out.println("enter the name of the cat: ");
name = scan.nextLine();
}
public void get_age()
{
System.out.println("enter the age of the cat: ");
age = scan.nextInt();
}
public void display()
{
System.out.println(serial_no+". the name of the dog is "+ name +" and his age is "+ age);
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scan = new Scanner(System.in);
int type,count=1;
pets object_1 = new pets();
pets.Dog dog = object_1.new Dog();
pets.Cat cat = object_1.new Cat();
System.out.println("press one for dog and two for cat and 3 for total");
while(count<=10)
{
System.out.println("\n"+count+". ");
type = scan.nextInt();
if(type == 1)
{
dog.get_name();
dog.get_age();
dog.display();
dog.serial_no +=1;
pets.total+=1;
}
if(type == 2)
{
cat.get_name();
cat.get_age();
cat.display();
cat.serial_no+=1;
pets.total+=1;
}
if(type==3)
{
dog.total_display();
}
if(type <=4 && type >=100)
{
System.out.println("enter correctly");
}
count++;
}
}
}
In order to fix your problem, you simply need to add a call to method nextLine() of class Scanner in your get_age() method in both class Cat and class Dog.
Here is the entire get_age() method from class Dog with the required addition.
public void get_age() {
System.out.println("enter the age of the dog: ");
age = scan.nextInt();
scan.nextLine(); // I added this line.
}
so I'm trying to use a switch so that when i click on a range of 1 to 4, each one is directed to a class and performs its function. The choice "1" should asks for the user to input the id, name, other name and marks, then calculate it's average. The second class should then display all the information and I'm not sure how to do it.
Here is my main code:
public class lab3q1 {
public static void main (String args[]){
Scanner sc = new Scanner(System.in);
Entries entriesobject = new Entries(); //object declaration
display displayobject = new display(); //object declaration
displayall displayallobject = new displayall(); //object declaration
sortdata sortdataobject = new sortdata();
System.out.println("1. Add new entries: ");
System.out.println("2. Display an entry: ");
System.out.println("3. Display all entries: ");
System.out.println("4. Sort Data: ");
System.out.println("5. Exit: ");
int s = sc.nextInt();
switch(s){
case 1:{
if(s==1)
try{
entriesobject.method0();
}
catch(Exception e){
System.out.println("You can't do that");
}
}
case 2:{
if(s==2){
try{
displayobject.method();
}
catch(Exception e){
System.out.println("You can't do that");
}
}
}
case 3:{
if(s==3){
try{
displayallobject.method2();
}
catch(Exception e){
System.out.println("You can't do that");
}
}
}
case 4:{
if(s==4){
try{
sortdataobject.method3();
}
catch(Exception e){
System.out.println("You can't do that");
}
}
}
case 5:{
if(s==5){break;}
}
}
}
}
Here is the first class:
public class Entries {
public void method0(){
int total=0,total2;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the student id: ");
int id = sc.nextInt();
sc.nextLine();
System.out.println("Enter the student name: ");
String name = sc.nextLine();
System.out.println("Enter the student other names: ");
String othername = sc.nextLine();
for(int i=1;i<=4;i++){
System.out.println("Enter the student marks" +(i));
int mark = sc.nextInt();
total += mark;
total2 =total/4;
System.out.println("The average marks is: "+total2);
}
}
}
And here is my second class:
public class display {
public void method() {
int n;
Scanner sc = new Scanner(System.in);
System.out.println("Here is the student id: ");
}
}
As you can see i can't seem to link them.
To answer your question: when different classes want to know about data in other classes (aka fields of other objects), you need ways to access that, like:
public class Student {
private int id;
... methods to put a value into id
public int getId() { return id; }
and then some other class that has one more Student objects can do.
Beyond that: you are getting your "separation of concerns" wrong. You should have one class that uses a scanner to "collect" data from the user. This class creates various other objects; and puts the data from the user into those objects.
All your other classes do not have / need a scanner object. They get their data, for example as parameters to their constructor.
System.out.println("The id is: " + someStudentObject.getId());
I am new to Java and have recently started coding within the last week. I have tried to build some basic things and did the following:
import java.util.Scanner;
public class App {
public static void main(String[] args){
// creating scanner object
Scanner userSex = new Scanner(System.in);
System.out.println("Enter your sex (male or female): ");
String sex = userSex.nextLine();
System.out.println("Thank you, you entered " + sex );
// new scanner
Scanner userAge = new Scanner (System.in);
System.out.println("Are you a child or adult: ");
String age = userAge.nextLine();
System.out.println("You are a " + sex + " " + age);
if (userAge.equals("child")) {
System.out.println("children");
} else if (userAge.equals("adult")) {
System.out.println("adults");
}
}
}
Unfortunately however, only the top of the code runs. The below code doesn't run and doesn't print anything out even when I enter "child" or "adult".
if (userAge.equals("child")) {
System.out.println("children");
} else if (userAge.equals("adult")) {
System.out.println("adults");
}
instead of
if(userAge.equals("child")) {
System.out.println("children");
}
else if(userAge.equals("adult")) {
System.out.println("adults");
}
do this
if(age.equals("child")) {
System.out.println("children");
}
else if(age.equals("adult")) {
System.out.println("adults");
}
The one thing that was already mentioned is that you only need one scanner object. It's unnecessary to create one for each string entered. The main problem is that you are testing if userAge equals "child" or "adult", but userAge is the scanner object. I think you meant to write age.equals("child"), as age is the actual String entered.
The following works:
import java.util.Scanner;
public class App {
public static void main(String[] args){
//creating scanner object
Scanner in = new Scanner(System.in);
System.out.println("Enter your sex (male or female): ");
String sex = in.next();
System.out.println("Thank you, you entered " + sex );
//new scanner
System.out.println("Are you a child or adult: ");
String age = in.next();
System.out.println("You are a " + sex + " " + age);
if(age.equals("child")) {
System.out.println("children");
}
else if(age.equals("adult")) {
System.out.println("adults");
}
}
}
Rather than creating two separate scanners you just use one and call the next()method. I also change the condition from if(userAge... to if(age...
There is no need to create two scanner object .Just create one scanner object.
You can also use BufferedReader instead of Scanner.In your code you use userAge.equals("child") .but this is not right since you store the value returned by scanner in age , use age to compare in if-else codition like age.equals("child")
Scanner scanner = new Scanner(System.in);
System.out.println("Enter your sex (male or female): ");
String sex = scanner.nextLine();
System.out.println("Thank you, you entered " + sex );
System.out.println("Are you a child or adult: ");
String age = scanner.nextLine();
System.out.println("You are a " + sex + " " + age);
if (age.equals("child")) {
System.out.println("children");
} else if (age.equals("adult")) {
System.out.println("adults");
}
Reference Link:https://docs.oracle.com/javase/tutorial/essential/io/scanning.html
I am making a game-ish type of thing with three classes, combined. NOT HOMEWORK; hobby.
Codes for three classes:
Runner:
public class CounterGameRunner
{
// instance variables - replace the example below with your own
public static void main(String [] args){
Scanner input = new Scanner(System.in);
CounterGameCounter game = new CounterGameCounter();
System.out.println("You want to play a game I see. What is your name?");
String name = input.next();
game.NameIn(name);
CounterGAME game1 = new CounterGAME();
game1.actual();
}
}
Actual Game:
public class CounterGAME
{
// instance variables - replace the example below with your own
Scanner input = new Scanner(System.in);
int number;
int count=1;
boolean loop = true;
public CounterGAME(){
}
public void actual(){
CounterGameCounter game2 = new CounterGameCounter();
System.out.println("Guess a number between 1 and 101, see how many times you get it!");
number=input.nextInt();
int r = (int)(Math.random() * (100) + 1);
while(loop==true){
if(number < r){
System.out.println("Too small, try again");
number = input.nextInt();
count++;
game2.Counter(count);
} else if(number == r){
System.out.println("Wow, you won! Who'd have thought?");
count++;
game2.Counter(count);
break;
System.out.println(game2.done());
} else if(number > r){
System.out.println("Too large, try again");
number = input.nextInt();
count++;
game2.Counter(count);
}
}
}
}
Counter Class:
public class CounterGameCounter
{
// instance variables - replace the example below with your own
private String Name;
String done1;
int correct;
public CounterGameCounter(){
}
public String NameIn (String nm){
Name = nm;
return Name;
}
public String NameOut(){
return Name;
}
public void Counter(int count){
correct = count;
}
public int getCount(){
return correct;
}
public String done(){
done1 = "Name: " + NameOut() + "\n" +
"Times Answered: " + getCount();
return done1;
}
}
Problem:
The counter works properly and everything else displays and functions properly in the end. However, any name I input in the beginning always shows "null" while running the program. Why?
Your variable names are really confusing, and there are a lot of bad practices in your code, but null in name is because you create a new Counter in CounterGAME:
public void actual(){
// here
CounterGameCounter game2 = new CounterGameCounter();
// more code
}
Change actual to receive a CounterGameCounter:
public void actual(CounterGameCounter game2){
// more code
}
And call it like:
public static void main(String [] args){
Scanner input = new Scanner(System.in);
CounterGameCounter game = new CounterGameCounter();
System.out.println("You want to play a game I see. What is your name?");
String name = input.next();
game.NameIn(name);
CounterGAME game1 = new CounterGAME();
game1.actual(game);
// more stuff
}
FREE TIPS:
use String getName() and void setName(String)
start variable, object and attribute names with lowercase
String name;
Object object;
Variable names must be representative and descriptive
CounterGameCounter counterGameCounter = new CounterGameCounter();
This is also applicable to Object names:
GameCounter gameCounter = new CounterGameCounter();
try this:
String name = input.nextLine();
instead of:
String name = input.next();
Here is my code. I did my work a bit off. I was supposed to not just do one applicant, I was supposed to do many and I was supposed to populate the applicants into 2 different arrays, one for graduates, and one for undergraduates. I need help figuring out how to make an empty array and fill up applicants in those. A start would help
import java.util.Scanner;
public class CollegeApplicant
{
String applicantName;
String collegeName;
public String getApplicantName()
{
return applicantName;
}
public void setApplicantName(String applicantName)
{
this.applicantName = applicantName;
}
public String getCollegeName()
{
return collegeName;
}
public void setCollegeName(String collegeName)
{
this.collegeName = collegeName;
}
public void checkCollege()
{
int choice;
Graduate g = new Graduate();
Undergraduate ug = new Undergraduate();
Scanner input = new Scanner(System.in);
System.out.println("1. Undergraduate");
System.out.println("2. Graduate");
System.out.println("enter your choice ");
choice = input.nextInt();
if (choice == 1)
{
System.out.println("Enter SAT marks :");
ug.SAT = input.nextDouble();
System.out.println("Enter GPA marks :");
ug.GPA = input.nextDouble();
}
else
{
System.out.println("Enter the origin of college:");
g.collegeOrigin = input.next();
System.out.println("Status :" + g.checkCollege());
}
}
public class Undergraduate
{
double SAT;
double GPA;
}
public class Graduate
{
String collegeOrigin;
String checkCollege()
{
String information = null;
if (collegeName.equals(collegeName)) information = "Applicant is applying from inside";
else information = "Applicant is applying from outside";
return information;
}
}
}
The Client
import java.util.Scanner;
public class CollegeApplicantClient extends CollegeApplicant
{
public static void main(String[] args)
{
TestCollege tc = new TestCollege();
Scanner input = new Scanner(System.in);
System.out.println("Enter the name of applicant ");
tc.setApplicantName(input.next());
System.out.println("enter the college name of applicant ");
tc.setCollegeName(input.next());
tc.checkCollege();
}
}
Here is an example of the possible output
Name: Joe
College Applied to: Harvard
SAT: 5
GPA: 2
Name: Tom
College Applied to: Yale
College of Origin: NYU – from outside
I need to be able to put the graduates in an array and the undergrads in an array. Then display it when it's all done