-2
File myFile = new File(
    Environment.getExternalStorageDirectory().getAbsolutePath(),
    "Notes/test.txt"
);
BufferedReader br = new BufferedReader(new FileReader(myFile));
String line = br.readLine();
while ((line = br.readLine()) != null) {
    b = line.split(",");
    xpoints = new int[b[0].length()];
    ypoints = new int[b[1].length()];
    for (int i = 1; i < b[0].length(); i++) {
        xpoints[i] = Integer.parseInt(b[0]);
        ypoints[i] = Integer.parseInt(b[1]);                
    }
    /*direction(xpoints, ypoints);*/
}
br.close();

Here, I get X and Y value from b[0] and b[1]. I want to store this values in integer array(like int []x and int []y).How can i get all these values in array as i said earlier?

2
  • 1
    Do you want two arrays? One for the x-values and one for the y-values? Or do you want one array where each value is a combination of the x-y value Commented Jan 30, 2015 at 9:24
  • Yeah, I want two arrays. Commented Jan 30, 2015 at 9:25

7 Answers 7

4

You should parse your String into int like:

x[i] = Integer.parseInt(str);

for every single String representation of each int element

beware to provide into str though only integer because it will throw NumberFormatException otherwise.

Sign up to request clarification or add additional context in comments.

Comments

0

You can iterate over the String array in a loop and then store the values in an int array of the same size.

int[] intArray = new int[b.length];
for(int i = 0; i < b.length; i++){
    try{
        intArray[i] = Integer.parseInt(b[i]);
    catch(NumberFormatException e){
        //handle exception
    }
}

Comments

0

In your while statement when you do a split do this:

String[] b = line.split(splitsby);
int[] intArray = new int[b.length];
for (int stringIndex = 0; stringIndex < b.length; stringIndex++) {
    intArray[stringIndex] = Integer.parseInt(b[stringIndex]);
}
System.out.println("X = " + intArray[0] + " Y = " + intArray[1]);

This is assuming that every value in b can be parsed as an Integer

Comments

0

Since you dont know the exact size of the elements you will get from file, I suggest you to create a ArrayList.

Arralist<Integer> a=new ArrayList<Integer>();
Arralist<Integer> b=new ArrayList<Integer>();

Then

File myFile = new File(Environment
                        .getExternalStorageDirectory().getAbsolutePath(),
                        "Notes/test.txt");
BufferedReader br = new BufferedReader(new FileReader(myFile));
String line=br.readLine();
String splitsby = ",";
while ((line = br.readLine()) !=null) {
     String[] b=line.split(splitsby);
     System.out.println("X = "+b[0]+" Y = "+b[1]);
     a.add(Integer.parseInt(b[0]);
     b.add(Integer.parseInt(b[1]);
}
br.close();

Comments

0

You can add this method:

private ArrayList<Integer> getIntegerArray(ArrayList<String> stringArray) {
    ArrayList<Integer> result = new ArrayList<Integer>();
    for(String stringValue : stringArray) {
        try {
            // convert String to Integer and store it into integer array list
            result.add(Integer.parseInt(stringValue));
        } catch (NumberFormatException nfe) {
            // System.out.println("Could not parse " + nfe);
            Log.w("NumberFormat", "Parsing failed! " + stringValue + " can not be an integer");
        } 
    }       
    return result;
}

1 Comment

Hint: Code blocks on their own are not very informative. Please explain what the code you're showing does, why you chose to write it that way, and how you expect it to work.
0

You can use Integer.parseInt() method to convert string into integer. you have to go through each string array element to use above method. Following like code can be used in any place as per your requirement.

    int[] x = new int[2];
    x[0] = Integer.parseInt(b[0]);
    x[1] = Integer.parseInt(b[1]);

1 Comment

Hint: Code blocks on their own are not very informative. Please explain what the code you're showing does, why you chose to write it that way, and how you expect it to work.
-1

Integer.parseInt(b[0]); See the Javadoc for more information.

Comments

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.