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

Thread: How To Make The Program Jump To The Next Function..

  1. #1
    Junior Member
    Join Date
    Feb 2010
    Posts
    18
    My Mood
    Amazed
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Question How To Make The Program Jump To The Next Function..

    Hi,

    I have the following 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 ?


  2. #2
    Junior Member
    Join Date
    Mar 2010
    Location
    Mauritius
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How To Make The Program Jump To The Next Function..

    Hi kumarr, To make a program exit on enter, implement something like this
    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)

  3. #3
    Junior Member
    Join Date
    Feb 2010
    Posts
    18
    My Mood
    Amazed
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: How To Make The Program Jump To The Next Function..

    Quote Originally Posted by Wanderer View Post
    Hi kumarr, To make a program exit on enter, implement something like this
    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.

  4. #4
    Junior Member
    Join Date
    Mar 2010
    Location
    Mauritius
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

  5. #5
    Junior Member
    Join Date
    Feb 2010
    Posts
    18
    My Mood
    Amazed
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: How To Make The Program Jump To The Next Function..

     
    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;
    	}

  6. #6
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default 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.

  7. #7
    Junior Member
    Join Date
    Mar 2010
    Location
    Mauritius
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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

    Anyway here is the final 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]

  8. #8
    Junior Member
    Join Date
    Mar 2010
    Location
    Mauritius
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How To Make The Program Jump To The Next Function..

    Thanks for helloworld922 for the advice

Similar Threads

  1. how to make a program take time...
    By DLH112 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 10th, 2010, 07:09 PM
  2. Help with a program to make a landscape picture
    By noseeds in forum Java Theory & Questions
    Replies: 1
    Last Post: December 15th, 2009, 10:25 PM
  3. How to make user press enter to continue in program?
    By BC2210 in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: October 3rd, 2009, 05:08 AM
  4. Replies: 9
    Last Post: June 27th, 2009, 04:05 PM
  5. Java program to write game
    By GrosslyMisinformed in forum Paid Java Projects
    Replies: 3
    Last Post: January 27th, 2009, 03:33 PM