Java and printf [closed] - java

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
The code is having some problem with class 2 line 12. Kindly help .
The code is in the image.
import java.util.Scanner;
public class first{
public static void main(String[] args){
Scanner b = new Scanner(System.in);
pro nc = new pro();
System.out.println("Enter the name of your first true crush");
String temp = b.nextLine();
nc.setname(temp);
nc.etc();
}
}
public class pro{
private String gname;
public String getName() {
return gname;
}
public void setName(String name) {
this.gname = name;
}
public void etc(){
System.out.printf("The name of your true crush was %s",getName());
}
}

In class 2 you have not specified the location of the function in System.out.printf(). You should use the this keyword and re-write the function as
public class pro {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void etc(){
System.out.printf("The carp is %s",this.getName());
}
}

there is no problem with this
I AM USING NETBEANS IDE.
/*
///
public class pro {
private String gname;
public String getname() {
return gname;
}
public void setname(String name) {
gname = name;
}
public void etc(){
System.out.printf("Tht name is %s",getname());
}
}
/////////
import java.util.Scanner;
public class first {
public static void main(String[] args) {
Scanner b= new Scanner(System.in);
pro nc = new pro();
System.out.println("enter the name");
String temp = b.nextLine();
nc.setname(temp);
nc.etc();
}
}
/////////
*/
Recheck this.
I have not changed your code, just typed it.
And yes I agree with Arc676 that people should post the code.

There is no problem on netbeans.
You can try to use System.out.format instead of System.out.printf.
import java.util.Scanner;
public class first{
public static void main(String[] args){
Scanner b = new Scanner(System.in);
pro nc = new pro();
System.out.println("Enter the name of your first true crush");
String temp = b.nextLine();
nc.setname(temp);
nc.etc();
}
}
public static class pro{
private String gname;
public String getName() {
return gname;
}
public void setName(String name) {
this.gname = name;
}
public void etc(){
System.out.format("The name of your true crush was %s", getName());
}
}

Related

how to create a List type of non public class from another package?

My Package A has one java file with 2 classes. Login class which is public and LoginDetails class which cannot be public because it is in the same file. how to create a List of LoginDetails type from Package B.
package A;
public class Login {
private String name;
private String passWord;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassWord() {
return passWord;
}
public void setPassWord(String passWord) {
this.passWord = passWord;
}
#Override
public String toString() {
return "Login [name=" + name + ", passWord=" + passWord + "]";
}
}
class LoginDetails{
public LoginDetails(int id, int geight) {
super();
this.id = id;
this.geight = geight;
}
private int id;
private int geight;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getGeight() {
return geight;
}
public void setGeight(int geight) {
this.geight = geight;
}
public void hidden() {
System.out.println("From hidden");
}
public LoginDetails() {
}
}
package B;
import java.util.ArrayList;
import java.util.List;
public class Demo {
public static void main(String[] args) throws Exception {
List<LoginDetails> l = new ArrayList<LoginDetails>();
}
}
A solution to your weird question which doesnt include changing neither of the Login nor LoginDetails classes would be by adding a second Public class called AUtils such like this:
AUtils/AFactory class
package A;
import java.util.ArrayList;
public class AUtils {
public static ArrayList<LoginDetails> generateList(){
return new ArrayList<LoginDetails>();
}
public static ArrayList<LoginDetails> generateListWithInitialSize(int x){
return new ArrayList<LoginDetails>(x);
}
public static LoginDetails generateAnObject(){
return new LoginDetails();
}
public static LoginDetails generateWithData(int id, int geight){
return new LoginDetails(id,geight);
}
}
And your Demo would look like this:
public class Demo {
public static void main(String[] args) {//plus you dont need To throw exception thus your program dont throw any!:)
List l = AUtils.generateList();
// List l = AUtils.generateListWithInitialSize(10);//will give you array list with initial size 10
l.add(AUtils.generateAnObject());//if you do so be aware that the objects would be created with 0 as id and eight.
// l.add(AUtils.generateWithData(3,3));
}
}
please be aware that this normally is not acceptable and considered as bad coding because its kinda turn around ;) so either you misunderstood the assignment or the one who wrote it is really a carrot.
happy coding.
You cannot do it directly without changing of the design or visibility of the classes.
If a class has no modifier (the default, also known as
package-private), it is visible only within its own package.
https://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html

How to convert to String ArrayList with another specification, ArrayList<Person> in java [duplicate]

This question already has answers here:
How do I print my Java object without getting "SomeType#2f92e0f4"?
(13 answers)
Closed 4 years ago.
package com.company;
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
ArrayList<Person> plot = new ArrayList<Person>();
plot.add(new Person("John"));
plot.add(new Person("Jane"));
RepkaStory.tell(plot);
}
private static class Person implements RepkaStory {
String name;
private Person(String name) { this.name= name; }
}
public interface RepkaStory {
static void tell(List<Person> a){
for (Person x : a ) {
System.out.println(x.toString);
}
}
}
}
but it gives as result
com.company.Main$Person#677327b6
com.company.Main$Person#1540e19d
and I can't convert it into String.
You should override toString method
public class Main {
public static void main(String[] args) {
ArrayList<Person> plot = new ArrayList<Person>();
plot.add(new Person("John"));
plot.add(new Person("Jane"));
RepkaStory.tell(plot);
}
private static class Person implements RepkaStory {
String name;
private Person(String name) { this.name= name; }
#Override
public String toString() {
return "Person [name=" + name + "]";
}
}
public interface RepkaStory {
static void tell(List<Person> a){
for (Person x : a ) {
System.out.println(x);
}
}
}
}
Or use System.out.println(x.name);

Java: Using a String as an instance variable in homework assignment.

I am having a bit of trouble getting started on this Java assignment, and was hoping to get some guidance from you guys. My issue is pretty simple and straightforward... How do I take the tester method's input and start using "Dave" as the new name? How do I make Dave the instance variable and how do I start using that in the setName() and greetCrewMember() methods? After that's done, how would I assign Aruna? The main thing tripping me up is the instance variable and calling it in the methods. Thanks for any help given!
public class Hal9000
{
private String name;
public static void main(String[] args)
{
Hal9000 hal = new Hal9000("Dave");
System.out.println(hal.greetCrewMember());
System.out.println("Expected: Welcome, Dave");
System.out.println(hal.doCommand("engage drive"));
System.out.println("Expected: I am sorry, Dave. I can't engage drive");
hal.setName("Aruna");
System.out.println(hal.doCommand("power down"));
System.out.println("Expected: I am sorry, Aruna. I can't power down");
}
public String getName()
{
}
public void setName(String newName)
{
String name = newName;
return name;
}
public String greetCrewMember()
{
String message = "Welcome," + name ;
return message;
}
public String doCommand(String whatToDo)
{
}
}
The only thing you have to do is to define a parameterised constructor-
Hal9000(String name)
{
this.name=name;
}
This will cause your statement-
Hal9000 hal = new Hal9000("Dave");
to execute correctly and set the name to Dave. After this you can set the name to anything else by your setname method.
You would also want to define something in your getname method.
You should use the field name within the constructor and use this to instantiate it within the setter and constructor.
Hal9000(String name) {
this.name = name;
}
further instantiate as
Hal9000 instanceForDave = new Hal9000("Dave"); // would set the 'name' for this instance as 'Dave'
Hal9000 instanceForAruna = new Hal9000("Aruna");
The setter implementation should ideally also make use of this name only, something similar to the constructor in your case as:
public void setName(String newName) {
this.name = newName;
}
and then when you need to fetch the name attribute of the class, the getter would be helpful as
public String getName() {
return this.name;
}
public class Hal9000
{
private String name;
public Hal9000(String[] stringArray) {
name = stringArray[0];
}
public static void main(String[] args)
{
String[] stringArray = { "Dave"};
Hal9000 hal = new Hal9000(stringArray);
System.out.println(hal.greetCrewMember());
System.out.println("Expected: Welcome, Dave");
System.out.println(hal.doCommand("engage drive"));
System.out.println("Expected: I am sorry, Dave. I can't engage drive");
hal.setName("Aruna");
System.out.println(hal.doCommand("power down"));
System.out.println("Expected: I am sorry, Aruna. I can't power down");
}
public String getName()
{
return this.name;
}
public void setName(String newName)
{
this.name = name;
}
public String greetCrewMember()
{
String message = "Welcome," + getName() ;
return message;
}
public String doCommand(String whatToDo) {
return whatToDo;
}
}

Avoiding main method static definition? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
The Person.setPhoneNumber is saying
Main method is not static in class PersonTest, please define the main method as:
public static void main(String[] args)
but I cannot define in Person class the string PersonName as static. What should I do then?
Person Class:
public class Person {
private String name;
private int age;
private String phoneNumber;
public Person(String n, int a){
name = n;
age = a;
phoneNumber = null;
}
public String getName(){
return name;
}
public void setname(String n){
this.name = n;
}
public int getAge(){
return age;
}
public void setAge(int a){
age = a;
}
public String getPhoneNumber(){
return phoneNumber;
}
public void setPhoneNumber(String pn){
this.phoneNumber = pn;
}
public String toString() {
return "Person {name=" + name +", age= " + age +", phone number =" + phoneNumber+ "}";
}
}
Person Test:
public class PersonTest {
public void main(String[] args){
Person person1 = new Person("Joel.Z", 20);
Person.setPhoneNumber("8324193601");
Person person2 = new Person("Fred Werd", 84);
Person.setPhoneNumber("585275333");
System.out.println(person1);
System.out.println(person2);
}
}
The main method should be static:
public static void main(String[] args)
Also, you should call you methods on an instance, not on a class:
public class PersonTest {
public static void main(String[] args){
Person person1 = new Person("Joel.Z", 20);
person1.setPhoneNumber("8324193601");
Person person2 = new Person("Fred Werd", 84);
person2.setPhoneNumber("585275333");
System.out.println(person1);
System.out.println(person2);
}
}
As #ΔλЛ says is correct, your main method must be static, also keep in mind that you must use the instance of the created object, it means:
Here you are creating person1 object:
Person person1 = new Person("Joel.Z", 20);
So, you must use that object to access the attribute:
person1.setPhoneNumber("8324193601");
instead of Person.setPhoneNumber("8324193601"); In the same way with the second object: person2.setPhoneNumber("585275333"); instead of Person.setPhoneNumber("585275333");

How to call String value one class to another class in Java

public class Sample1 {
public String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public static void main(String[] args) {
Sample1 s1= new Sample1();
s1.setName("Abc");
}
}
public class Sample2 {
public static void main(String[] args) {
Sample1 n2= new Sample1();
System.out.println(n2.getName());
}
}
I have two classes Sample1 and Sample2 two. I am allocating string value using setter method and returning in another class by using getter method, but an output is null. Why null is an output and how to get string value from one call to another class?
I think you misunderstood the main method, maybe I am wrong, however only one main method is executed.
If you run Sample2.main - on Sample1 you are not setting a name so it is null (Sample1.main is never executed).
If you run Sample1.main - Sample1 is created and assigned a name.
So either assign the name in the Sample2.main:
public static void main(String[] args) {
Sample1 n2= new Sample1();
n2.setName("xxx");
System.out.println(n2.getName());
}
or do it via constuctor.
public class Sample1 {
private final String name;
public String getName() {
return name;
}
public Sample1(String name) {
this.name = name;
}
}
Consider the code :
Sample1 n2= new Sample1();
System.out.println(n2.getName());
Here the Name is not set , So you need to set the name before getting the name.
Sample1 n2= new Sample1();
n2.setName("name goes here");
System.out.println(n2.getName());
Also, you can try parameterized constructor in the Sample1 class and access like in sample2 class:
Sample1 n2= new Sample1("your name goes here");
System.out.println(n2.getName());
The constructor will be :
public Sample2(String name){
this.name = n;
}
3 thing you can add method in Sample1 class and access it in Sample2 class.
I don't want to set String value in Sample2 class, need to assign string value in Sample1 only, after that i need that string value in Sample2 class
public class Sample1 {
private String _name;
public String getName() {
return _name;
}
private setName(String name) {
_name = name;
}
public SetNameHelper(){
setName("somestring");//You will be setting the name in Sample 1
}
}
public class Sample2 {
public static void main(String[] args) {
Sample1 n2= new Sample1();
n2.SetNameHelper();
System.out.println(n2.getName());//You will be getting Name in Sample 2 class
}
}
class Sample2 {
Sample1 sample1;
Sample2(Sample1 sample){
this.sample1 = sample;
}
private String getSample1Name() {
return this.sample1.getName();
}
public static void main(String[] args) {
Sample1 sample1 = new Sample1();
sample1.setName("Abc");
Sample2 n2= new Sample2(sample1);
System.out.println(n2.getSample1Name());
}
}
I think I understand you confusion: you mistake a main function for a constructor!
I noticed that you created a main function in each class, whose only role is to create an instance and set the internal fields of the class. It's probably a Constructor you were looking for.
Here's the deal:
A main method is the entry point of a program. It just stays, to run me: begin executing this code. You probably (99.9% of the case) need only one main method per project.
A Constructor is a method which creates an instance of, each class, i.e. an object you can manipulate elsewhere in your code. Read up on Object-Oriented Programming
So here is your example, fixed (I believe), in a way that can bring sample1 value into an sample2, as you say:
public class Sample1 {
public String name;
public String Sample1(String initialName){
this.name = initialName;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public class Sample2{
public String name;
public String Sample2(String initialName){
this.name = initialName;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public class ProgramEntryPoint{
public static void main(String[] args) {
Sample1 s1 = new Sample1("a name");
System.out.println("Initial sample1 name: " + s1.getName());
s1.setName("a New Name!");
System.out.println("New sample1 name: " + s1.getName());
Sample2 s2 = new Sample2(s1.getName());
System.out.println("Initial sample2 name: " + s2.getName());
}
}
Which, once you run ProgramEntryPoint.main, will print:
Initial sample1 name: a name
New sample1 name: a New Name!
Initial sample2 name: a New Name!
This is all simple Java stuff, you should read up a few basic tutorials (the ones on oracle website are a good start)

Categories

Resources