| | |
nextLine() terminates program
Thread Solved |
•
•
Join Date: Nov 2007
Posts: 14
Reputation:
Solved Threads: 0
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.
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)
import java.io.FileReader; import java.io.BufferedReader; import java.io.IOException; import java.io.FileNotFoundException; import java.util.Scanner; public class projectCOMP2315 { public static void main( String [] args ) { Scanner input = new Scanner (System.in); // Declaracion de variables String nombreDocTXT = null, arregloTotalDatos [] = new String [600], stringPueblo = null, trimmedElement = null; int totalDatos = 0, opcion = 0, x = 0, nuevoIndice = 0; boolean flag = false; try { System.out.print( "¿Cual es el nombre del archivo a leer? " ); nombreDocTXT = input.next(); FileReader reader = new FileReader( nombreDocTXT ); BufferedReader in = new BufferedReader( reader ); String leeUnoAUno = in.readLine(); while( leeUnoAUno != null ) { arregloTotalDatos[ totalDatos ] = leeUnoAUno; leeUnoAUno = in.readLine(); totalDatos = totalDatos + 1; } System.out.print(" \n"); for ( x = 1 ; x <= 45 ; x++ ) { System.out.print( "*" ); } System.out.print(" M E N U "); for ( x = 1 ; x <= 45 ; x++ ) { System.out.print( "*" ); } System.out.print(" \n1. Print the names of people from a particular town. "); System.out.print(" \n2. Imprimir el nombre de todas …. "); System.out.print(" \n3. Imprimir los nombres de todas …. "); System.out.print(" \n4. Imprimir la lista completa de ….. "); System.out.print(" \n5. Indicar cuántas personas hay en un …… "); System.out.print(" \n"); for ( x = 1 ; x <= 101 ; x++ ) { System.out.print( "*" ); } System.out.print(" \n9. Salir del programa "); System.out.print(" \n"); for ( x = 1 ; x <= 101 ; x++ ) { System.out.print( "*" ); } System.out.print(" \nSeleccion: "); opcion = input.nextInt(); in.close( ); } catch( FileNotFoundException filenotfoundexxption ) { System.out.println( nombreDocTXT + " no existe." ); } catch( IOException ioexception ) { ioexception.printStackTrace( ); } switch (opcion) { case 1: { System.out.println( "\n¿De que pueblo desea buscar informacion? " ); String pueblo = input.nextLine(); stringPueblo = pueblo.trim(); for ( x = 0 ; x < arregloTotalDatos.length ; x++ ) { trimmedElement = arregloTotalDatos[ x ].trim(); if ( (stringPueblo) .equals (trimmedElement) ) { nuevoIndice = x - 1; System.out.println(arregloTotalDatos[ nuevoIndice ]); flag = true; } } if ( flag == false ) { System.out.println( pueblo + "\nNo hay un record con ese pueblo." ); } } case 9: { break; } } // termina switch } // termina main } // termina projectCOMP2315
•
•
Join Date: Nov 2007
Posts: 14
Reputation:
Solved Threads: 0
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...
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...
•
•
Join Date: Aug 2007
Posts: 80
Reputation:
Solved Threads: 10
if you didn't got the exception here
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??
Java Syntax (Toggle Plain Text)
nombreDocTXT = input.next();
>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
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. *Voted best profile in the world*
•
•
Join Date: Aug 2007
Posts: 80
Reputation:
Solved Threads: 10
This was interesting. It was really skipping the readLine(). So try this..
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
because you are reducing the current index 0 to -1 and trying to print element on -1st index...
[/code]
Java Syntax (Toggle Plain Text)
System.out.println( "\n¿De que pueblo desea buscar informacion? " ); input = new Scanner(System.in); String pueblo = input.nextLine();
You will have to use some different logic here
Java Syntax (Toggle Plain Text)
nuevoIndice = x - 1; System.out.println(arregloTotalDatos[ nuevoIndice ]);
[/code]
•
•
Join Date: Nov 2007
Posts: 14
Reputation:
Solved Threads: 0
lookof2day, thanks for your input, that did the trick... The reason I have this code here
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...
Java Syntax (Toggle Plain Text)
nuevoIndice = x - 1; 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...
![]() |
Similar Threads
- need help with 16 - bit calculator (Assembly)
- where should i put the cin.get();?? (C++)
- Is there any more Documenation that I need? (C++)
- Help with Java letter counter (Java)
- How to use Dev C++ (C++)
- Assembly newbie question (Assembly)
- c++ compile warnings (C++)
- Dev-C++ Question (C++)
- Need Help counting Array Length (C++)
Other Threads in the Java Forum
- Previous Thread: transparent frame
- Next Thread: homework help
| Thread Tools | Search this Thread |
actuate android api applet application applications array arrays automation balls bank binary bluetooth business c++ chat class clear client code codesnippet collections component coordinates database defaultmethod development dice doctype dragging ebook eclipse educational error formatingtextintooltipjava fractal froglogic game givemetehcodez graphics gui hql html ide image infinite ingres input integer internet intersect invokingapacheantprogrammatically j2me java javaprojects jni jpanel jtextarea julia linux list loop looping map method methods mobile mysql netbeans newbie openjavafx parameter php problem program programming project recursion repositories scanner scrollbar server set sms sort sorting sql sqlserver state storm string sun superclass swing swt text-file threads tree websites windows






