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: String question

  1. #1
    Junior Member
    Join Date
    Jun 2012
    Posts
    8
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default String question

    Why is the program not displaying the lines with the strings one, two, etc?
    Thanks






    /*
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    */
    package file.head;

    import java.io.*;
    import java.util.Scanner;

    /**
    *
    * @author Erik
    */
    public class FileHead {

    /**
    * @param args the command line arguments
    */
    public static void main(String[] args) throws IOException {

    String filename;



    //create keyboard
    Scanner keyboard = new Scanner(System.in);


    //get filename
    System.out.print("Please give filename: ");
    filename = keyboard.nextLine();

    //open file
    PrintWriter outputFile = new PrintWriter(filename);

    outputFile.println("one");
    outputFile.println("two");
    outputFile.println("three");
    outputFile.println("four");
    outputFile.println("five");

    //close
    outputFile.close();

    File file = new File(filename);
    Scanner inputFile = new Scanner(file);

    while (inputFile.hasNext())
    {
    String friendName = inputFile.nextLine();

    System.out.println(filename);
    }

    inputFile.close();
















    }
    }


  2. #2
    Junior Member
    Join Date
    Jun 2012
    Posts
    18
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Re: String question

    Can i see the output??

  3. #3
    Junior Member
    Join Date
    Jun 2012
    Posts
    8
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: String question

    Do not understand what you are asking I am a complete novice

  4. #4
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: String question

    I've never used Scanner, but a quick glance at the API doc for the class makes me think that 'hasNext' and 'nextLine' is not a good combination. 'hasNext' and 'next' seem better matched but not appropriate for you, since you're not (yet) interested in delimiters. There's a more appropriate method than 'hasNext' to test for more lines of input. Try that one and see if you get what you want.

    Put your code in code tags. See the "BB Code" link below.

  5. The Following User Says Thank You to Sean4u For This Useful Post:

    pierce21 (June 25th, 2012)

  6. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: String question

    Why is the program not displaying the lines with the strings
    There is no code to print those lines.
    Can you copy the console contents here from when you execute the program?

    On windows: To copy the contents of the command prompt window:
    Click on Icon in upper left corner
    Select Edit
    Select 'Select All' - The selection will show
    Click in upper left again
    Select Edit and click 'Copy'

    Paste here.

    I expect to see the name of the file printed several times.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Sean4u (June 25th, 2012)

  8. #6
    Junior Member
    Join Date
    Jun 2012
    Posts
    8
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: String question

    Im sorry I do not understand what you are suggesting besides BBCODE

  9. #7
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: String question

    Norm has done a better job than I have of reading your code

  10. #8
    Junior Member
    Join Date
    Jun 2012
    Posts
    8
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: String question

    Yes I am getting that inside of NetBean IDE. I want it to print One two three etc.

  11. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: String question

    Look at the name of the variable that you are reading the file's lines into
    and compare it to the name of the variable that is being printed.

    Think about what you see printed.
    If you don't understand my answer, don't ignore it, ask a question.

  12. #10
    Junior Member
    Join Date
    Jun 2012
    Posts
    29
    Thanks
    2
    Thanked 7 Times in 7 Posts

    Default friendName not filename

    CHECK THE LINE WHERE I PUT ARROW HEAD
     
     
    import java.io.*;
    import java.util.Scanner; 
     
     
    public class yourClass{
     
    /**
    * @param args the command line arguments
    */
    public static void main(String[] args) throws IOException {
     
    String filename;
     
     
     
    //create keyboard
    Scanner keyboard = new Scanner(System.in); 
     
     
    //get filename
    System.out.print("Please give filename: "); 
    filename = keyboard.nextLine(); 
     
    //open file
    PrintWriter outputFile = new PrintWriter(filename); 
     
    outputFile.println("one"); 
    outputFile.println("two");
    outputFile.println("three");
    outputFile.println("four");
    outputFile.println("five"); 
     
    //close
    outputFile.close();
     
    File file = new File(filename); 
    Scanner inputFile = new Scanner(file); 
    while (inputFile.hasNext())
    {
    String friendName = inputFile.nextLine(); 
     
    System.out.println(friendName);  //IT SHOULD BE friendName not filename. <<<<<<<<<---------------------------------
    }
    inputFile.close();	
    }}
    Last edited by kindk12; June 25th, 2012 at 05:39 PM.

  13. The Following User Says Thank You to kindk12 For This Useful Post:

    pierce21 (June 25th, 2012)

  14. #11
    Junior Member
    Join Date
    Jun 2012
    Posts
    8
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: String question

    Please tell me if it is obvious because I have been at this since 9am this morning

  15. #12
    Junior Member
    Join Date
    Jun 2012
    Posts
    8
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: String question

    Thanks. About to try right now.

Similar Threads

  1. Replies: 1
    Last Post: April 19th, 2012, 02:46 AM
  2. Question about Scanner and String values
    By cecilg23 in forum Java Theory & Questions
    Replies: 1
    Last Post: April 9th, 2012, 07:58 PM
  3. Question on String class
    By TP-Oreilly in forum Java Theory & Questions
    Replies: 8
    Last Post: September 25th, 2011, 08:12 PM
  4. Java array and string question
    By colerelm in forum Collections and Generics
    Replies: 2
    Last Post: September 14th, 2011, 11:50 PM
  5. [SOLVED] difference between String Concatenation and String -Buffer/Builder .append(<value>)
    By chronoz13 in forum Java Theory & Questions
    Replies: 5
    Last Post: September 3rd, 2011, 08:16 AM