Results 21 to 27 of 27
Thread: array problem
- 12-19-2010, 07:29 PM #21
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,604
- Blog Entries
- 7
- Rep Power
- 17
Imagine that your input contains the following characters (as a matter of fact it does):
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.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>
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 12-19-2010, 08:00 PM #22
Senior Member
- Join Date
- Nov 2010
- Posts
- 155
- Rep Power
- 3
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:
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]for ( int j = 0; j < n; j++) { array [i][j] = 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 nullsJava 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)); } }
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 loopLast edited by aizen92; 12-19-2010 at 08:31 PM.
-
One suggestion to help make your code readable -- don't use [quote]/[/quote] tags but instead use [code]/[/code] tags.
- 12-20-2010, 08:51 AM #24
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,604
- Blog Entries
- 7
- Rep Power
- 17
- 12-20-2010, 09:09 AM #25
Senior Member
- Join Date
- Nov 2010
- Posts
- 155
- Rep Power
- 3
ok so this is my new code
however it gives me an error in the line of the array iside the for loop (bolded one)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)); } }
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 )
- 12-20-2010, 10:23 AM #26
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,604
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 12-21-2010, 05:42 AM #27
Senior Member
- Join Date
- Nov 2010
- Posts
- 155
- Rep Power
- 3
Similar Threads
-
Array problem please help
By newToJava3 in forum New To JavaReplies: 2Last Post: 12-15-2010, 12:38 PM -
array problem
By jabo in forum New To JavaReplies: 2Last Post: 03-31-2010, 09:54 AM -
Array problem
By c3jcarmy in forum New To JavaReplies: 11Last Post: 03-11-2010, 02:45 AM -
array problem
By wats in forum New To JavaReplies: 1Last Post: 12-12-2007, 07:08 AM -
array problem
By Albert in forum Advanced JavaReplies: 2Last Post: 07-01-2007, 01:13 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks