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 12 of 12

Thread: Program works, just not how i thought/wanted it would/to work.

  1. #1
    Junior Member
    Join Date
    May 2014
    Posts
    29
    Thanks
    13
    Thanked 0 Times in 0 Posts

    Default Program works, just not how i thought/wanted it would/to work.

    so i have a class that creates a file:
    java.io.*;
    import java.lang.*;
    import java.util.*;
     
    public class CreateFile {
     
    		private Formatter x;
     
    		public void openFile(){
    			try{
    				x=new Formatter("NikolaiLavroff.txt");
    			}
    		catch(Exception e){
    			System.out.println("You have an error");
    		}
    		}
     
    		public void addRecords(){
    			x.format("%s%s%s", "15\t", "N\t", "L\n");
    			x.format("%s%s%s", "18\t", "F\t", "L\n");
    			x.format("%s%s%s", "53\t", "K\t", "T\n"); 
    			x.format("%s%s%s", "53\t", "J\t", "L\n");  //there were names in place of letters but ive changed them.
    		}
     
    		public void closeFile(){
    			x.close();
    		}
     
    }

    What i wanted to see in the file was:

    15 N L
    18 F L
    53 K T
    53 J L


    and i that is what i saw.

    I have another class to read the file:

    import java.io.*;
    import java.lang.*;
    import java.util.*;
     
    public class ReadFile {
     
    	private Scanner x;
     
    	public void openFile(){
    		try{
    			x=new Scanner(new File("NikolaiLavroff.txt"));
    		}
    		catch(Exception e){
    			System.out.println("Could not find file");
    		}
    	}
    	public void readFile(){
    		while(x.hasNext()){
    			String a =x.next();
    			String b =x.next();
    			String c =x.next();
     
    		System.out.printf("%s%s%s", a,b,c);
    		}
    	}
    	public void closeFile(){
    		x.close();
    	}
    }

    and then my main:
     
    import java.io.File;
    import java.util.*;
    import javax.swing.JFrame;
     
    class Apples{
     
        public static void main (String args[]){
     
        	ReadFile r=new ReadFile();
        	r.openFile();
        	r.readFile();
        	r.closeFile();
        }
      }

    now what i was expecting to get in the program was

    15 N L
    18 F L
    53 K T
    53 J L

    but instead i got all of the above (^^^) in one line and no spaces between them. I think the problem lies in the way i added the data (added records) in my createfile class because i didnt know how to make the data start on a new lign without using '\n'. I think it is simple problem as i am a java rookie


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

    Default Re: Program works, just not how i thought/wanted it would/to work.

    got all of the above (^^^) in one line
    To put what's printed next on the next line,
    either use the println() method
    or include a "\n" character where you want the end of line to be.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    May 2014
    Posts
    29
    Thanks
    13
    Thanked 0 Times in 0 Posts

    Default Re: Program works, just not how i thought/wanted it would/to work.

    yes, but, i copied all of the code from a tutorial except, in the tutorial it shows how to add one record (in my CreateFile class) but then he just says he added more records, not showing how, but then i copied the exact same code in the tutorial for the ReadFile class but there was a different result in the tutorial so im wondering what i have to change in the 'CreateFile' class so that i get the wanted result.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Program works, just not how i thought/wanted it would/to work.

    To move the next String that is written to a new line, add a "\n" character where you want the end of the current line to be.

    Or use a method like println() that automatically adds the "\n".
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Jun 2014
    Posts
    33
    My Mood
    Inspired
    Thanks
    9
    Thanked 1 Time in 1 Post

    Default Re: Program works, just not how i thought/wanted it would/to work.

    I'm nowhere close being able to give you the solution, but do think that the core issue here is copying code and expecting it to work without you, the programmer, understanding how it operates.

    My suggestion is that you, instead, use that same tutorial and try to make your own code from scratch. Do not put anything into your code without fully understanding what it does.

    This way you will identify where your understanding is inadequate and can read up on that exact thing. Chances are that you will learn much more - and will be able to solve the issue expressed here yourself.

    If someone else helps you to fix the code you simply copied and changed, you will get the result you wanted, but the result you want should really be learning and understanding, not?

    Apologies if this post isn't very helpful, although it is trying to be.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Program works, just not how i thought/wanted it would/to work.

    thats not what im looking for, i know how to do that.
    Please explain the problem. I thought the program printed everything on one line and you want the output to go to multiple lines.

    Post the program's current output, explain what is wrong with it and show what you want it to be.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    May 2014
    Posts
    29
    Thanks
    13
    Thanked 0 Times in 0 Posts

    Default Re: Program works, just not how i thought/wanted it would/to work.

    Nothing is wrong with the method you proposed, i was just wondering how i could make 'x.format' go on a new line without having to do '\n'.

    The programs current output is as i said, all the data in one line.

    --- Update ---

    Quote Originally Posted by Baldyr View Post
    I'm nowhere close being able to give you the solution, but do think that the core issue here is copying code and expecting it to work without you, the programmer, understanding how it operates.

    My suggestion is that you, instead, use that same tutorial and try to make your own code from scratch. Do not put anything into your code without fully understanding what it does.

    This way you will identify where your understanding is inadequate and can read up on that exact thing. Chances are that you will learn much more - and will be able to solve the issue expressed here yourself.

    If someone else helps you to fix the code you simply copied and changed, you will get the result you wanted, but the result you want should really be learning and understanding, not?

    Apologies if this post isn't very helpful, although it is trying to be.
    i guess so. The problem is, the explaining comes after the code in these tutorials so its not that easy to do what you're saying. Also when you talk about reading up on an 'exact thing', i need the exact terminology in most cases to look that thing up, which i doubt i'll have in most cases.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Program works, just not how i thought/wanted it would/to work.

    how i could make 'x.format' go on a new line without having to do '\n'.
    What is wrong with using the "\n" character?
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    May 2014
    Posts
    29
    Thanks
    13
    Thanked 0 Times in 0 Posts

    Default Re: Program works, just not how i thought/wanted it would/to work.

    Quote Originally Posted by Norm View Post
    What is wrong with using the "\n" character?
    nothing in particular its just that, as i've said, in the tutorial i saw, another method is used, and ive specified, in what ive written, all the information that explains my problem.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Program works, just not how i thought/wanted it would/to work.

    You can write it anyway you want, but if you want the Strings on different lines, there needs to be a new line character between the lines. Your choice.

    in the tutorial i saw, another method
    Maybe the author or editor made a mistake.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    May 2014
    Posts
    29
    Thanks
    13
    Thanked 0 Times in 0 Posts

    Default Re: Program works, just not how i thought/wanted it would/to work.

    Quote Originally Posted by Norm View Post
    You can write it anyway you want, but if you want the Strings on different lines, there needs to be a new line character between the lines. Your choice.


    Maybe the author or editor made a mistake.

    It shows the program working...

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Program works, just not how i thought/wanted it would/to work.

    Must be a good editor.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Program Works on NetBeans, but Jar file doesn't work correctly.
    By LeandroRodirgues in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 11th, 2013, 12:36 PM
  2. Replies: 9
    Last Post: September 15th, 2013, 02:48 PM
  3. Replies: 5
    Last Post: March 20th, 2013, 02:29 PM
  4. Help wanted, 20$ a week - you only work a few minutes.
    By kxsb11 in forum Paid Java Projects
    Replies: 1
    Last Post: January 30th, 2012, 03:55 AM
  5. Program works fine but with friend it doesn't work
    By Rizza in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 1st, 2011, 01:20 PM