[Solved] Fixed Array?/ StringBuilder?/ String? which is best way to create a string, if 84 strings to be appended

If by “best” you mean “most memory and/or runtime efficient” then you’re probably best off with a StringBuilder you pre-allocate. (Having looked at the implementation of String.join in the JDK, it uses StringJoiner, which uses a StringBuilder with the default initial capacity [16 chars] with no attempt to avoid reallocation and copying.) You’d sum up … Read more

[Solved] edit object/arrayList in java again and again [closed]

I got the answer bu doing some simple arithmetic. Create another class to select random number for bellow cases: step1: Taking current student index step2: Randomly selecting one student, whom number should reduce, rather than step1 student continuing again step1 and step2 solved edit object/arrayList in java again and again [closed]

[Solved] Java code output is weird?

When you say System.out.println(person[0]); java doesn’t automatically know what you want printed out. To tell it, you write a method in your Examples class called toString() which will return a string containing the info you want. Something like: @Override public String toString() { return “Name: ” + name + ” Age: ” + String.valueOf(this.age) + … Read more

[Solved] If a class implements serializable interface then is its child class also serialize or not?

The Serializable interface does nothing by itself, it is merely, as you remarked, a marker interface to show Java that this particular class is serializable. That means that if you denote a class with this interface, all child classes will be treated as serializable by themselves (just like normal inheritance), but the task of a … Read more

[Solved] java dynamic class instance creation

If you need to dynamically create new objects and assign them to a variable, I would utilize a map with the key used to simulate naming a variable and the value as the newly created Chicken object: e.g. new HashMap<nameOfVariable, Chicken>() This will get you around not knowing the number or name of your instances … Read more

[Solved] Connecting 3 different java files

Let’s do a quick analysis of your code: Your Student object looks like a constructor for the Student class object type which is most likely in this case an inner class of the CollegeTester class. So here’s the deal, your addCommand() already connects your CollegeTester class with your Student class, by executing this command after … Read more

[Solved] Assign the value to the variable in Java

if(rain && temperature < 0) snow = true; But you also need to set snow = false by default, so: public class Q8 { boolean snowForecast() { boolean snow = false; int temperature=-5; boolean rain=true; if(rain && temperature < 0) snow = true; return snow; } } Right now, this is what your doing: 1) … Read more

[Solved] How to parse a simple text file in java

import java.io.BufferedReader; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; public class Read_Text_File { public static void main(String[] args) { System.out.println(getValues()); } public static ArrayList<String> getValues() { FileInputStream stream = null; try { stream = new FileInputStream(“src/resources/java_txt_file.txt”); } catch (FileNotFoundException e) { e.printStackTrace(); } BufferedReader reader = new BufferedReader(new InputStreamReader(stream)); String strLine; ArrayList<String> … Read more

[Solved] Java File Name Printing

Loop through both the ArrayLists you have. Each ArrayList contains the file names as it is. You’ll need a nested loop (a loop inside a loop). In the core of the nested loops, you want to do a compare between current position of each ArrayList. You can use .equals() method for this. The pseudo code … Read more

[Solved] how to read float array from command line in java? [closed]

I haven’t tested it, but wrote using Scanner docs Scanner sc = new Scanner(System.in); while(sc.hasNext()) { String next = sc.next(); if(“q”.equals(next)) break; float value = new Float(next); System.out.println(“Float: “+value); } 2 solved how to read float array from command line in java? [closed]

[Solved] How do I dynamically resize a text component in Java Netbeans, when the text has filled the component, instead of it having scroll bars?

How do I dynamically resize a text component in Java Netbeans, when the text has filled the component, instead of it having scroll bars? solved How do I dynamically resize a text component in Java Netbeans, when the text has filled the component, instead of it having scroll bars?