Results 1 to 6 of 6
  1. #1
    ahmedzica is offline Member
    Join Date
    Dec 2011
    Posts
    7
    Rep Power
    0

    Default Check out my code

    Hello everybody,

    I'm trying to write a program that takes a number from 1 to 10 as an input and returns it to String form as an output (i.e. input=1, output=one)
    I've already written my code. but it's not working. can you cech it?

    Java Code:
    package writenumbers;
    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.io.IOException;
    
    public class Main {
    
       
        public static void main(String[] args) {
           
            BufferedReader dataIn = new BufferedReader (new InputStreamReader(System.in));
           
            int num;
    
            System.out.println("Insert A number from 1 to 10");
    
            String[] writeNumber={"One","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten"};
    
           try{
               num=dataIn.read();
               for (int i=1;i<=10;i++){
                    if(num==i){
                    System.out.println("You've entered"+ writeNumber[i]);
                    }
               }
           }catch (IOException e){}
    
           
    
    
        }
    
    }

    Waiting for your response,
    Thanks

  2. #2
    ahmedzica is offline Member
    Join Date
    Dec 2011
    Posts
    7
    Rep Power
    0

    Default Re: Check out my code

    I'd like also to know how to separate the input I'm getting from the user in order to calculate the average of three numbers as a "Grades"

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

    Default Re: Check out my code

    how to separate the input I'm getting from the user
    How is the input being done by the user?
    Does it come all on one line separated by spaces or ; or ??? what
    or is each number on a separate line?

    What values does the read() method return? Are they usable by your code? Are they what you expect?

    You should look at using the Scanner class. It has several methods that make reading input from users easier.

  4. #4
    ahmedzica is offline Member
    Join Date
    Dec 2011
    Posts
    7
    Rep Power
    0

    Default Re: Check out my code

    Quote Originally Posted by Norm View Post
    How is the input being done by the user?
    Does it come all on one line separated by spaces or ; or ??? what
    or is each number on a separate line?

    What values does the read() method return? Are they usable by your code? Are they what you expect?

    You should look at using the Scanner class. It has several methods that make reading input from users easier.
    I got it thanks :)

  5. #5
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default Re: Check out my code

    You have a problem here:

    String[] writeNumber={"One","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten"};
    num=dataIn.read();
    for (int i=1;i<=10;i++){
    if(num==i){
    System.out.println("You've entered"+ writeNumber[i]);
    }
    }

    Running this code would give you an ArrayIndexOutOfBoundsException when i==10.

    Also, it's not the best way to limit your input (running a loop of all possible values!).

    Just create a MAX and and MIN like this:

    private final int inputMAX = 10;
    private final int inputMIN = 1;

    then control your input like this:

    int number = Math.Min(Math.Max(num, inputMIN), inputMAX);

    then you can write it directly:

    System.out.println("You've entered " + writeNumber[number-1]);
    Last edited by ozzyman; 12-27-2011 at 06:31 PM.

  6. #6
    ahmedzica is offline Member
    Join Date
    Dec 2011
    Posts
    7
    Rep Power
    0

Similar Threads

  1. Code check
    By JustinP in forum New To Java
    Replies: 4
    Last Post: 09-17-2011, 11:07 PM
  2. can someone check my code plz
    By aizen92 in forum New To Java
    Replies: 20
    Last Post: 02-21-2011, 01:19 AM
  3. Code to check if a piece of code is legal.
    By vahshir in forum New To Java
    Replies: 3
    Last Post: 08-30-2010, 04:21 AM
  4. can anyone check my code?
    By Harmesh Goyal in forum New To Java
    Replies: 8
    Last Post: 06-29-2010, 05:43 AM
  5. Plz Some one check my code
    By TamTam in forum AWT / Swing
    Replies: 1
    Last Post: 02-07-2009, 11:24 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
  •