0

so my program prints an array but when it does it also includes brackets and commas and i dont want those.

here is my library

import java.util.*;
public class library2
{
        public static boolean IsPrime(int p)
        {
                for (int i = 2; i < p; i++)
                {
                        if (p % i == 0 && i != p)
                                return false;
                }
                return true;
        }
        public static List<List<Integer>> GetPrimes(int n)
        {
                List<Integer> arr = new ArrayList<Integer>();
                for (int j = 2; j < n; j++)
                {
                        if(IsPrime(j))
                        {
                                arr.add(j);
                        }
                }
                return Arrays.asList(arr);
        }
}

here is my driver

import java.util.Scanner;
import java.util.*;
import java.util.Arrays;
public class driver2
{
        public static void main(String[] args)
        {
                Scanner scan = new Scanner(System.in);
                System.out.print("Please input an integer: ");
                int n = scan.nextInt();
                Boolean check = library2.IsPrime(n);
                List<List<Integer>> arr = library2.GetPrimes(n);
                System.out.println(Arrays.toString(arr));
        }
}

ive tried the Arrays.toString(); method but i get an error when i compile. i used import java.util.Arrays; but that didnt solve anything. can someone help me out.

4
  • 1
    So how do you want your output formatted? Commented Feb 15, 2015 at 8:05
  • instead of [[1, 2, 3, 4, 5]] which i get, i want 1 2 3 4 5 Commented Feb 15, 2015 at 8:08
  • OK. Then write your code to do just that. Commented Feb 15, 2015 at 8:09
  • Why in the first place do you need List<List<Integer>> when you can do it with List<Integer> Commented Feb 15, 2015 at 10:15

1 Answer 1

1

arr is a list and not an array.

You should iterate over and print:

     Iterator<List<Integer>>  arrIterator = arr.iterator(); 
     while (arrIterator.hasNext()) {
        List<Integer> l = arrIterator.next();
        Iterator<Integer> lIterator = l.iterator(); 
        while(lIterator.hasNext()){
          int i = lIterator.next();     
          System.out.print(i + " ");
        }
        System.out.print("\n");
    }
Sign up to request clarification or add additional context in comments.

5 Comments

would i place this in my library or my driver
hasNext() is a method of Iterator and not the list itself.
thank you but can you explain why my <Arrays.toString(arr));> didnt work
Because your arr is not an array, its a list.
And now you can (if you wish to) use syntactic sugar and re-write it using for-each loops.

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.