Results 1 to 2 of 2
  1. #1
    Join Date
    Nov 2007
    Location
    On the computer
    Posts
    2
    Rep Power
    0

    Default "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:

    Java 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:

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

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

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

Similar Threads

  1. Syntax error on token "(", ; expected
    By baltimore in forum AWT / Swing
    Replies: 3
    Last Post: 10-28-2009, 12:19 AM
  2. How to solve "No compiler error"?
    By iceman in forum New To Java
    Replies: 5
    Last Post: 04-22-2008, 03:37 AM
  3. "Cannont find symbol Constructor" error
    By Welsh in forum New To Java
    Replies: 7
    Last Post: 01-25-2008, 12:12 AM
  4. Strange error message "Source not found"
    By ppayal in forum Eclipse
    Replies: 0
    Last Post: 11-25-2007, 06:19 PM
  5. Replies: 1
    Last Post: 07-25-2007, 10:55 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •