944,030 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 11260
  • Java RSS
Nov 22nd, 2007
0

nextLine() terminates program

Expand Post »
Hello and Happy Thanksgiving (to those who celebrate it),

I have a small program that needs to read data off of a text file (name, city and age) and store it in an array. The program asks the user for a city name, and it's supposed to loop through the array and print out the names of people from that city. Now, my problem starts when I ask for the town to look for, since there are two-word towns (e.g. San Juan). I was using input.next() but it was only accepting the first word. I changed it to input.nextLine() and now it skips that part; it just exits the program.

Java Syntax (Toggle Plain Text)
  1.  
  2. import java.io.FileReader;
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.FileNotFoundException;
  6. import java.util.Scanner;
  7.  
  8. public class projectCOMP2315
  9. {
  10. public static void main( String [] args )
  11. {
  12. Scanner input = new Scanner (System.in);
  13.  
  14. // Declaracion de variables
  15. String nombreDocTXT = null,
  16. arregloTotalDatos [] = new String [600],
  17. stringPueblo = null,
  18. trimmedElement = null;
  19. int totalDatos = 0,
  20. opcion = 0,
  21. x = 0,
  22. nuevoIndice = 0;
  23. boolean flag = false;
  24.  
  25. try
  26. {
  27. System.out.print( "¿Cual es el nombre del archivo a leer? " );
  28. nombreDocTXT = input.next();
  29.  
  30. FileReader reader = new FileReader( nombreDocTXT );
  31. BufferedReader in = new BufferedReader( reader );
  32.  
  33. String leeUnoAUno = in.readLine();
  34.  
  35. while( leeUnoAUno != null )
  36. {
  37. arregloTotalDatos[ totalDatos ] = leeUnoAUno;
  38. leeUnoAUno = in.readLine();
  39. totalDatos = totalDatos + 1;
  40. }
  41.  
  42. System.out.print(" \n");
  43. for ( x = 1 ; x <= 45 ; x++ )
  44. {
  45. System.out.print( "*" );
  46. }
  47. System.out.print(" M E N U ");
  48. for ( x = 1 ; x <= 45 ; x++ )
  49. {
  50. System.out.print( "*" );
  51. }
  52. System.out.print(" \n1. Print the names of people from a particular town. ");
  53. System.out.print(" \n2. Imprimir el nombre de todas …. ");
  54. System.out.print(" \n3. Imprimir los nombres de todas …. ");
  55. System.out.print(" \n4. Imprimir la lista completa de ….. ");
  56. System.out.print(" \n5. Indicar cuántas personas hay en un …… ");
  57. System.out.print(" \n");
  58. for ( x = 1 ; x <= 101 ; x++ )
  59. {
  60. System.out.print( "*" );
  61. }
  62. System.out.print(" \n9. Salir del programa ");
  63. System.out.print(" \n");
  64. for ( x = 1 ; x <= 101 ; x++ )
  65. {
  66. System.out.print( "*" );
  67. }
  68. System.out.print(" \nSeleccion: ");
  69. opcion = input.nextInt();
  70.  
  71. in.close( );
  72. }
  73.  
  74. catch( FileNotFoundException filenotfoundexxption )
  75. {
  76. System.out.println( nombreDocTXT + " no existe." );
  77. }
  78.  
  79. catch( IOException ioexception )
  80. {
  81. ioexception.printStackTrace( );
  82. }
  83.  
  84. switch (opcion)
  85. {
  86. case 1:
  87. {
  88. System.out.println( "\n¿De que pueblo desea buscar informacion? " );
  89. String pueblo = input.nextLine();
  90.  
  91. stringPueblo = pueblo.trim();
  92.  
  93. for ( x = 0 ; x < arregloTotalDatos.length ; x++ )
  94. {
  95. trimmedElement = arregloTotalDatos[ x ].trim();
  96.  
  97. if ( (stringPueblo) .equals (trimmedElement) )
  98. {
  99. nuevoIndice = x - 1;
  100. System.out.println(arregloTotalDatos[ nuevoIndice ]);
  101. flag = true;
  102. }
  103. }
  104.  
  105. if ( flag == false )
  106. {
  107. System.out.println( pueblo + "\nNo hay un record con ese pueblo." );
  108. }
  109. }
  110. case 9:
  111. {
  112. break;
  113. }
  114. } // termina switch
  115. } // termina main
  116. } // termina projectCOMP2315
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Java-Newbie is offline Offline
14 posts
since Nov 2007
Nov 22nd, 2007
0

Re: nextLine() terminates program

I just read cscgal message about people posting their homework problems in hopes of getting a quick solution. Just wanted you to know that I've tried everything I can think of to get a solution for this, you were my last resort.

I tried using two next() statements but it didn't work for single-word towns since it was expecting the user to write a second word...
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Java-Newbie is offline Offline
14 posts
since Nov 2007
Nov 23rd, 2007
0

Re: nextLine() terminates program

Have you used Scanner.nextLine()??
Reputation Points: 16
Solved Threads: 11
Junior Poster in Training
lookof2day is offline Offline
83 posts
since Aug 2007
Nov 23rd, 2007
0

Re: nextLine() terminates program

I tried that and got " non-static method nextLine() cannot be referenced from a static context". Thanks... :-)
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Java-Newbie is offline Offline
14 posts
since Nov 2007
Nov 24th, 2007
0

Re: nextLine() terminates program

if you didn't got the exception here
Java Syntax (Toggle Plain Text)
  1. nombreDocTXT = input.next();
then you can't get it when calling nextLine(); because it's just another method of Scanner class. However, can you provide your updated code that threw exception??
Reputation Points: 16
Solved Threads: 11
Junior Poster in Training
lookof2day is offline Offline
83 posts
since Aug 2007
Nov 24th, 2007
0

Re: nextLine() terminates program

>Now, my problem starts when I ask for the town to look for, since there are two-word towns (e.g. San Juan).

It's pretty simple. Read in the file as lines.

Take that line, assign it to a String then use the appropriate 'string find' methods to get the part you want.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Nov 24th, 2007
0

Re: nextLine() terminates program

This was interesting. It was really skipping the readLine(). So try this..
Java Syntax (Toggle Plain Text)
  1. System.out.println( "\n¿De que pueblo desea buscar informacion? " );
  2. input = new Scanner(System.in);
  3. String pueblo = input.nextLine();
It will work. I'm still trying to know the reason why it was skipping the instruction.

You will have to use some different logic here
Java Syntax (Toggle Plain Text)
  1. nuevoIndice = x - 1;
  2. System.out.println(arregloTotalDatos[ nuevoIndice ]);
because you are reducing the current index 0 to -1 and trying to print element on -1st index...
[/code]
Reputation Points: 16
Solved Threads: 11
Junior Poster in Training
lookof2day is offline Offline
83 posts
since Aug 2007
Nov 26th, 2007
0

Re: nextLine() terminates program

lookof2day, thanks for your input, that did the trick... The reason I have this code here

Java Syntax (Toggle Plain Text)
  1. nuevoIndice = x - 1;
  2. System.out.println(arregloTotalDatos[ nuevoIndice ]);

is because the data in the text file is in the format,
Name
Town
Age
Name
Town
Age
etc....

so I already know that if I find a town with that name (e.g. San Juan), the element above it is a person's name.

Thanks everybody for reading my post and helping out...
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Java-Newbie is offline Offline
14 posts
since Nov 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: transparent frame
Next Thread in Java Forum Timeline: homework help





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC