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 6 years ago.
Improve this question
public class Book
{
String bookName;
public static void main(String[] args)
{
Book Object = new Book();
Object.bookName = "Network Technology Design";
System.out.println("The book named", Object.bookName);
}
}
As you see, the
System.out.println("The book named", Object.bookName);
is wrong,but if I do that
System.out.println(Object.bookName);
No any error, why?
You need to concatenate the Strings with a "+", because System.out.println() only takes one parameter.
You have to do it like this:
System.out.println("The book named " + Object.bookName);
If you see the PrintStream class,then you can find that there are no such method println which accepts 2 arguments.
So
System.out.println("The book named", Object.bookName); is wrong and System.out.println(Object.bookName); is right
System.out.println expected String, and you try to pass additional paramters. As menthioned in comments, use string concatenation or foramt function
System.out.println(String.format("The book named %s", Object.bookName))
System.out.println() takes only one parameter - Any primitive data type or Object type. Need to make it one argument by concatenating or combining..!
PrintStream class overloadingprintln() method depending on arguments type it call the method and it have only one argument or no argument.
like println(), println(String x), println(int x) etc
Related
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 4 years ago.
Improve this question
In the code below, the method onOrOff() must be printed to the console, stating engine is off. However, this specific method will not print.
I have tried System.out.println("engine is off");, but an error occurs stating “missing return statement”.
What is the main reason why the method onOrOff() is not shown in the output?
public static void main(String[] args) {
Vehicle truck = new Vehicle(200, 8, "red", 5);
System.out.println(truck.size);
System.out.println(truck.wheels);
System.out.println(truck.color);
System.out.println(truck.numbofGears);
onOrOff();
}
Vehicle(int size, int wheels, String color, int numbofGears) {
this.size=size;
this.color=color;
this.wheels=14;
this.numbofGears=numbofGears;
}
static String onOrOff() {
return Engine is off;
}
Your onOrOff() method returns a String and you're calling this method from main, yet you're not doing anything with the returned String. Try the following:
System.out.println(onOrOff());
UnholySheep already answered the question; but I will try to add a bit more details:
Engine is off is not a string. Also you need to print the return value of the method.
Erratum: Nathan Hughes did too
use double quotes, like this: return "Engine is off";
If you want a String, you have to surround the word(s) with double quotation marks, for example: "Hello World", or Foo.
So, in your example, you will have to use return "Engine is off" instead; return Engine is off by itself won't work.
Unrelated question, but why in the Vehicule constructor, you have a wheels parameter, but you give the associated attribute another value (14) regardless of the parameter?
Is that only temporary?
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 7 years ago.
Improve this question
I was trying to convert an array to a list and vice-versa and perform different methods of array and list respectively using the below code.
import java.util.*;
public class LinkedList{
public static void main(String[] args){
String[] things1 = {" seminar", " presentations", " hackathon", " movies"};
LinkedList<String> theList = new LinkedList<String>(Arrays.asList(things1));
theList.add("tickets");
theList.addFirst("Hellow");
things1 = theList.toArray(new String[theList.size()]);
for(String x: things1)
System.out.printf("%s", x);
}
}
Now, the problem is that when I am running it from NetBeans it works correctly, but when I am trying it in normal text editor and running through terminal it gives an error:
LinkedList.java:10: error: type LinkedList does not take parameters
LinkedList<String> theList = new LinkedList<String>(Arrays.asList(things1));
^
LinkedList.java:10: error: type LinkedList does not take parameters
LinkedList<String> theList = new LinkedList<String>(Arrays.asList(things1));
^
2 errors
I think the list should take parameter but the error is completely opposite.
Why is it so?
You import
java.util.*
And since there's a java.util.LinkedList in the standard libraries NetBeans seems to pick that one up instead while javac from the JDK uses your own LinkedList which doesn't take parameters (such as String in LinkedList<String). To fix this problem you can do one of the following
Rename your class
Wherever you use a LinkedList you put the full name such as com.myproject.LinkedList or java.util.LinkedList to make it explicit which one you want (replace com.myproject with the actual package that your own linked list is in)
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 7 years ago.
Improve this question
public class StringDemo
{
public static void main(String [] args)
{
String s = "Sachin";
s.concat(" Tendulkar");
s.toLowerCase();
System.out.print(s);
}
}
This example giving output as : Sachin
then how many objects have been created?
The answer is: an indeterminate number.
on the face of it, the two operations on s each create a single String object,
two more String objects are created at load time to represent the literals,
objects may be created when print is called: internally to the System.out PrintWriter and the stream stack that it wraps,
each String object may (or may not) have a distinct char[] object inside it,
it is possible that the operations on s could be optimized away, since they actually have no effect on the output of the program (!!),
when the application is called, it will be passed a String[] argument, potentially populated with multiple String, and (finally),
an arbitrary number of objects will be created during JVM bootstrapping and class loading ... prior to the application starting.
So, depending on what objects you count, how you count them, and the other assumptions that you make, the answer could be some number from zero to a very large number of objects.
Note: the normal quiz answer for this would be "2 Strings are created", but as you can see the answer is a lot more complicated than that.
Note 2: the concat and toLowerCase methods do NOT create strings in the string pool. In fact, the only String operation that puts strings into the pool is intern. (It is easy to verify this experimentally, or by reading the Java class library source code.)
String in java is a immutable type.
s.concat(" Tendulkar");
s.toLowerCase();
these 2 lines return 2 distinct strings and doesn't affect the original string.
In java String is considered as immutable which means that it cannot be changed once its created, so if you count how many you have, on the first line you declared the first one, when you did s.concat("SE 6") you created a new object, and finally s.toLowerCase() created the 3rd object, therefore 3 string objects are created.
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 8 years ago.
Improve this question
public class project5{
public static void main(String args[]){
String[] storage = {"123457897", "123456","654654654"};
int current;
current = Integer.parseInt(storage[1].subString(1,5));
System.out.println(current);
}
}
So I'm trying to, as an exercise, just get the first 5 numbers in the first thing of the array and parse it as an integer and store it as the variable current. It gives me the error:
test.java:5: error: cannot find symbol
current = Integer.parseInt(storage[1].subString(1,5));
^
symbol: method subString(int,int)
location: class String
1 error
What is it that I'm doing wrong?
There is no subString method on the String class. There only is substring (all lowercase).
http://docs.oracle.com/javase/7/docs/api/java/lang/String.html
For variable, class and method names casing matters.
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 8 years ago.
Improve this question
I am trying to use a generic method to return a String value
This is the method call
System.out.printf("%s", impresionTabulada(arregloEntrada[i]),i);
This is the method
public static <E> String impresionTabulada(E elemento, int indx){
if(indx%4==0)
return String.format("%s\n", elemento);
else
return String.format("%s\t\t", elemento);
}
but the compiler throws this error
"Description Resource Path Location Type The method
impresionTabulada(E, int) in the type
EJERC18_6_PruebaMetodoGenerico_ImpresionTabulada is not applicable for
the arguments
(E) EJERC18_6_PruebaMetodoGenerico_ImpresionTabulada.java /1 libro de
java D&D/CAP18/com/ejercDietel/Ejercicios line 23 Java Problem"
I have been researching about this issue but i dont know what i am doing wrong.
Thanks in advance
You're calling the method with a single argument impresionTabulada(arregloEntrada[i]) but you defined it with two parameters.
I'm assuming the method call should be :
System.out.printf("%s", impresionTabulada(arregloEntrada[i],i));
Simplified Signature of impresionTabulada
String impresionTabulada(E, int)
It takes in 2 parameters, the first of type E, and the second of type int.
Substitution of impresionTabulada
// This is NOT code...this is just a substitution for later!
x = impresionTabulada(arregloEntrada[i]) // Take note that is only taking in 1 parameter - arregloEntrada[i]
Therefore...
Your printf expression is this...
System.out.printf("%s", x, i); // This `x` is from above
System.out.printf("%s", x , i); // This `x` is from above - expanded
System.out.printf("%s", impresionTabulada(arregloEntrada[i]), i); // This is your bad code!
As you can see, it is your printf(...) function that is taking the argument that you want as the second impresionTabulada(...) argument.
Solution
System.out.printf("%s", impresionTabulada(arregloEntrada[i], i) );