Results 1 to 11 of 11
  1. #1
    iwtljava is offline Member
    Join Date
    Nov 2016
    Posts
    22
    Rep Power
    0

    Default Stuck on making a new line

    So this code lets the user enter a person's name and social security number, up to five persons actually. My problem is that at the end it should output this:

    Person1 SSN1
    Person2 SSN2
    Person3 SSN3 etc.

    Now it prints the person and the SSN on seperate lines, like this:

    Person1
    SSN1
    Person2
    SSN2 etc.

    Any ideas? I've been stuck at this for hours and I'm not even sure I can just add a "\n" somewhere because it sure doesn't seem to work wherever I try to put it...

    Java Code:
    public class 2darray {
    
    	public static void main(String[] args) {
    		String[][] nameAndSoc = new String[5][2];
    		
    		
    		int i = 0;
    		int j = 0;
    		String string= " ";
    		for (i  = 0; i < 5; i++){	
    			
    			UIManager.put("OptionPane.cancelButtonText", "Print");
    			String in;
    			in = JOptionPane.showInputDialog(null, "Give name and social security number ");
    			if(in==null){
    				break;
    			}
    			String[] line =  in.split(",");
    			
    			
    			for (j = 0; j< 2; j++){
    				
    				 nameAndSoc[i][j] = line[j];
    				string =  string.concat( nameAndSoc[i][j]) + "\n";
    				
    			}
    		
    		}
    		
    		System.out.println(string);
    		
    		
    			
    		}
    	}

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Stuck on making a new line

    On line 24 you are putting a line break, which applies to both parts of the data (name and soc).
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    iwtljava is offline Member
    Join Date
    Nov 2016
    Posts
    22
    Rep Power
    0

    Default Re: Stuck on making a new line

    Yes I do know that it is not the correct position to put it. However, it feels like I have tried every possible position of \n, and wherever I put it I don't get it to work. If I skip newline completely, it prints all the inputs in a single line

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Stuck on making a new line

    Don't use a loop at line 21. Just concat the two together with a "\n" at the end.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    iwtljava is offline Member
    Join Date
    Nov 2016
    Posts
    22
    Rep Power
    0

    Default Re: Stuck on making a new line

    If I remove the for loop with j, it only outputs the name and not the social security number. Like this:


    Java Code:
    public class 2darray {
     
        public static void main(String[] args) {
            String[][] nameAndSoc = new String[5][2];
             
             
            int i = 0;
            int j = 0;
            String string= " ";
            for (i  = 0; i < 5; i++){    
                 
                UIManager.put("OptionPane.cancelButtonText", "Print");
                String in;
                in = JOptionPane.showInputDialog(null, "Give name and social security number ");
                if(in==null){
                    break;
                }
                String[] line =  in.split(",");
                 
                 
               
                     
                    nameAndSoc[i][j] = line[j];
                    string =  string.concat( nameAndSoc[i][j]) + "\n";
                     
                
             
            }
             
            System.out.println(string);
             
             
                 
            }
        }

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Stuck on making a new line

    Try debugging the code to see what it is doing by printing the contents of the nameAndSoc array. The Arrays class's deepToString method is useful for that:
    Java Code:
      System.out.println("an ID "+ java.util.Arrays.deepToString(theArrayName));
    Where does the code check the length of the line array and use the length to be sure to access all the elements in the line array?
    Last edited by Norm; 12-05-2016 at 05:48 PM.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Stuck on making a new line

    But you know you've got two things in there.
    Well, at least that's your assumption.
    So just use index 0 and 1, and forget about using 'j' at all.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  8. #8
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Stuck on making a new line

    Quote Originally Posted by iwtljava View Post
    If I remove the for loop with j, it only outputs the name and not the social security number.
    That's not what I said. Re-read this.

    Don't use a loop at line 21. Just concat the two together with a "\n" at the end.
    Concatenate the two together. The two means the name and ssn. Use 0 and 1 to do it.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  9. #9
    iwtljava is offline Member
    Join Date
    Nov 2016
    Posts
    22
    Rep Power
    0

    Default Re: Stuck on making a new line

    Thank you! I'm not sure if you meant it like this, or if there is a shorter way, but at least it's working now! Thanks again!!

    Java Code:
                            nameAndSoc[i][0] = line[0];
    			string = string.concat(nameAndSoc[i][0]);
    			nameAndSoc[i][1] = line[1];
    			string = string.concat(nameAndSoc[i][1]+"\n");

  10. #10
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Stuck on making a new line

    Well, that is essentially what I meant. But I wonder if the posted code is really what you ran (I have my doubts).

    First, identifiers (like a class name), can't start with a digit.
    And it looks like you are splitting on a command where your example data shows a space delimiter.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Stuck on making a new line

    Since the line is already the array you want then you could just do the one assignment:
    Java Code:
    nameAndSoc[i] = line;
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Replies: 16
    Last Post: 09-17-2011, 02:41 AM
  2. Replies: 1
    Last Post: 02-13-2011, 03:09 PM
  3. Java File making new line
    By counterfox in forum New To Java
    Replies: 3
    Last Post: 11-28-2010, 12:46 AM
  4. Replies: 2
    Last Post: 07-02-2010, 02:20 PM
  5. Replies: 4
    Last Post: 09-10-2009, 10:18 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
  •