1

So I have a String that was given to me. I have eliminated all non integer characters, except for whitespace. Each line the amount of integer varies, some lines I will have one, sometimes I have two or even three. I now need to read in each integer and add them to an array list accordingly. Currently this is what I have:

while(fileScanner.hasNextLine()){
        fileScanner.nextLine();
        line=lineReplacer.nextLine();
        String line2=line.replaceAll("[a-z]","").replaceAll("[A-Z]","").replaceAll(": ","").replaceAll(" ","").replaceAll(", ", " ");
        specs.add(Integer.parseInt(line2));
    }

I am stuck on how to add in each int, without getting an error, without knowing how many ints are the line I currently have.

Edit: Expected values would be things such as, 2 2 2, or 12 14 11, or just 10, or 29 30. The ints must stay separated because they need to be added to an array. The only limitation on input is that it isn't more than three ints.

4
  • please specify Input data and expected output data. Commented Feb 7, 2014 at 7:02
  • Why not just use Scanner.nextInt()? Commented Feb 7, 2014 at 7:06
  • I can't use nextInt function because it has whitespace in it still. Commented Feb 7, 2014 at 7:10
  • @dfburke nextInt skips white space, see my answer. Commented Feb 7, 2014 at 7:26

3 Answers 3

2

Try this:

String line="Hello 12 hi 458 h20 Bye 8";
String line2 = line.replaceAll("\\D+","");
System.out.println(line2);

Output:

12458208

You can add all values to list like:

for(int i=0;i<line2.length();i++)
{
    specs.add(Integer.parseInt(line2.charAt(i)+""));
}
Sign up to request clarification or add additional context in comments.

1 Comment

Then that combines two or three seperate integers into one, say 2 3 5 into 235.
1

Try this:

    String line = "ABC 1000,snsdf 987";
    Pattern pattern = Pattern.compile("\\d+");
    Matcher matcher = pattern.matcher(line);
    List<Integer> list = new ArrayList<Integer>();
    while (matcher.find()) {
        String match = matcher.group();
        list.add(Integer.parseInt(match));
    }

You can convert the list to an array if needed:

    Integer[] array = new Integer[list.size()];
    array =  list.toArray(array);

Comments

0

nextInt works fine in this situation, it skips the white space between the integers. Try for example:

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner fileScanner = new Scanner("1 2 23 4\n3");
        while (fileScanner.hasNextInt()) {
            System.out.println(fileScanner.nextInt());
        }
    }

}

This gives output:

1
2
23
4
3

3 Comments

So I just made a test class and you're absolutely correct! although, when I create an array list, it skips over index 1. It just adds, "1" and then "23". but it misses the two and the 4. Why is that?
Seems, you call nextInt twice in the loop. It will read the next integer whenever it is called.
That is simple enough to fix, thank you for this. I don't know how long it would have taken me to realize that nextInt skips whitespace.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.