קלט מהמשתמש ב JAVA איך מבצעים קלט של לתוך משתנה integer מהמשתמש? ניסתי איזה קוד אבל כנראה שזה לא זה.
A adventureboy New member 27/1/04 #1 קלט מהמשתמש ב JAVA איך מבצעים קלט של לתוך משתנה integer מהמשתמש? ניסתי איזה קוד אבל כנראה שזה לא זה.
I i c e b e r g New member 28/1/04 #2 -----> public static int readInt(){ while(true){ try{ return Integer.valueOf (readString().trim()).intValue(); } catch(NumberFormatException e){ System.out.println ("Not an integer. Please try again!"); } } } public static String readString(){ int ch; String r = ""; boolean done = false; while (!done){ try{ ch = System.in.read(); if (ch < 0 || (char)ch == '\n') done = true; else if ((char)ch != '\r') r = r + (char) ch; } catch(java.io.IOException e){ done = true; } } return r; }
-----> public static int readInt(){ while(true){ try{ return Integer.valueOf (readString().trim()).intValue(); } catch(NumberFormatException e){ System.out.println ("Not an integer. Please try again!"); } } } public static String readString(){ int ch; String r = ""; boolean done = false; while (!done){ try{ ch = System.in.read(); if (ch < 0 || (char)ch == '\n') done = true; else if ((char)ch != '\r') r = r + (char) ch; } catch(java.io.IOException e){ done = true; } } return r; }