•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 455,985 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,760 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 652 | Replies: 4
![]() |
•
•
Join Date: Nov 2007
Posts: 9
Reputation:
Rep Power: 0
Solved Threads: 0
Im having arrayoutofbounds error on my code.
what am i doing wrong here? The output should be if i input 35, should be MU.
import java.io.*;
class s{
public static void main(String args[]) throws IOException{
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
String[] a = {"X","C","O","M","P","U","T","E","R","S"};
System.out.println("Enter Price: ");
String b = in.readLine();
for(int i=0;i<b.length();i++){
System.out.print(a[b.charAt(i)]);
}
}
}what am i doing wrong here? The output should be if i input 35, should be MU.
•
•
Join Date: Nov 2004
Location: Netherlands
Posts: 5,752
Reputation:
Rep Power: 19
Solved Threads: 200
In other words, read the API docs for Integer and its parse methods, and see if there is something there that can help you.
And find out what the difference between a char and an int is, as you can use a char as an int (as you've done here), but its value won't be what you seem to be expecting (find an ASCII character code table).
And find out what the difference between a char and an int is, as you can use a char as an int (as you've done here), but its value won't be what you seem to be expecting (find an ASCII character code table).
Java Programmer and Sun Systems Administrator
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
•
•
Join Date: Nov 2007
Location: Belgium
Posts: 62
Reputation:
Rep Power: 1
Solved Threads: 6
You just cast it. In Java a char is actually just like an int, it only depends on how you use it.
Black Box
java Syntax (Toggle Plain Text)
// Declaration of char c, initialized with the character 'a'. char c = 'a'; // For int value of the character, cast to int: (int). int i = (int) c; // Do some work. i++; // Get the character back from the int by casting to char: (char). c = (char) i; // Now you see why 'b' comes after 'a' in the ascii table. System.out.println(c);
Black Box
![]() |
•
•
•
•
•
•
•
•
DaniWeb Java Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- stream tokenizer (Java)
Other Threads in the Java Forum
- Previous Thread: Ball Line Game
- Next Thread: http connection in j2me



Linear Mode