Page 2 of 2 FirstFirst 12
Results 21 to 27 of 27

Thread: array problem

  1. #21
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,363
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by aizen92 View Post
    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
    Imagine that your input contains the following characters (as a matter of fact it does):

    Java Code:
    4<enter>
    5: 2 3 12 16 44<enter>
    3: 2 6 7<enter>
    6: 1 2 3 8 9 99<enter>
    2: 89 99<enter>
    To be able to read it all you have to call nextInt() (to read the first int), nextLine() (to get rid of that <enter> character), nextInt() (to read the first integer on the next line), skip(":") (to get rid of that colon, nextInt(), nextInt() ... a few times and readLine() again, etc. etc.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

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

    Default

    ok so i think i got 2 versions of what might be a correction of the previous code with the correction u told me about

    version 1:
    Java Code:
    import java.util.Scanner;
    import java.io.*;
    import java.util.Arrays;
    
    public class Problem3
    {
        public static void main (String[] args) throws FileNotFoundException
        {
            Scanner input = new Scanner(new File(args[0]));
            int N = input.nextInt();
            [B]input.nextLine();[/B]
            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(":");
                    
                    [B]for ( int j = 0; j < n; j++)
                    {
                        array [i][j] = scanLine.nextInt();
                    }
                    
                    scanLine.nextLine();[/B]
                    i++;
                }
            }
            System.out.println(Arrays.toString(array));
        }
    }
    version 2:
    Java Code:
    import java.util.Scanner;
    import java.io.*;
    import java.util.Arrays;
    
    public class Problem3
    {
        public static void main (String[] args) throws FileNotFoundException
        {
            Scanner input = new Scanner(new File(args[0]));
            int N = input.nextInt();
            [B]input.nextLine();[/B]
            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(":");
                    [B]array [i][n] = scanLine.nextInt();
                    scanLine.nextLine();[/B]
                    i++;
                }
            }
            System.out.println(Arrays.toString(array));
        }
    }
    but when i tried to run it, it still gave me the same result which is the nulls
    i hope i did the corrections right

    EDIT:

    I set a breakpoint on the last while loop in my code to use the debugger, but the thing is i dont think the code reaches this while loop because it gave me the output directly without going through the loop
    Last edited by aizen92; 12-19-2010 at 08:31 PM.

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

    Default

    One suggestion to help make your code readable -- don't use [quot&#101;]/[/quot&#101;] tags but instead use [cod&#101;]/[/cod&#101;] tags.

  4. #24
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,363
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by aizen92 View Post
    I set a breakpoint on the last while loop in my code to use the debugger, but the thing is i dont think the code reaches this while loop because it gave me the output directly without going through the loop
    Don't use a debugger, use System.out.println( ... ) and print everything you're interested in and everything you think the program must've read.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

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

    Default

    ok so this is my new code

    Java Code:
    import java.util.Scanner;
    import java.io.*;
    import java.util.Arrays;
    
    public class Problem3incomplete
    {
        public static void main (String[] args) throws FileNotFoundException
        {
            Scanner input = new Scanner(new File(args[0]));
            int N = input.nextInt();
            input.nextLine();
            int [][] array = new int [N][];
            
            while(input.hasNextLine())
            {
                String str = input.nextLine();
                Scanner scanLine = new Scanner (str);
    
                scanLine.useDelimiter(": ");
                int n = scanLine.nextInt();
                scanLine.useDelimiter(" ");
                
                int i = 0;
                while(scanLine.hasNext())
                {
                    [B]for ( int j = 0; j < n; j++)
                    {
                        array [i][j] = scanLine.nextInt();
                    }[/B]                
                    scanLine.nextLine();
                    i++;
                }
            }
            System.out.println(Arrays.toString(array));
        }
    }
    however it gives me an error in the line of the array iside the for loop (bolded one)
    the error is mismatchinput thing

    the exact error im getting in the console is :

    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 Problem3incomplete.main(Problem3incomplete.java:28 )

  6. #26
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,363
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by aizen92 View Post
    however it gives me an error in the line of the array iside the for loop (bolded one) the error is mismatchinput thing
    Where are the System.out.println( ... ) calls? Print out the value of the loop variable j, also print out what the String line is you're trying to scan. Don't just guess and don't immediately post here when your program doesn't do what you want it to do. Experiment with the code.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

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

    Default

    ok
    i played with thecode a little and it turned great and worked finally
    Thanks JosAH for the help

Page 2 of 2 FirstFirst 12

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
  •