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

Thread: Exception catching, but i'm getting no output to the console?

  1. #1
    Junior Member
    Join Date
    Jun 2013
    Posts
    5
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Exception catching, but i'm getting no output to the console?

    Hello, i've been messing around with basic scripting in java, and now i'm trying to get exceptions down. Here is the code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package First;
     
    import java.util.InputMismatchException;
    import java.util.Scanner;
     
    /**
     *
     * @author Office
     */
    public class Constructorexample {
     
        public int total;
        public static int yourage;
        public static String yourname;
       static Scanner s = new Scanner(System.in);
     
        public static void getnameandage(String name, int age){
     
          int l = name.length();       
          int ax2 = age*2;
           System.out.println();
           System.out.println("The total length of your name is " + l);
           System.out.println();
           System.out.println("Your age x 2 = " + ax2);
        }
     
        public static void main(String[] args) 
        {
            System.out.println("Please enter your name:");
           try{
            yourname = s.nextLine();
    }catch (Exception e){
    System.out.println("Wrong input!");
    }
     
     
            System.out.println();
            System.out.println("Now please enter your age:");
            yourage = s.nextInt();
     
            getnameandage(yourname, yourage);
     
     
        }
     
     
    }

    I want to check if the user input a number instead of letters, and vice verse for the second prompt. I've tryed a try/catch block around yourname=s.nextLine(); and this stop the program from stopping, but it wont display text like I ask it to. Could someone help me out here? I also would like the program to ask the question again once the error is displayed, how would I do that? Thanks for any help!


  2. #2
    Junior Member
    Join Date
    May 2013
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Exception catching, but i'm getting no output to the console?

    Found this at Check if my int contains an int (try/catch) (Beginning Java forum at JavaRanch), granted, it deals with an int, but similiar concept with string:

    System.out.print("Please enter int number: ");
    while (!keyboard.hasNextInt()
    {
    keyboard.next();
    System.err.print("That wasn't an int number. Try again: ");
    }
    i = keyboard.nextInt();

  3. #3
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Exception catching, but i'm getting no output to the console?

    nextLine() is unlikely to throw an exception. If it does it will have nothing to do with the user having entered something that *could* be interpreted as a number. After all, there is no law actually forbidding a person's parents naming them "3.142" (and adopting the obvious familial nickname).

    If you want to check that the user does not enter something like that as their name you should parse what nextLine() returns as a double or int. Then if there is no exception thrown you know they have entered a numeral.

Similar Threads

  1. How to make console output bold
    By ColeTrain in forum Java Theory & Questions
    Replies: 6
    Last Post: October 16th, 2012, 09:54 AM
  2. Having problems with output to console....help!
    By toppcon in forum What's Wrong With My Code?
    Replies: 21
    Last Post: July 13th, 2011, 06:38 PM
  3. [SOLVED] Problems with Try/Catch Catching Wrong Exception
    By bgroenks96 in forum What's Wrong With My Code?
    Replies: 14
    Last Post: June 10th, 2011, 08:08 PM
  4. Quick question with throwing and catching an exception.
    By Andyandhisboard in forum What's Wrong With My Code?
    Replies: 6
    Last Post: May 12th, 2011, 10:24 PM
  5. Changing output from console to .html
    By RSYR in forum File I/O & Other I/O Streams
    Replies: 6
    Last Post: December 10th, 2009, 07:55 PM