Page 1 of 2 12 LastLast
Results 1 to 20 of 27

Thread: array problem

  1. #1
    aizen92 is offline Senior Member
    Join Date
    Nov 2010
    Posts
    155
    Rep Power
    4

    Default array problem

    I am having a bit of a problem dealing with arrays
    i have a file that contains a bunch of integers, such as the following one
    "3
    4: 2, 5, 3, 1
    2: 3, 7
    5: 5, 6, 4, 3, 3"

    the 3 is the number of arrays i should make
    and the numbers 4,2, and 5 are the sizes of each array

    i want to make a code that reads the file and put the values in arrays
    ie: array 1 must contain the elements 2 5 3 1
    array 2 must contain the elements 3 7
    array 3 must contain the elements 5 6 4 3 3

    plz i need some help here and i appreciate it very much
    thanks in advance

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    12,998
    Blog Entries
    7
    Rep Power
    19

    Default

    If you unravel your problem you'll see that you have two smaller problems:

    1) reading the input and dissecting it;
    2) storing the read numbers in arrays.

    Can you solve subproblem 1?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    aizen92 is offline Senior Member
    Join Date
    Nov 2010
    Posts
    155
    Rep Power
    4

    Default

    well i can read the first integer which is the number of arrays, but i dont quite know how to read the rest

    sry about that but im still a beginner in the whole java thing and im learning it on my own from a book and solving problems i find on the net to help myself

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    12,998
    Blog Entries
    7
    Rep Power
    19

    Default

    Quote Originally Posted by aizen92 View Post
    well i can read the first integer which is the number of arrays, but i dont quite know how to read the rest

    sry about that but im still a beginner in the whole java thing and im learning it on my own from a book and solving problems i find on the net to help myself
    Have you read the API documentation for the Scanner class? If not, do so.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    aizen92 is offline Senior Member
    Join Date
    Nov 2010
    Posts
    155
    Rep Power
    4

    Default

    well yeh i read it but i didnt find what i wanted, or i didnt know where to find it
    but what exactly may I be looking in the documentation?

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    The Scanner class has several different nextXXX methods listed in the API, some for reading lines, some for reading ints, some for reading doubles, etc.... The XXX should tell you what the Scanner will try to read. So if you want to read an int, you'll use nextInt().

  7. #7
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    12,998
    Blog Entries
    7
    Rep Power
    19

    Default

    Quote Originally Posted by aizen92 View Post
    well yeh i read it but i didnt find what i wanted, or i didnt know where to find it
    but what exactly may I be looking in the documentation?
    You can read what that class has to offer you. For sure it doesn't have a readArray() method but other methods can be of great help too. If you think the Scanner class is too complicated for you (I sincerely doubt it) have a look at the String.split() method and the BufferedReader class and its readLine() method.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    aizen92 is offline Senior Member
    Join Date
    Nov 2010
    Posts
    155
    Rep Power
    4

    Default

    i re-read the documentation and im sry but i didnt find what i must find
    but i have a thing that i still didnt cover the bufferedreader or the split or the readline, so i guess i must do them without these

    i was also trying to do a thing here

    for ( int i = 0; i < N; i++)
    {
    numElm [i] = input.nextInt();
    input.nextLine();
    }
    but i got this error here:

    java.util.InputMismatchException
    at java.util.Scanner.throwFor(Scanner.java:840)
    at java.util.Scanner.next(Scanner.java:1461)
    at java.util.Scanner.nextInt(Scanner.java:2091)
    at java.util.Scanner.nextInt(Scanner.java:2050)
    at Problem3.main(Problem3.java:18)

  9. #9
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    12,998
    Blog Entries
    7
    Rep Power
    19

    Default

    Quote Originally Posted by aizen92 View Post
    i re-read the documentation and im sry but i didnt find what i must find
    but i have a thing that i still didnt cover the bufferedreader or the split or the readline, so i guess i must do them without these
    The nextInt() method tries to read an int for you; if it stumbles upon a character that isn't a digit and if it hasn't read any digits before it assumes there is no int in the input stream. Your input stream has commas in it. You should get rid of them when reading ints. Read the API documentation for the Scanner class again, it can also do that for you.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    aizen92 is offline Senior Member
    Join Date
    Nov 2010
    Posts
    155
    Rep Power
    4

    Default

    i got a mistake in the input file its without the commas
    this is how it is in exact:
    3
    4: 2 5 3 1
    2: 3 7
    5: 5 6 4 3 3

  11. #11
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    12,998
    Blog Entries
    7
    Rep Power
    19

    Default

    Quote Originally Posted by aizen92 View Post
    i got a mistake in the input file its without the commas
    this is how it is in exact:
    There still is a colon after the first integer; you have to get rid of that too.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  12. #12
    aizen92 is offline Senior Member
    Join Date
    Nov 2010
    Posts
    155
    Rep Power
    4

    Default

    can i do the "input.next()" and the write the "input.nextInt()" to record the numbers?

  13. #13
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    12,998
    Blog Entries
    7
    Rep Power
    19

    Default

    Quote Originally Posted by aizen92 View Post
    can i do the "input.next()" and the write the "input.nextInt()" to record the numbers?
    Sure, also read the API documentation for the Scanner class (again); you'll see that you also can get past that colon by using scanner.skip(":"); and then read the ints for your array.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  14. #14
    aizen92 is offline Senior Member
    Join Date
    Nov 2010
    Posts
    155
    Rep Power
    4

    Default

    ohh thats great
    thnx very much, ill try it now with the scanner.skip and post how it turned out to be

  15. #15
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    12,998
    Blog Entries
    7
    Rep Power
    19

    Default

    Quote Originally Posted by aizen92 View Post
    ohh thats great
    thnx very much, ill try it now with the scanner.skip and post how it turned out to be
    Well, your scanner.next() (and ignoring the return value) is just as good as scanner.skip(":"); good luck with it and thanks for letting us know how it went.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  16. #16
    aizen92 is offline Senior Member
    Join Date
    Nov 2010
    Posts
    155
    Rep Power
    4

    Default

    well i got it to compile by using the following scanner.skip but i get an error InputMismatch, what is wrong here?

  17. #17
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    12,998
    Blog Entries
    7
    Rep Power
    19

    Default

    Quote Originally Posted by aizen92 View Post
    well i got it to compile by using the following scanner.skip but i get an error InputMismatch, what is wrong here?
    I need to see some code; I'm not psychic during weekends. Please also show us the error message verbatim and all of it.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  18. #18
    aizen92 is offline Senior Member
    Join Date
    Nov 2010
    Posts
    155
    Rep Power
    4

    Default

    ok so this is what i have done with my piece of code

    import java.util.Scanner;
    import java.io.*;
    import java.util.Arrays;

    public class ArrayView
    {
    public static void main (String[] args) throws FileNotFoundException
    {
    Scanner input = new Scanner(new File(args[0]));
    int N = input.nextInt();
    int [][] array = new int [N][];

    while(input.hasNextLine())
    {
    String str = input.nextLine();
    Scanner scanLine = new Scanner (str);

    int i = 0;
    while(scanLine.hasNextInt())
    {
    int n = input.nextInt();
    scanLine.skip(":");
    array [i][n] = scanLine.nextInt();
    i++;
    }
    }
    System.out.println(Arrays.toString(array));
    }
    }
    and the input file is the following

    4
    5: 2 3 12 16 44
    3: 2 6 7
    6: 1 2 3 8 9 99
    2: 89 99
    but the output im getting is the following

    [null, null, null, null]
    all what i want to have is to show the 4 arrays with each elements

    i guess i did well with this code, it took me some time :P

    please help me with this code

  19. #19
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    12,998
    Blog Entries
    7
    Rep Power
    19

    Default

    If you want to read an int you call the method nextInt() but you type, say, 123<enter>. The nextInt() method reads and returns 123 but it leaves the character <enter> in the input buffer. The nextLine() method reads all characters up to an <enter> character. There still is such a character in the input buffer so the nextLine() method happily reads an empty line from the input. The fix is to put an additional nextLine() call just after you've read an int using the nextInt() method and you're expecting an end of line.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  20. #20
    aizen92 is offline Senior Member
    Join Date
    Nov 2010
    Posts
    155
    Rep Power
    4

    Default

    Ok, i see what does the nextLine() do in exact, but can u plz explain more in this lines becuase i didnt quite understand them well

    There still is such a character in the input buffer so the nextLine() method happily reads an empty line from the input. The fix is to put an additional nextLine() call just after you've read an int using the nextInt() method and you're expecting an end of line.

Page 1 of 2 12 LastLast

Similar Threads

  1. Array problem please help
    By newToJava3 in forum New To Java
    Replies: 2
    Last Post: 12-15-2010, 12:38 PM
  2. array problem
    By jabo in forum New To Java
    Replies: 2
    Last Post: 03-31-2010, 09:54 AM
  3. Array problem
    By c3jcarmy in forum New To Java
    Replies: 11
    Last Post: 03-11-2010, 02:45 AM
  4. array problem
    By wats in forum New To Java
    Replies: 1
    Last Post: 12-12-2007, 07:08 AM
  5. array problem
    By Albert in forum Advanced Java
    Replies: 2
    Last Post: 07-01-2007, 01:13 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •