JAVA: How to convert String ArrayList to Integer Arraylist? - java

My question is -
how to convert a String ArrayList to an Integer ArrayList?
I have numbers with ° behind them EX: 352°. If I put those into an Integer ArrayList, it won't recognize the numbers. To solve this, I put them into a String ArrayList and then they are recognized.
I want to convert that String Arraylist back to an Integer Arraylist. So how would I achieve that?
This is my code I have so far. I want to convert ArrayString to an Int Arraylist.
// Read text in txt file.
Scanner ReadFile = new Scanner(new File("F:\\test.txt"));
// Creates an arraylist named ArrayString
ArrayList<String> ArrayString = new ArrayList<String>();
// This will add the text of the txt file to the arraylist.
while (ReadFile.hasNextLine()) {
ArrayString.add(ReadFile.nextLine());
}
ReadFile.close();
// Displays the arraystring.
System.out.println(ArrayString);
Thanks in advance
Diego
PS: Sorry if I am not completely clear, but English isn't my main language. Also I am pretty new to Java.

You can replace any character you want to ignore (in this case °) using String.replaceAll:
"somestring°".replaceAll("°",""); // gives "sometring"
Or you could remove the last character using String.substring:
"somestring°".substring(0, "somestring".length() - 1); // gives "somestring"
One of those should work for your case.
Now all that's left is to parse the input on-the-fly using Integer.parseInt:
ArrayList<Integer> arrayInts = new ArrayList<Integer>();
while (ReadFile.hasNextLine()) {
String input = ReadFile.nextLine();
try {
// try and parse a number from the input. Removes trailing `°`
arrayInts.add(Integer.parseInt(input.replaceAll("°","")));
} catch (NumberFormatException nfe){
System.err.println("'" + input + "' is not a number!");
}
}
You can add your own handling to the case where the input is not an actual number.

For a more lenient parsing process, you might consider using a regular expression.
Note: The following code is using Java 7 features (try-with-resources and diamond operator) to simplify the code while illustrating good coding practices (closing the Scanner). It also uses common naming convention of variables starting with lower-case, but you may of course use any convention you want).
This code is using an inline string instead of a file for two reasons: It shows that data being processed, and it can run as-is for testing.
public static void main(String[] args) {
String testdata = "55°\r\n" +
"bad line with no number\r\n" +
"Two numbers: 123 $78\r\n";
ArrayList<Integer> arrayInt = new ArrayList<>();
try (Scanner readFile = new Scanner(testdata)) {
Pattern digitsPattern = Pattern.compile("(\\d+)");
while (readFile.hasNextLine()) {
Matcher m = digitsPattern.matcher(readFile.nextLine());
while (m.find())
arrayInt.add(Integer.valueOf(m.group(1)));
}
}
System.out.println(arrayInt);
}
This will print:
[55, 123, 78]

You would have to create a new instance of an ArrayList typed with the Integer wrapper class and give it the same size buffer as the String list:
List<Integer> myList = new ArrayList<>(ArrayString.size());
And then iterate through Arraystring assigning the values over from one to the other by using a parsing method in the wrapper class
for (int i = 0; i < ArrayString.size(); i++) {
myList.add(Integer.parseInt(ArrayString.get(i)));
}

Related

Scanning a file and getting two tokens at a time and saving those in an ArrayList

Hi everyone this is my first question here so I apologize in advance if it is not in the correct format for this forum. I'm a comp sci student and I am really a novice programmer. For an assignment, I need to read in a file of polynomials and then sort them from highest degree to lowest. On each line of the file I have a coefficient and an exponent separated by a space.
This is what my .txt file looks like:
"5.6 3
4 1
8.3 0" which represents the polynomial 5.6x^3 + 4x + 8.3
I have to scan the file from a JFileChooser and then add the tokens to an ArrayList of type Polynomial. My question/problem is how can I run the file contents through a for loop and separate the first token(coefficients) from the second token(exponents) and then add them to the ArrayList? I'm going to need the exponents to be of type int and the coefficients should be double.
This is what I have so far:
ArrayList aList = new ArrayList();
// scanner used to read each line
try {
scan = new Scanner(file);
if (file.isFile()) {
while (scan.hasNext()) {
for (int i = 0; i < something.size(); i++) {
//this is where I'm lost, not sure what I need to do here
}
}
}
....
Thank you guys I appreciate the assistance.
-Linden
EDIT:
Alright I figured it out, it was a lot more complicated than I thought. Here's what I did:
Scanner scan = new Scanner(file);
if (file.isFile()) {
for (int i = 0; i <=2; i ++) {
term(scan);
}
}
static void term(Scanner scan) {
String s = scan.nextLine();
String [] splitter = s.split(" ");
String coefficient = splitter[0];
String exponent = splitter[1];
//populating
try {
arrayList.add(new Polynomial(coefficient, exponent));
} catch (InvalidPolynomialSyntax e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Thanks to the two guys that tried to help me out and the 24 viewers who didn't lol
First, you need to look at the scanner class. What methods does it have that you should use to get the data you need?
I don't want to give you the answer to your homework out of respect but broadly what you need is a way to get each line of that file from the scanner so you can compare the contents of each line to each other.
So look at the scanner class documentation and see what it has that you can use.
Start by simply trying to print out each line. Once you do that you can think about how you want to compare them.
you can read file as one sentence. First get all the text such as 5.6 3 4 1 8.3 0 as string. Then you can just use String [] arrayOfNumbers = yourString.split(" ");
This will split the string according to " " character so you get numbers directly.

How do i declare custom generated strings inside a while loop?

What I'm trying to do is to declare a certain amount of strings according to the amount of tokens a scanner scans in a single input, then have these strings equal the next input. This is what I'm trying:
int numberOfTokens = 0;
boolean mainLoop = true;
Scanner input = new Scanner(System.in);
while(mainLoop == true)
{
while(input.hasNext())
{
String int(+ numberOfTokens) = input.next(); (this doesn't work)
numberOfTokens += 1;
}
}
I hope I made it clear of what I am trying to do. I tried using String arrays, but they won't work for what i'm trying to do.
Thanks.
You can do:
String[] myStringArray = new String[abc];
where abc is an integer you get from user
and then
myStringArray[index] = input.next();
and index must be a valid number between 0 and abc
If you don't know in advance how many strings you will need to store then an array is a poor choice of data structure, at least during the input phase. Use a List instead -- these keep the elements in order, yet expand as needed to accommodate new elements. They are convenient to work with overall, but if you ultimately must get the strings in array form (e.g. because some external API requires that form) then it is easy to obtain the corresponding array.
For example:
List<String> tokens = new ArrayList<>();
while (input.hasNext()) {
tokens.add(input.next());
// a List keeps track of its own length
}
If you later wanted the array then you could do
String[] tokenArray = tokens.toArray(new String[0]);
The number of tokens recorded in the List is available at any time as tokens.size(), or after you convert to an array, as tokenArray.length.
In any event, you cannot create new variables at runtime in Java.
Instead of string variables, you should declare one variable like this before the while loop.
List<String> tokens = new ArrayList<>();
while (input.hasNext()) {
tokens.add(input.next());
}
You can then operate on the tokens, like this:
int n = tokens.size();
for (String token : tokens) {
System.out.println(token);
}

Java - Ideas to turn strings into numbers (RegEx/Parsing/etc.)?

I'm reading in from a file, and the input is like this:
Description (1.0,2.0) (2,7.6) (2.1,3.0)
Description2 (4,1)
...
Description_n (4,18) (8, 7.20)
I want to be able to take the numbers inside parentheses and use turn them from strings into numbers so that I can do mathematical operations of them. Right now, to simplify things, my code only reads in the first line and then splits it based on spaces:
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(new File("filename.txt")));
//reader reads in the first line
String firstLine = reader.readLine();
//splits into an array of ["Description","(1.0,2.0)","(2,7.6)","(2.1,3.0)"]
String[] parts = first.split(" ");
//now I want to store 1.0, 2, and 2.1 in one array as ints and 2.0, 7.6, and 3.0 in another int array
} catch (Exception e) {
System.exit(0);
}
What are some ways I can store the numbers inside parentheses into two separate arrays of ints (see comment above)? Should I use regular expressions to somehow capture something of the form "( [1-9.] , [1-9.] )" and then pass those into another function that will then separate the first number in the pair from the second and then convert them both into integers? I'm new to regular expression parsing in Java, so I'm not sure how to implement this.
Or is there a simply, better way to do this?
This stores the numbers into Double-arrays (not two-dimensional arrays, arrays of Double objects), since some have .#. int-arrays would eliminate the post decimal part.
It uses the regex \b([\d.]+)\b to find each number within each paren-group, adding each to an ArrayList<Double>. Note that it assumes all input is perfect (nothing like (bogus,3.2). The list is then translated into an array of Double objects.
This should give you a good start towards your goal.
import java.util.Arrays;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
<P>{#code java DoubleInParenStringsToArrays}</P>
**/
public class DoubleInParenStringsToArrays {
public static final void main(String[] ignored) {
String input = "(1.0,2.0) (2,7.6) (2.1,3.0)";
String[] inputs = input.split(" ");
//"": Dummy string, to reuse matcher
Matcher mtchrGetNums = Pattern.compile("\\b([\\d.]+)\\b").matcher("");
for(String s : inputs) {
ArrayList<Double> doubleList = new ArrayList<Double>();
mtchrGetNums.reset(s);
while(mtchrGetNums.find()) {
//TODO: Crash if it's not a number!
doubleList.add(Double.parseDouble(mtchrGetNums.group(0)));
}
Double[] doubles = doubleList.toArray(new Double[doubleList.size()]);
System.out.println(Arrays.toString(doubles));
}
}
}
Output:
[C:\java_code\]java DoubleInParenStringsToArrays
[1.0, 2.0]
[2.0, 7.6]
[2.1, 3.0]
How to parse per item:
Double.parseDouble("your string here");
As for the storing, I didnt get the pattern you want to store your values. What's the reason why you want 1.0, 2, and 2.1 in 1 array and 2.0, 7.6, and 3.0 to another?
Just do Integer.parseInt(string), or Double.parseDouble(string), then add those to the array. I'm not really 100% sure what you're asking, though.
I would use a String Tokenizer.But need more information and thought for full impl.
This is your line : "Description (1.0,2.0) (2,7.6) (2.1,3.0)"
First thing - can there be cases without parenthesis? Will there always be sets f 2,2,2 numbers ?
Do you want to take care of errors at each number or just skip the line or skip processing if there is an error (like number of numbers does not match?).
Now you need a data structure to hold numbers. You could make a class to hold each individual element in a seperate property if each number has a distinct meaning in the domain or have an array list or simple array if you want to treat them as a simple list of numbers. If a class one sample (incopmplete):
class LineItem{
}
Now to actually break up the string there are many ways to do it. Really depends on the quality of data and how you want to deal with possible errors
One way is find the first opening parenthesis( take rest of string and parse out using a String Tokenizer.
Something like:
int i = str.indexOf("(");
String s2 = str.substring(i);
StringTokenizer st = new StringTokenizer(s2, "() ,";//parenthesis, comma and space
ArrayList<Double> lineVals1 = new ArrayList<Double>();
ArrayList<Double> lineVals1 = new ArrayList<Double>();
int cnt = 0;
while(st.hasMoreTokens()){
cnt++;//use this to keep count of how many numbers you got in line and raise error if need be
String stemp = st.nextToken();
if(isNumeric(stemo)){
if(cnt % 2 == 1){
lineVals1.add(Double.parseDouble(stemp));
}else{
lineVals2.add(Double.parseDouble(stemp));
}
}else{
/raise error if not numberic
}
}
public static boolean isNumeric(String str)
{
NumberFormat formatter = NumberFormat.getInstance();
ParsePosition pos = new ParsePosition(0);
formatter.parse(str, pos);
return str.length() == pos.getIndex();
}

Sorting info from a txt file into two different arrays

For a uni assignment, I have to take input from a text file and sort it into two separate arrays. The text file is a football league table, arranged as such:
Barcelona 34
Real Madrid 32
I have written a piece of code like this:
holdingString = fileInput.readLine ();
StringTokenizer sort = new StringTokenizer (holdingString + " ");
countOfTokens = sort.countTokens();
System.out.println (countOfTokens + " tokens: " + holdingString);
This prints out the number of tokens and what the tokens are for each line, so it gives output of
Two tokens: Barcelona 34
Three tokens: Real Madrid 32
I've then written this piece of code:
for (int i = 0; i < countOfTokens; i++)
{
String temp = sort.nextToken ();
System.out.println(temp);
}
This reads just the next token and prints it out.
However, rather than printing the next token out, I want to check if it is a word or a number, and separate it into a different array accordingly, so it will be like this:
ArrayTeam Zero Element Barcelona
ArrayTeam First Element Real Madrid
ArrayPoints Zero Element 34
ArrayPoints First Element 32
What's the easiest way to do this? I've tried using a try/catch, but didn't get it right. I've also tried using an if statement with \d, but that's not worked either.
Like AmitD, I agree that using split is more appropriate in this case, but if you still like to use a StringTokenizer you do something like:
StringBuilder teamName=new StringBuilder();
for (int i = 0; i < countOfTokens-1; i++)
{
if (i>0) teamName.append(' ');
teamName.append(sort.nextToken());
}
teamNames[k]=teamName.toString(); //add the new team to your teamNames array
points[k]=Integer.parseInt(sort.nextToken()); //if your points array is of int type
you could use java.util.Scanner class to read data from the file. it has methods such as nextInt(), nextDouble ...whhich might be useful in your case.
Scanner scan = new Scanner(file);
int number;
if(scan.hasNextInt()){
number = scan.nextInt();
}
check Scanner API
String readLine = "Real Madrib 40";
String[] team = readLine.split( "\\d" );
System.out.println(team[0]);
String score = readLine.replace( team[0],"" );
System.out.println(score);
Output :
team[0] : Real Madrib
score : 40
You can save all that trouble using split
String strs[] = holdingString.split("\\s");
E.g.
"Barcelona 34".split("\\s"); will return you Array of Strings where
array[0]=Barcelona array[1]=34
From Javadoc of StringTokenizer
StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead.
Update
As #madhairsilence pointed out
You need another deliminator. You can use = like property files
"Real Madrid =34".split("=");//will return you Array of Strings where
array[0]=Real Madrid, array[1]=34
You can use Scanner as you are reading from file.

Breaking up a string in Java into a string array

I've researched this subject thoroughly, including questions and answers on this website....
this is my basic code:
import java.util.Scanner;
class StringSplit {
public static void main(String[] args)
{
System.out.println("Enter String");
Scanner io = new Scanner(System.in);
String input = io.next();
String[] keywords = input.split(" ");
System.out.println("keywords" + keywords);
}
and my objective is to be able to input a string like "hello, world, how, are, you, today," and have the program break up this single string into an array of strings like "[hello, world, how, are, you, today]...
But whenever i compile this code, i get this output:
"keywords = [Ljava.lang.String;#43ef9157"
could anyone suggest a way for the array to be outputted in the way i require??
Sure:
System.out.println("keywords: " + Arrays.toString(keywords));
It's not the splitting that's causing you the problem (although it may not be doing what you want) - it's the fact that arrays don't override toString.
You could try using Java's String.Split:
Just give it a regular expression that will match one (or more) of the delimeters you want, and put your output into an array.
As for output, use a for loop or foreach look to go over the elements of your array and print them.
The reason you're getting the output you're getting now is that the ToString() method of the array doesn't print the array contents (as it would in, say, Python) but prints the type of the object and its address.
This code should work:
String inputString = new String("hello, world, how, are, you, today");
Scanner scn = new Scanner(inputString);
scn.useDelimiter(",");
ArrayList<String> words = new ArrayList<String>();
while (scn.hasNext()) {
words.add(scn.next());
}
//To convert ArrayList to array
String[] keywords = new String[words.size()];
for (int i=0; i<words.size(); ++i) {
keywords[i] = words.get(i);
}
The useDelimiter function uses the comma to separate the words. Hope this helps!

Categories

Resources