What does mean, lines.next.toInt in Java [closed] - java

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
What does mean this line in java:
m = lines.next.toInt
Thanks.

You can get this done in java using any reader. e.g- BufferedReader, FileReader anything like this.
public class InJava
{
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
//
//in main()
public static void main(String [] args)
{
Object obj= br.readLine();
}
}
If you are using a list use Iterator.
public class InJava
{
List<double> lst= new ArrayList<double>();
//fill the list.
Iterator it= lst.iterator();
//in main()
while(it.hasNext())
{
Object obj= it.next();
//Continue
}
}

lines is perhaps an iterable collection of Double objects. The code that you've posted accesses the next element in sequence and converts it to an integer.
If you can post the surrounding code as well, we could get more context on the same.

Related

Java list of string don't display all elements [closed]

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 5 days ago.
Improve this question
i'm trying to add elements in a list inside a method and after get it from another method.
Here is the code :
public class ChatMessageRepository {
List<String> msgs = new ArrayList<String>();
ChatMessageRepository addChatMessage(String message) {
msgs.add(message);
System.out.println(msgs);
return null;
}
List<String> getLastTenMessages() {
List<String> firstNElementsList = msgs.stream()
.limit(10)
.collect(Collectors.toList());
return firstNElementsList;
}
}
The problem is when I try to display the firstNElementsList it's alaway empty and also when i print the list from ChatMessageRepository Method it's display me only the last element
Any one have an idea guys ?

Check if my code is correct for the following problems given? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I am just learning Java.
And I want to improve my code and answer the question below.
3.Inside the main() method of Simulator , create an instance of a "Cat" object, and invoke that object's run() method.
Does my code require additional information? I want to answer the above question and this is what I have so far:
class Simulator {
public static void main(String[] args) {
Cat c = new Cat();
System.out.println(c);
}
}
Am I correct? I found the second class returns as an random integer. Will that work to invoke the run method.
There are so many things wrong...
"Code snippets" are for Javascript. Your program is Java. Java <> Javascript :(
Your "Cat" object implements a thread. You use threads for "concurrency", to do things "in parallel". Does your "meow" really merit spawning off a thread?
What about member "1"? You declare it. You initialize it. And then you fail to use it for ANYTHING. Q: Why bother?
System.out.println(c) prints the "object"; it doesn't print anything "meaningful".
Suggested modifications:
public class Cat
{
private int i;
public void meow() {
System.out.println("Meowing: " + i);
}
public Cat(int i) {
this.i = i;
}
public static void main(String args[]) {
Cat cat = new Cat (1);
cat.meow();
}
}

Java Enum Causing NullPointerException [closed]

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 5 years ago.
Improve this question
I have an enum and I am trying to iterate through it using a foreach and for some reason it is returning null on my first constant. I see nothing wrong with it.
First off, I am creating and initializing a hashmap to store the enums so that I can manipulate them to my heart's content.
public class ScriptLoader {
private ResourceLocation resloc;
private File file;
private FileReader fr;
private BufferedReader br;
//Here
private Map<String, Node> nodes;
Init here:
public void loadScript() throws IOException{
file = resloc.getFile();
fr = new FileReader(file);
br = new BufferedReader(fr);
//Here
nodes = new HashMap<>();
this.setNodes();
if(Engine.stateOfEngine == EnumEngineState.DEBUGGER_ON) {
Engine.LOGGER.log("\tScript Loaded!", EnumLoggerTypes.DEBUG);
}
}
Secondly, I am creating a method called setNodes() which will add the constants to the map in the parent class of setNodes() which is called ScriptLoader.
public void setNodes(){
for(EnumNodes node : EnumNodes.values()) {
nodes.put(node.getName(), node.getNode());
}
}
And I am calling it here:
public void loadScript() throws IOException{
file = resloc.getFile();
fr = new FileReader(file);
br = new BufferedReader(fr);
nodes = new HashMap<>();
this.setNodes();
if(Engine.stateOfEngine == EnumEngineState.DEBUGGER_ON) {
Engine.LOGGER.log("\tScript Loaded!", EnumLoggerTypes.DEBUG);
}
}
Now, I have an enum that I have a list of "nodes" that I want to iterate through, which can be seen in the setNodes() method.
Now the crash report is here. For some reason, it is pointing the ExceptionInInitializationError at the first enum constant, and the null pointer exception at the enum declaration. I didn't think that an enum declaration could return null.
From that error message, I'm guessing that something in your enum constructor is throwing an exception. Digging through your github a bit, I believe that the problem is in Node. It looks like the enums may be initialized by Java's classloader before start is called on an instance of Engine, causing Engine.getScriptLoader() to return null, and Node's constructor to throw a NullPointerException.

How to debug an if statement that is not working? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 years ago.
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.
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.
Improve this question
import java.util.*;
public class ass {
public static void main(String[] args) {
// TODO Auto-generated method stub
String[]c={"sid","is","cool"};
List<String>list1=new ArrayList<String>();
for(String w:c){
list1.add(w);
}
String[]q={"is"};
List<String>list2=new ArrayList<String>();
for(String t:q){
list2.add(t);
}
EditList(list1,list2);
for(int i=0;i<list1.size();i++){
System.out.printf("%s ", list1.get(i));
}
}
public static void EditList(Collection<String>l1, Collection<String>l2){
Iterator<String>it=l1.iterator();
while(it.hasNext()){
if(l2.contains(it.next()));
it.remove();
}
}
}
In this programme I have two lists. I wanted to remove the items that are common in the first and second list from the first list and print it. I don't want a workaround or any other code suggestions. Can someone please explain why my code is not working?
I am following New Boston's tutorials.
Here:
if(l2.contains(it.next()));
it.remove();
That semicolon after if is a real statement.
Thus it.remove() happens always; like if ... that if not there!
Thus the real answer: always always always use
if (){
stuff
}
... even for single statements! Same for loops!
try
public static void EditList(Collection<String>l1, Collection<String>l2){
Iterator<String>it=l1.iterator();
while(it.hasNext()){
String current=it.next();
if(l2.contains(current)){
i1.remove(current); // assuming u wish to remove from l1
}
}
}

getSize() is undefined for Collection? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Here's the code in question (these methods are in the same class):
private int size;
public int getSize(){
return this.size;
}
public boolean addAll(Collection c) {
Iterator iter = c.iterator();
int i =0;
while(i < c.getSize()){
add(iter.next()); // This part isn't finished yet
i++;
this.size++;
}
I'm receiving the error where I called c.getSize(). The error is: c.getSize() is undefined for type Collection.
The method name is size(), not getSize(). See the docs.
In collection there is no method with name "getSize()" But you can use size() method to get the size of the collection.

Categories

Resources