Results 1 to 6 of 6
Thread: Check out my code
- 12-26-2011, 05:09 PM #1
Member
- Join Date
- Dec 2011
- Posts
- 7
- Rep Power
- 0
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
- 12-26-2011, 05:11 PM #2
Member
- Join Date
- Dec 2011
- Posts
- 7
- Rep Power
- 0
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"
- 12-26-2011, 06:57 PM #3
Re: Check out my code
How is the input being done by the user?how to separate the input I'm getting from 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.
- 12-27-2011, 12:38 PM #4
Member
- Join Date
- Dec 2011
- Posts
- 7
- Rep Power
- 0
-
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.
- 01-04-2012, 02:07 PM #6
Member
- Join Date
- Dec 2011
- Posts
- 7
- Rep Power
- 0
Similar Threads
-
Code check
By JustinP in forum New To JavaReplies: 4Last Post: 09-17-2011, 11:07 PM -
can someone check my code plz
By aizen92 in forum New To JavaReplies: 20Last Post: 02-21-2011, 01:19 AM -
Code to check if a piece of code is legal.
By vahshir in forum New To JavaReplies: 3Last Post: 08-30-2010, 04:21 AM -
can anyone check my code?
By Harmesh Goyal in forum New To JavaReplies: 8Last Post: 06-29-2010, 05:43 AM -
Plz Some one check my code
By TamTam in forum AWT / SwingReplies: 1Last Post: 02-07-2009, 11:24 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks