How To Make The Program Jump To The Next Function..
Hi,
I have the following code:
Code :
private static LinkedList InitListA()
{
LinkedList ListA = new LinkedList();
String Temp = null;
int counter, buffer = 0;
for (counter = 0; counter < MAX; counter++)
{
System.out.println("Enter Some Integer Values For List 'A':");
BufferedReader InputStream = new BufferedReader(new InputStreamReader(System.in));
try
{
Temp = InputStream.readLine().toString();
buffer = Integer.parseInt(Temp);
ListA.add(new Integer(buffer));
}}
catch (IOException e)
{
e.printStackTrace();
}
}
return ListA;
}
I want InitListA() to terminate when <ENTER> is pressed with no strings keyed in.
How do I do it ?
Re: How To Make The Program Jump To The Next Function..
Hi kumarr, To make a program exit on enter, implement something like this
Code :
public static void main(String[]args){
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
while(true){
String s = null;
try {
s = br.readLine();
} catch (IOException e) {
}
if(s.length() == 0){
System.out.println("Exiting...");
System.exit(0);
}
// add your program logic here
}
}
cheers, wanderer(prit4u.wordpress.com)
Re: How To Make The Program Jump To The Next Function..
Quote:
Originally Posted by
Wanderer
Hi kumarr, To make a program exit on enter, implement something like this
Code :
public static void main(String[]args){
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
while(true){
String s = null;
try {
s = br.readLine();
} catch (IOException e) {
}
if(s.length() == 0){
System.out.println("Exiting...");
System.exit(0);
}
// add your program logic here
}
}
cheers, wanderer(prit4u.wordpress.com)
Hey, would it work with LinkedList type ? Cuz I was getting some exceptions earlier with this same method.
Re: How To Make The Program Jump To The Next Function..
Well if you post the final code here, I can probably look at it.
Re: How To Make The Program Jump To The Next Function..
Code :
public class ExitOnEnter {
public static void main(String[]args)
{
LinkedList ListA = new LinkedList();
int buffer;
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
while(true){
System.out.println("Press enter to exit");
String s = null;
try {
s = br.readLine().toString();
buffer = Integer.parseInt(s);
ListA.add(new Integer(buffer));
} catch (IOException e) {
e.printStackTrace();
}
if(s.length() == 0 || isStringOfSpaces(s)){
System.out.println("Exiting...");
System.exit(0);
}
}
}
private static boolean isStringOfSpaces(String s){
for(char c : s.toCharArray()){
if(c != ' ')
return false;
}
return true;
}
Re: How To Make The Program Jump To The Next Function..
System.exit() isn't a good way to terminate a program... it's much safer to simply return.
Re: How To Make The Program Jump To The Next Function..
Well if you look at what I wrote, I told you to implement the logic after the check of s.length.
You are getting an exception basically due to the parseInt().
You cannot convert an empty string to an integer :mad:
Anyway here is the final code.
Code :
public class ExitOnEnter {
public static void main(String[]args)
{
LinkedList ListA = new LinkedList();
int buffer;
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
while(true){
String s = null;
try {
s = br.readLine().toString();
} catch (IOException e) {
e.printStackTrace();
}
if(s.length() == 0 || isStringOfSpaces(s)){
System.out.println("ListA: "+ListA);
System.out.println("Exiting...");
return;
}else{
buffer = Integer.parseInt(s);
ListA.add(new Integer(buffer));
}
}
}
private static boolean isStringOfSpaces(String s){
for(char c : s.toCharArray()){
if(c != ' ')
return false;
}
return true;
}
}
Cheers,
Wanderer
[prit4u.wordpress.com]
Re: How To Make The Program Jump To The Next Function..
Thanks for helloworld922 for the advice