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 8 years ago.
Improve this question
I know how to create arrays like so:
int[] myIntArray = new int[]{1,2,3};
My question is this. What if I had a file, titled Lab11Input.txt, and filled it with integer values. How would I go about creating a method that opens the file, counts the number of numbers in the file, creates the array, then fills the array with the values from the file?
For example, if I pass in Lab11Input.txt as an argument, could I do it that way?
A simple way using Scanner:
Scanner sc = new Scanner(new File ("Lab11Input.txt"));
List<Integer> ints = new ArrayList<>();
while(sc.hasNextInt()) {
ints.add(sc.nextInt());
}
// then you can convert ints to an array
Related
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 1 year ago.
Improve this question
I know that the () in ArrayList<Integer> = new ArrayList<>() can be used to describe the capacity for the ArrayList, but recently I've seen people put ArrayLists in the () of other ArrayLists.
I wasn't able to find the answer from searching on Google multiple times, any ideas?
This is a call to the ArrayList(Collection<T>) constructor. It creates a new array list with the elements of the Collection (e.g., another ArrayList) being passed to the constructor.
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'm completely new to java and coding in general and I need to make a program display certain text using different fonts each time randomly.
This question should be split into two parts. How can I list all available font family names in Java? and How to pick a random index of an array?. You should always try to split your problems into separate smaller sub-problems.
Here are the answers to both problems: A and B.
I made a neat function out of it which picks you the name of a random font family. If the language of the text that should be rendered is not English, replace Locale.ENGLISH with your language.
private static final Random RANDOM = new Random();
private static String pickRandomFontFamily() {
String[] availableFonts = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames(Locale.ENGLISH);
return availableFonts[RANDOM.nextInt(availableFonts.length)];
}
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 3 years ago.
Improve this question
I am stuck on some homework as i am new to java and still learning. I am wondering if it is possible to find a word in a .txt file and output the line that the word is on. I also need to allow a user to make a choice based on what is displayed back.
Example :
Word is Details
Txt file contains
Details on lion
Details on tiger
Output : "Details on tiger"
Thank in advanced for any help
This question has been answered before, but anyway You can go with this apporach.
Simply put:
Create a Scanner object and pass the required file into the
constructor as a new file object.
Iterate over the file with a
while loop until you find the specified string.
In order to store the lines that contains the desired word,we decalre a new string variable
Here's the code snippet:
Scanner scanner= new Scanner(new File("filename.txt"));
String lines = "";
while(scanner.hasNextLine()){
String stringLine = scanner.nextLine();
if(stringLine.indexOf("YOUR_WORD") != -1){
//print whatever you want here
System.out.println(stringLine);
//add every line that contains stringLine into another string;
lines+=stringLine;
}
}
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 8 years ago.
Improve this question
I would like to have an array which holds a list of arrival times of trains like this
Arrival Times Array:
9.15
10.34
12.10
6.15
What would be the correct way to do it and i also want to sort them
Use class java.util.Date. The array declaration will look like
Date[] myDates = new Date[n];
where n is number of elements you need.
You can also create arraylist like below snippet
import java.util.*;
List<Calendar> dates = new ArrayList<Calendar>(n); // initial size if not given it will grow dynamically.
dates.add( Calendar.getInstance() );
for you case to simple hold the time of the trains you can use any arrays of the float or double like below
float floatArray[] = new float[5]; //if size is fixed or else use arraylist or
double doublearray[]=new double[5];
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I was asked to use an array to create a phonebook which would read in a text file.
However I am having trouble each line on the text file into my object array.
so my solution was to create an array of type String then make my object array = to string array... but thats the problem.
Is there any way of my directly using the Scanner class to read in my text and save it into an object array as Scanner class's next() method is type string... and my array is type object... ?
thanks.
Try:
List<String> lines = new ArrayList<>();
while(scanner.hasNextLine()) {
lines.add(sc.nextLine());
}
String[] array = lines.toArray(new String[lines.size()]);
ArrayList is the key All you need to do is convert your Strings to arrayLists and then you can add or pop the elements more easily