Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: my code will not return everything...

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Unhappy my code will not return everything...

    hi, i have been looking at this code way to long! i have been playing with the return statement and i don't know what the problem is. i just want it to print the entire 9 by 9 text file that it reads. take a look at it thanks in advance! i will also attach an example of the type of copy of data or txt file it reads...
    import java.util.*;
    import java.io.*;
    import javax.swing.JOptionPane;
     
    public class SudokuSolverXXX{
    public static void main (String [] args){
      String input = JOptionPane.showInputDialog("Enter the file path: ");
    	String file_Name = new String(input); //String("input");
    	int [][] s = new int[9][9];
    	try{
    		File q = new File(file_Name);
    		Scanner fin = new Scanner(q);
    		 for(int r = 0; r < s.length; ++r){
    		   for(int c = 0; c < s[r].length; ++c){
    		     s[r][c] = fin.nextInt();
    			 }
    	System.out.println(r);
    		}
    	}
     
    	//System.out.println (s);
    	catch (IOException e) {
    		System.out.println( "The file path or name: "+input+", was not found! " );
    	}
    }
    }
    Attached Files Attached Files


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: my code will not return everything...

    want it to print the entire 9 by 9 text file that it reads.
    Please copy the full output from the program and paste it here. Add some comments to the post describing what is wrong with the output and show what you want the output to look like.

    Please edit your post and wrap your code with code tags:
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Sep 2012
    Posts
    128
    Thanks
    1
    Thanked 14 Times in 14 Posts

    Default Re: my code will not return everything...

    System.out.println(s);

    You can't print the 2d array with such a simple statement. You need a loop.

    How come you are using ++c (preincrement)) and not c++ ?

    BTW, the puzzle cannot be solved. It isn't valid!

  4. #4
    Junior Member
    Join Date
    Mar 2013
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: my code will not return everything...

    This code reads the attached file "sudoku.txt". Then it is supposed to print everything in the txt file to the console. as of now it prints this:
    0
    1
    2
    3
    4
    5
    6
    7
    8


    it should print:
    9 8 3 1 0 0 9 0 6
    6 0 0 9 0 6 8 0 0
    6 7 0 0 0 8 5 1 2
    6 4 7 0 0 0 3 0 0
    5 0 0 0 5 0 0 0 1
    4 0 8 0 0 0 7 4 0
    3 1 2 5 0 0 8 9 0
    2 0 5 4 0 7 1 0 0
    8 0 6 0 0 1 0 5 9


    --- Update ---

    i know the puzzle cant be solved this is just a project it will in time tell me that it is not valid, but as of now im just trying to get it to return the entire puzzle as it is in the txt file.

  5. #5
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: my code will not return everything...

    Where is the code that prints each cell's value? I don't see it posted anywhere.

  6. #6
    Junior Member
    Join Date
    Mar 2013
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: my code will not return everything...

    the goal is to print each cell's value that is what i need help with.

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: my code will not return everything...

    To print several rows of data with each row having several columns, use nested loops.
    The outer loop goes over the rows.
    The inner loop goes over the columns
    print each column's value on the current line
    at the end of the columns, use println() to move the output to the next line
    If you don't understand my answer, don't ignore it, ask a question.

  8. The Following User Says Thank You to Norm For This Useful Post:

    zach_l_b (March 30th, 2013)

  9. #8
    Junior Member
    Join Date
    Mar 2013
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: my code will not return everything...

    ok, i fixed it my self here is the code i wanted:

    import java.util.*;
    import java.io.*;
    import javax.swing.JOptionPane;
     
    public class SudokuSolverXXX{
    public static void main (String [] args){
      String input = JOptionPane.showInputDialog("Enter the file path: ");
    	String file_Name = new String(input); //String("input");
    	int [][] s = new int[9][9];
    	try{
    		File q = new File(file_Name);
    		Scanner fin = new Scanner(q);
    		 for(int r = 0; r < s.length; ++r){
    		   for(int c = 0; c < s[r].length; ++c){
    		     s[r][c] = fin.nextInt();
    			 }
    			 //System.out.print(s[r][c]);
    			 //System.out.print(" "+s[8][1]);
    		}
    		for(int u = 0; u < s.length; u++) {
     
    				for(int a = 0; a < s[u].length; a++) {
     
    					System.out.print(s[u][a] + " ");
     
    				}
    				System.out.println();
    			}
    		}
    	catch (IOException e) {
    		System.out.println( "Dude, the file path or name: "+input+", was not found! " );
    	}
    }
    }

Similar Threads

  1. [SOLVED] return
    By dipakshah8944 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: July 16th, 2012, 12:21 AM
  2. My code return null, Why?
    By andry2181 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 16th, 2012, 09:51 AM
  3. Replies: 4
    Last Post: February 26th, 2012, 05:36 PM
  4. Return Object does not return the expected output
    By Nour Damer in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 13th, 2011, 07:24 AM
  5. Replies: 5
    Last Post: July 7th, 2011, 09:22 AM