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
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 2 years ago.
Improve this question
I was trying to find how to write/read arrays from my Firebase and I ended up with this. When I write I do:
String RestaurantIDs = "";
for(String RestaurantID : category.getRestaurantIDs()) { RestaurantIDs += RestaurantID + ","; }
DataToSave.put(CategoryFireBase.RestaurantIDs, "[" + RestaurantIDs + "]");
And when I read I do:
ArrayList<String> restaurantIDs = new ArrayList<>();
String[] Temp_restaurantIDs = document.get(CategoryFireBase.RestaurantIDs).toString().replace("[","").replace("]","").split(",");
for (String value : Temp_restaurantIDs){restaurantIDs.add(value);}
And I have 2 questions about it.
Is it ok that I'm saving it as long string instead of arrays?
If there is a way to read and write the array as an actual array, is it better then this?
Is it ok that I'm saving it as long string instead of arrays?
There is nothing wrong with that but it's more convenient to use arrays. For example, if you have in the database an array of restaurant ids, when you make a get() call, you can get that arrays directly as a list, without the need to split that long String.
If there is a way to read and write the array as an actual array, is it better then this?
Is it better because you can use specialized methods to update or remove elements within an array, as explained in the official documentation:
https://firebase.google.com/docs/firestore/manage-data/add-data#update_elements_in_an_array
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 7 years ago.
Improve this question
I am new to Java Programming.I want to fetch data from the database and show it in JTable.
This is my current code.
String columns[]={"Transaction_id","User_name","Amount","Recharge_Spending","Spend_by"};
Object data[][]=new Object[200][5];
int i=0;
while(rs.next())
{
result_Transaction_id[i]=rs.getInt("f.Transaction_id");
result_User_name[i]=rs.getString("U.User_name");
data[i][5]={(Object)result_Transaction_id[i],(Object)result_User_name[i]};//This is where i get error
++i;
}
JTable View_table =new JTable(data,columns);
I want to collect the results of the query and add them in the specified object.
But i get the following error "Array constants can only be used in initializers".
I found a lot of solutions to "Array Constants can only be used in initializers" error, but those don't suit my case.
An "array constant" means { ... }.
As the error is trying to tell you, you can only use that syntax when declaring an array variable.
To assign an array to an existing expression, use new TypeName[] { ... }.
The error is the way you declare your array.
As stated here: https://stackoverflow.com/a/10520659/3558900 You can only declare an array NOT like this:
String columns[]={"Transaction_id","User_name","Amount","Recharge_Spending","Spend_by"};
BUT
String[] s;
s = new String[] {"Transaction_id","User_name","Amount","Recharge_Spending","Spend_by"};
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 7 years ago.
Improve this question
array = ArrayUtils.removeElement(array, array[i]);
This is working for int array, not for String array...
Is there any fast way to remove an element from a string array in Java?
Seems that you are using org.apache.commons.lang.ArrayUtils class, which is not standard Java class but part of Apache Commons Lang library. If you know an index of element, it would be more efficient to use
array = ArrayUtils.remove(array, i);
As this version will not search for given element, just remove by index. This should work for object arrays (including String array) as well.
Since array can not be resized, it may create some problems (eg.- raise of cost) when you try to remove some element from an array.
In this case ArrayList would be a better alternative. You can add/remove element from ArryList more continently -
List<String> strgs = new ArrayList<String>();
strgs.add("first");
strgs.add("second");
strgs.add("third");
strgs.remove("second");
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
Hii friends I am beginner in java. I am getting string from InputStream in the format like:
aD1bD2cD3dD4eD5fD6g
where D1,D2,D3... are data and small letters a,b,c,d,e,f,g are identifier so I can identify data between a and b and c and so on.
Now the problem is that I am getting more than one data pattern with spaces in between. But I need extract only first data from it. For example, consider data received as
a-674b-26c96d-662e-39f93g a74b-2c96d66e-39f86g a-84b-96c96d562e-99f93g
then I need to extract only first data from this entire data string that is
a-674b-26c96d-662e-39f93g
Please help me.
Use this
String[] params = data.split(" ");
String firstString = params[0];
split() function will split your whole string using space as delimiter, resulting in array of strings. First element of the array is the string you are looking for.
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