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
Code :
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?
Re: using java.util.InputMismatchException with try
remove line
in your catch() method
this way you only fall through it once. Your in a while loop while true...
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
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
Code Java:
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;
}
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:
Code :
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 !
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.
Code Java:
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;
}
}
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
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).
Code Java:
/**
* 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.
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.
Re: using java.util.InputMismatchException with try
well I finished you answer.....
my Outputs:
Code Java:
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???
Re: using java.util.InputMismatchException with try
Im going home:: have a good night
Code Java:
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);
}
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.
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.
Re: using java.util.InputMismatchException with try
removed to stay on topic..
Copeg how to delete a post?
Re: using java.util.InputMismatchException with try
well, i didnt understand this code since the only import i know is java.util.* :P
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
Re: using java.util.InputMismatchException with try
Quote:
Originally Posted by
itayj
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)?