Jump to content

Java: Constructor

Go to solution Solved by Bartholomew,
6 minutes ago, Hip said:

This works fine now ? 

But what is the difference between %f and %.2f? I see that with %f it shows more digits behind the ","

Yes, specifies the digits after "." (Or after "," depending on locale used for output formatting :))

Hey guys,

 

I am learning java right now and I have a question regarding the constructor.

The code I have written is similar to a code I have seen in a tutorial on YouTube, but mine doesn't work somehow.

 

First question is, is it enought o put both files in the same folder? Or do I have to link the constructor file somehow to the main file?

Second question what does "%s" stand for? Is it like "take the first variable"?

I get this error message: 

Quote

E:\Java_Codes\Car_Constructor>java Car
The model Interior@4aa298b7, has Exception in thread "main" java.util.MissingFormatArgumentException: Format specifier '%s'
        at java.util.Formatter.format(Unknown Source)
        at java.io.PrintStream.format(Unknown Source)
        at java.io.PrintStream.printf(Unknown Source)
        at Car.main(Car.java:10)

 

Main file:

import javax.swing.*;
import java.util.*;

public class Car
{
	public static void main(String[] args)
	{
		Interior standard = new Interior("economy", 5, "leather",  19999.95);
		
		System.out.printf("The model %s, has %s seats made of %s and costs %s", standard);
		
	}
}

 

Constructor file:

import javax.swing.*;
import java.util.*;

public class Interior
{
	//declaration
	
	String  type, material;
	int seats;
	double price;
	
	Interior(String b, int s, String m, double p)
	{
		type = b;
		seats = s;
		material = m;
		price = p;
	}
}

 

Link to comment
https://linustechtips.com/topic/1054718-java-constructor/
Share on other sites

Link to post
Share on other sites

11 minutes ago, Hip said:

Hey guys,

 

I am learning java right now and I have a question regarding the constructor.

The code I have written is similar to a code I have seen in a tutorial on YouTube, but mine doesn't work somehow.

 

First question is, is it enought o put both files in the same folder? Or do I have to link the constructor file somehow to the main file?

Second question what does "%s" stand for? Is it like "take the first variable"?

I get this error message: 

 

Main file:


import javax.swing.*;
import java.util.*;

public class Car
{
	public static void main(String[] args)
	{
		Interior standard = new Interior("economy", 5, "leather",  19999.95);
		
		System.out.printf("The model %s, has %s seats made of %s and costs %s", standard);
		
	}
}

 

Constructor file:


import javax.swing.*;
import java.util.*;

public class Interior
{
	//declaration
	
	String  type, material;
	int seats;
	double price;
	
	Interior(String b, int s, String m, double p)
	{
		type = b;
		seats = s;
		material = m;
		price = p;
	}
}

 

%s is "format the Nth argument passed after the format string as a string and insert it here"

Nth because if you specify more % "markers" progressivly more arguments need to be passed.

 

Also, you now pass a object as arguments, you want the members in this case (if they are accesible depends if the classes are in the same package).

 

So (more or less) youd want:

 

System.out.printf("The model %s, has %d seats made of %s and costs %.2f", standard.model, standard.seats, standard.material, standard.price );

 

The various % things are formatting specifyers and their type and order relate to the arguments passed to "fill" them.

 

Tip; use getters and setters instead of direct member access and specify access level to members (private, protected or public).

 

Hope this gets you started and gives you some new keywords to find info on.

 

Link to comment
https://linustechtips.com/topic/1054718-java-constructor/#findComment-12482178
Share on other sites

Link to post
Share on other sites

As long as they are in the same folder you don't have to do any extra work to link them.

 

%s means put a string here. Others like %d and %f mean put a decimal/float here instead. That string/decimal/float is then expected to be placed as another argument after the format string.

 

There are 2 things wrong here:

  1. The main problem is your string format requires 4 string arguments but you only provide 1. So it should be more like
    System.out.printf("The model %s, has %s seats made of %s and costs %s", a, b, c, d);
  2. Secondly interior is not a string so you will not get an expected output. I'm guessing that would want to use the variables within interior so you should pass things like standard.type, standard.material, etc

 

Also note only this part is the constructor:

Interior(String b, int s, String m, double p)
	{
		type = b;
		seats = s;
		material = m;
		price = p;
	}

It is the constructor that initializes the object "Interior"

Link to comment
https://linustechtips.com/topic/1054718-java-constructor/#findComment-12482185
Share on other sites

Link to post
Share on other sites

On 4/14/2019 at 11:21 PM, Bartholomew said:

 

 

System.out.printf("The model %s, has %d seats made of %s and costs %.2f", standard.model, standard.seats, standard.material, standard.price );

 

Thank you for the info, I have changed the code but it still doesn't work correctly.

I have found the  mistake I made, I have compiled the wrong file. lol

Thank you for your help so far.

I have one last question, can I use %.2f and %f as the same type? is there a difference?

@mail929

import javax.swing.*;
import java.util.*;

public class Car
{
	public static void main(String[] args)
	{
		Interior standard = new Interior("economy", 5, "leather",  19999.95);
		
		System.out.printf("The model %s, has %i seats made of %s and costs %d", standard.type, standart.seats, standart.material, standart.price);
		
	}
}

I get this error message:

 

Exception in thread "main" java.util.UnknownFormatConversionException: Conversion = 'i'
        at java.util.Formatter$FormatSpecifier.conversion(Unknown Source)
        at java.util.Formatter$FormatSpecifier.<init>(Unknown Source)
        at java.util.Formatter.parse(Unknown Source)
        at java.util.Formatter.format(Unknown Source)
        at java.io.PrintStream.format(Unknown Source)
        at java.io.PrintStream.printf(Unknown Source)
        at Car.main(Auto.java:12)

 

Link to comment
https://linustechtips.com/topic/1054718-java-constructor/#findComment-12490966
Share on other sites

Link to post
Share on other sites

5 minutes ago, Hip said:

Thank you for the info, I have changed the code but it still doesn't work correctly.


import javax.swing.*;
import java.util.*;

public class Car
{
	public static void main(String[] args)
	{
		Interior standard = new Interior("economy", 5, "leather",  19999.95);
		
		System.out.printf("The model %s, has %i seats made of %s and costs %d", standard.model, standart.seats, standart.material, standart.price);
		
	}
}

I get this error message:

 


Exception in thread "main" java.util.UnknownFormatConversionException: Conversion = 'i'
        at java.util.Formatter$FormatSpecifier.conversion(Unknown Source)
        at java.util.Formatter$FormatSpecifier.<init>(Unknown Source)
        at java.util.Formatter.parse(Unknown Source)
        at java.util.Formatter.format(Unknown Source)
        at java.io.PrintStream.format(Unknown Source)
        at java.io.PrintStream.printf(Unknown Source)
        at Car.main(Auto.java:12)

 

%d (decimal integer) for the integer not %i

 

Im assuming the "standart" vs "standard"in the last 3 arguments is not in the code running since itd be compile time error? 

 

Link to comment
https://linustechtips.com/topic/1054718-java-constructor/#findComment-12490998
Share on other sites

Link to post
Share on other sites

10 minutes ago, Bartholomew said:

%d (decimal integer) for the integer not %i

 

Im assuming the "standart" vs "standard"in the last 3 arguments is not in the code running since itd be compile time error? 

 

This works fine now ? 

But what is the difference between %f and %.2f? I see that with %f it shows more digits behind the ","

Link to comment
https://linustechtips.com/topic/1054718-java-constructor/#findComment-12491042
Share on other sites

Link to post
Share on other sites

6 minutes ago, Hip said:

This works fine now ? 

But what is the difference between %f and %.2f? I see that with %f it shows more digits behind the ","

Yes, specifies the digits after "." (Or after "," depending on locale used for output formatting :))

Link to comment
https://linustechtips.com/topic/1054718-java-constructor/#findComment-12491072
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×