Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-26-2007, 09:39 PM
Member
 
Join Date: Nov 2007
Location: On the computer
Posts: 2
finlandssvensk is on a distinguished road
"Error in number, try again."
I'm supposed to write a program where the user types in a number, and then the program either adds the number to the result (if the number typed was odd), or divides the result by the number typed in (if the number typed was even, and 0 is even). There are two types of errors that can occur:
- if the user types in 0 -> division by 0 (I got that to do what I want)
- if the user types in something other than a number (i.e. letters) -> the program should write out an error message and then quit
Right now if the user types in letters or something, they get a message "Error in number, try again." and then the program keeps going. Here is the code:

Code:
import java.io.*; import javagently.*; public class Uppg8_CS { /** * @param args */ public static void main(String[] args) throws IOException { int tal = 0; Nummer resultat = new Nummer(0); System.out.println("Resultatet är: " + resultat.varde()); // System.out.println("T E S T"); while (tal!=99) { try { // System.out.println("T E S T"); BufferedReader in = Text.open(System.in); System.out.print("Mata in ett tal (99 avslutar): "); tal = Text.readInt(in); if (tal%2 == 0) { resultat.dividera(tal); } else { resultat.addera(tal); } // end of if-else System.out.println("Resultatet är nu: " + resultat.varde()); } // end of try /* takes care of when 0 is typed in */ catch (ArithmeticException a) { System.out.println("Ojdå, division med noll! Nytt försök..."); } // end of catch } // end of while } // end of main } /* a class Nummer which I'm supposed to use */ class Nummer { private int num = 0; public Nummer(int tal) { this.num = tal; } // Nummer public void addera(int tal) { this.num = this.num + tal; } // addera public void dividera(int tal) { this.num = this.num / tal; } // dividera public int varde() { return this.num; } }
And the code for the package javagently:

Code:
public class Text { /* some code irrelevant to this... */ public static int readInt (BufferedReader in) throws IOException { if (T==null) refresh(in); while (true) { try { return Integer.parseInt(T.nextToken()); } catch (NoSuchElementException e1) { refresh (in); } catch (NumberFormatException e2) { System.out.println("Error in number, try again."); } } } /* some more irrelevant code... */
So my question is: how do I make so my error comes instead of the automatic one? and how do I make the program quit if that error comes?
Thanks a lot,
Christa

**Edit**
PS tell me if you need something translated...
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 11-27-2007, 12:36 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,222
hardwired is on a distinguished road
Some of the trouble in this is that your Text.readInt method takes care of the non–digit entry in its while loop (with NumberFormatException). Assuming you cannot rewrite/alter Text you can implement your own reader and check for non–numeric input.
Code:
import java.io.*; public class U8CS { public static void main(String[] args) throws IOException { int tal = 0; Nummer resultat = new Nummer(0); System.out.println("Resultatet är: " + resultat.varde()); try { BufferedReader br = new BufferedReader( new InputStreamReader(System.in)); while (tal!=99) { System.out.print("Mata in ett tal (99 avslutar): "); String line = br.readLine(); if(!isParsable(line)) System.exit(1); tal = Integer.parseInt(line); if(tal == 0) { System.out.println("Ojdå, division med noll! Nytt försök..."); continue; // try again } else if (tal%2 == 0) { resultat.dividera(tal); } else { resultat.addera(tal); } System.out.println("Resultatet är nu: " + resultat.varde()); } br.close(); } catch (IOException e) { System.out.println("Read error: " + e.getMessage()); } } private static boolean isParsable(String s) { System.out.println("s = " + s); for(int j = 0; j < s.length(); j++) { if(!Character.isDigit(s.charAt(j))) { System.out.printf("charAt(%d) = %s is not a digit%n", j, s.charAt(j)); return false; } } return true; } }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to solve "No compiler error"? iceman New To Java 5 04-22-2008 05:37 AM
"Cannont find symbol Constructor" error Welsh New To Java 7 01-25-2008 02:12 AM
Strange error message "Source not found" ppayal Eclipse 0 11-25-2007 08:19 PM
Syntax error on token "(", ; expected baltimore AWT / Swing 1 08-01-2007 01:34 AM
Error: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException romina New To Java 1 07-26-2007 12:55 AM


All times are GMT +3. The time now is 03:05 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org