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

Thread: using java.util.InputMismatchException with try

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default using java.util.InputMismatchException with try

    iv writen this code in order to prevent an unexpected ending of program in case there is an input mismatch
    static int smartInputInt ()
    	{
    		boolean flag=true;
    		int end=-10;
    		while (flag)
    		{
    			flag=false;
    			try
    			{
    				end=reader.nextInt ();
    			}
    			catch (java.util.InputMismatchException e)
    			{
    				System.out.println ("Intput Error !");
    				flag=true;
    			}
    		}
    		return end;
     
    	}
    the problem with this code is that if a wrong input is inserted it causes a loop that never ends that prints "Intput Error !"
    how can i fix this problem?

  2. #2
    Member
    Join Date
    Jun 2011
    Location
    Rhode Island
    Posts
    69
    My Mood
    Bored
    Thanks
    11
    Thanked 7 Times in 6 Posts

    Default Re: using java.util.InputMismatchException with try

    remove line

    flag=true;
    in your catch() method
    this way you only fall through it once. Your in a while loop while true...

  3. #3
    Junior Member
    Join Date
    Oct 2011
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: using java.util.InputMismatchException with try

    i want the loop to keep going until the right type of input will be inseted
    if i remove flag=true; there is no need for a loop since it will be over the first time
    Last edited by itayj; October 24th, 2011 at 03:49 PM.

  4. #4
    Member
    Join Date
    Jun 2011
    Location
    Rhode Island
    Posts
    69
    My Mood
    Bored
    Thanks
    11
    Thanked 7 Times in 6 Posts

    Default Re: using java.util.InputMismatchException with try

    I think this is a good example for a do-while loop then, set the boolean within the if-else statements

    static int smartInputInt (){
       boolean flag=false;//<--- do one time unless turned to true
       int end;
       do{
          try{
     
    	 end=reader.nextInt (); //<-- somewhere here is where you should stop to read inputlines
    // I loose your thought pattern here not sure where this exception comes in 
    // you need if something re-read input if NULL
          }catch (java.util.InputMismatchException e){
    	     System.out.println ("Intput Error !");
    	     flag=true;
          }
       }while(flag);
     
       return end;	
    }
    Last edited by william; October 24th, 2011 at 03:57 PM. Reason: added notes

  5. #5
    Junior Member
    Join Date
    Oct 2011
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: using java.util.InputMismatchException with try

    you do not understand the problem
    it has nothing to do with the loop.
    try running it yourself
    you will get this output if u will enter any other type of input then int:
    Intput Error !
    Intput Error !
    Intput Error !
    Intput Error !
    Intput Error !
    Intput Error !
    Intput Error !
    Intput Error !
    Intput Error !
    Intput Error !
    Intput Error !
    Intput Error !
    Intput Error !
    Intput Error !
    Intput Error !
    Intput Error !
    Intput Error !
    Intput Error !
    Intput Error !
    Intput Error !
    Intput Error !
    Intput Error !
    Intput Error !
    Intput Error !
    Intput Error !
    Intput Error !
    Intput Error !
    Intput Error !
    Intput Error !
    Intput Error !
    Intput Error !
    Intput Error !
    Intput Error !
    Intput Error !
    Intput Error !
    Intput Error !
    Intput Error !
    Intput Error !
    Intput Error !
    Intput Error !
    Intput Error !
    Intput Error !
    Intput Error !
    Intput Error !
    Intput Error !
    Intput Error !
    Intput Error !
    Intput Error !
    Intput Error !
    Intput Error !
    Intput Error !
    Intput Error !
    Intput Error !
    Intput Error !
    Intput Error !
    Intput Error !
    Intput Error !
    Intput Error !
    Intput Error !
    Intput Error !
    Intput Error !
    Intput Error !
    Intput Error !
    Intput Error !
    Intput Error !
    Intput Error !
    Intput Error !
    Intput Error !
    Intput Error !
    Intput Error !
    Intput Error !
    Intput Error !
    Intput Error !
    Intput Error !
    Intput Error !
    Intput Error !
    Intput Error !
    Intput Error !
    Intput Error !
    Intput Error !
    Intput Error !
    Intput Error !
    Intput Error !
    Intput Error !
    Intput Error !
    Last edited by itayj; October 24th, 2011 at 04:13 PM.

  6. #6
    Member
    Join Date
    Jun 2011
    Location
    Rhode Island
    Posts
    69
    My Mood
    Bored
    Thanks
    11
    Thanked 7 Times in 6 Posts

    Default Re: using java.util.InputMismatchException with try

    I do understand. You don't stop to read the input line when in the Reader. you never clear out the IO stream out so you always get get same input. Use a if statement in the try catch to stop this. or clear out the stream and/or get another user inputs.

    If coding was easy then anyone can do it.

    add the code all of it just working with a method I am not going to write the code I have other code to write.

    JFreeChart crap....

    want to see my test stuff. this test was to get it working on its own then import it to my project. I should have 3 of these charts stacked on top of each other were the display shows multiple lines. Each chart has different user controls.
    package components;
     
    import java.awt.Dimension;
    import java.awt.HeadlessException;
     
    import java.util.ArrayList;
    import java.util.Iterator;
    import java.util.Random;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import org.jfree.chart.ChartFactory;
    import org.jfree.chart.ChartFrame;
    import org.jfree.chart.JFreeChart;
    import org.jfree.chart.plot.PlotOrientation;
    import org.jfree.data.xy.XYSeries;
    import org.jfree.data.xy.XYSeriesCollection;
     
    /**
     *
     * @author William
     */
    public class XYPlotJfreeChart extends JFrame {
     
        JPanel panel;
        XYSeries series1;
        ArrayList<Point2D> list;
        Random ran = new Random();
     
        public XYPlotJfreeChart() {
     
     
    //        XYPlotJfreeChart chart = new XYPlotJfreeChart();
            panel = new JPanel();
            series1 = new XYSeries("first");
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
            getData();
     
    XYSeriesCollection dataset = new XYSeriesCollection();
    dataset.addSeries(series1);
     
    JFreeChart panel = ChartFactory.createXYAreaChart(
            "MyTitle", //title
            "X", //x axis label
            "Y", //y axis label
            dataset, //data set
            PlotOrientation.HORIZONTAL,
            true, //show legend
            true,  //use tooltips
            false); //configure chart to generate url's
    ChartFrame frameChart = new ChartFrame("MyChart Title", panel);
     
    frameChart.setVisible(true);
    frameChart.setSize(400, 400);
     
        }
     
        public static void main(String[] args) {
            XYPlotJfreeChart xy = new XYPlotJfreeChart();
    //        Dimension d = new Dimension(400, 400);
    //        xy.setMinimumSize(d);
    //        xy.setMaximumSize(d);
    //        xy.setVisible(true);
        }
     
        private void init() {
     
     
        }
     
        private void getData() {
            list = new ArrayList<Point2D>();
            longPause();
    long time = 0;
            for (int i = 0; i < 30; i++) {
     
                Point2D points = new Point2D() {
     
                    long time;
                    double x;
                    double y;
     
                    @Override
                    public double getX() {
                        return x;
                    }
     
                    @Override
                    public double getY() {
                        return y;
                    }
     
                    public long getTime() {
                        return time;
                    }
     
                    public void setTime(long t) {
                        time = t;
                    }
     
                    @Override
                    public void setLocation(double d, double d1) {
                        x = d;
                        y = d1;
                    }
                };
     
                points.setLocation(ran.nextDouble() * 180, ran.nextDouble() * 180);
                points.setTime(time);
                list.add(points);
    //            System.out.println("x point "+ points.getX());
    //            System.out.println("y point " + points.getY());
     
                time += 15;
                series1.add(points.x, points.y);
            }
     
        }
     
        private void printList() {
            Iterator<Point2D> l = list.listIterator();
     
            while (l.hasNext()) {
                Point2D p = l.next();
                System.out.println("");
                System.out.println("TIME " +p.getTime());
                System.out.println("x point " + p.getX());
                System.out.println("y point " + +p.getY());
     
            }
        }
     
        private void longPause() {
    //        System.out.println("paused ");
            for (int i = 0; i < 1000000; i++) {
                //do nothing just a counting loop
            }
        }
    }
     
    class Point2D {
     
        long time;
        double x;
        double y;
     
        public double getX() {
            return x;
        }
     
        public double getY() {
            return y;
        }
     
        public long getTime() {
            return time;
        }
     
        public void setTime(long t) {
            time = t;
        }
     
        public void setLocation(double d, double d1) {
            x = d;
            y = d1;
        }
    }

  7. #7
    Junior Member
    Join Date
    Oct 2011
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: using java.util.InputMismatchException with try

    this is a lot more complexed then i thought it would be i think i will pass since i cant understand most of this code

  8. #8
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: using java.util.InputMismatchException with try

    Well, part of the reason you don't understand his code is because it's not documented well. Also, your longPause method should just sleep the thread, otherwise you risk crashing/overloading the poor users game.. I generally do create a second method instead of constantly using a try-catch statement. (I personally think that it should be an error, not an exception).

    /**
     * Delays the program for the specified amount of seconds.  Handles InterruptedException with a call to printStackTrace() and returning
    */
    public void longPause(int seconds)
    {
      long delay = (long)seconds * 1000;
      try
      {
         Thread.sleep(delay);
      }catch(InterruptedException e)
      {
         e.printStackTrace();
         return;
      }
    }

    Just scanned most of the code, that stood out.

  9. #9
    Member
    Join Date
    Jun 2011
    Location
    Rhode Island
    Posts
    69
    My Mood
    Bored
    Thanks
    11
    Thanked 7 Times in 6 Posts

    Default Re: using java.util.InputMismatchException with try

    Its not documented cause i wrote the entire thing.

    Why would you think I don't under stand this code?

    Its junk code, I use test each component before adding it to the project. This tiny peace is not even close to what I am building. This is just for display to correct my values.

  10. #10
    Member
    Join Date
    Jun 2011
    Location
    Rhode Island
    Posts
    69
    My Mood
    Bored
    Thanks
    11
    Thanked 7 Times in 6 Posts

    Default Re: using java.util.InputMismatchException with try

    well I finished you answer.....

    my Outputs:
    run:
    in here
    7
    end is 7
    in here
    8
    end is 8
    in here
    8
    end is 8
    in here
    9
    end is 9
    in here
    4
    end is 4
    in here
    d
    Intput Error !java.util.InputMismatchException
    end is 4

    would guess that is what you are going for???
    Last edited by william; October 24th, 2011 at 05:02 PM.

  11. #11
    Member
    Join Date
    Jun 2011
    Location
    Rhode Island
    Posts
    69
    My Mood
    Bored
    Thanks
    11
    Thanked 7 Times in 6 Posts

    Default Re: using java.util.InputMismatchException with try

    Im going home:: have a good night

            Scanner reader = new Scanner(System.in);
     
            boolean flag = true;
            int end = -10 ;
     
           do{
                try {
                    System.out.println("in here");
                    end = reader.nextInt();
                    System.out.println("end is " + end);
                } catch (InputMismatchException e) {
                    System.out.println("Intput Error !" +e.toString());
                    System.out.println("end is " + end);
                    flag = false;
                }
            }while (flag);
        }

  12. #12
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: using java.util.InputMismatchException with try

    You should always document anything you create.
    Quote Originally Posted by william
    Its not documented cause i wrote the entire thing.
    Which is exactly why it should be documented.

  13. #13
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: using java.util.InputMismatchException with try

    This thread is being driven off topic from the original poster's question

    Original poster, if your question is still not answered please do say so - if there was a misunderstanding I suggest perhaps more explanation to the problem.

    Others - let us please stay on topic relative to the original poster's question. The conversation is meaningful, so I recommend starting another thread or taking this offline in a PM or profile conversation.

  14. The Following User Says Thank You to copeg For This Useful Post:

    william (October 24th, 2011)

  15. #14
    Member
    Join Date
    Jun 2011
    Location
    Rhode Island
    Posts
    69
    My Mood
    Bored
    Thanks
    11
    Thanked 7 Times in 6 Posts

    Default Re: using java.util.InputMismatchException with try

    removed to stay on topic..

    Copeg how to delete a post?
    Last edited by william; October 24th, 2011 at 06:40 PM. Reason: ask question

  16. #15
    Junior Member
    Join Date
    Oct 2011
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: using java.util.InputMismatchException with try

    well, i didnt understand this code since the only import i know is java.util.*

  17. #16
    Junior Member
    Join Date
    Oct 2011
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: using java.util.InputMismatchException with try

    yes,my question hasnt queit been answered , i would like to know if there is a simpler way of making a loop with java.util.InputMismatchException in try.
    somthing that doesn't involve any complexed import.i'm just a high school student and i wants to make a simple checkers game
    more efficient so that i could get some extra credit on the assignment

  18. #17
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: using java.util.InputMismatchException with try

    Quote Originally Posted by itayj View Post
    yes,my question hasnt queit been answered , i would like to know if there is a simpler way of making a loop with java.util.InputMismatchException in try.
    somthing that doesn't involve any complexed import.i'm just a high school student and i wants to make a simple checkers game
    more efficient so that i could get some extra credit on the assignment
    The loop you posted, and the do/while posted by william is just about as simple as it gets. If you are looking for ways to improve, consider other ways to improve the code. For example usability: what if the user wants to quit (these code snippets will continue to loop until a number is put in)?

Similar Threads

  1. Replies: 3
    Last Post: April 26th, 2011, 02:51 AM
  2. [SOLVED] InputMisMatchException
    By jmack in forum Exceptions
    Replies: 3
    Last Post: January 26th, 2011, 10:30 AM
  3. java.util.ConcurrentModificationException Issue
    By joshuaa in forum Collections and Generics
    Replies: 1
    Last Post: December 2nd, 2010, 04:41 PM
  4. INPUTMISMATCHEXCEPTION
    By james in forum What's Wrong With My Code?
    Replies: 7
    Last Post: February 15th, 2010, 01:02 AM
  5. Replies: 4
    Last Post: April 29th, 2009, 10:53 AM