User Name Password Register
DaniWeb IT Discussion Community
All
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
Reply
Join Date: Nov 2007
Posts: 9
Reputation: pickachu is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
pickachu pickachu is offline Offline
Newbie Poster

ArrayOutofBounds

  #1  
Dec 3rd, 2007
Im having arrayoutofbounds error on my code.

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.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Nov 2004
Location: Netherlands
Posts: 5,752
Reputation: jwenting is a jewel in the rough jwenting is a jewel in the rough jwenting is a jewel in the rough jwenting is a jewel in the rough 
Rep Power: 19
Solved Threads: 200
Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: ArrayOutofBounds

  #2  
Dec 3rd, 2007
and why should that be so? charAt gives the character value at the index indicated.
'3' does not yield 3 though.
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote  
Join Date: Feb 2006
Posts: 1,508
Reputation: masijade is a glorious beacon of light masijade is a glorious beacon of light masijade is a glorious beacon of light masijade is a glorious beacon of light masijade is a glorious beacon of light 
Rep Power: 10
Solved Threads: 136
masijade's Avatar
masijade masijade is offline Offline
Posting Virtuoso

Re: ArrayOutofBounds

  #3  
Dec 3rd, 2007
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).
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
Reply With Quote  
Join Date: Nov 2007
Posts: 9
Reputation: pickachu is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
pickachu pickachu is offline Offline
Newbie Poster

Re: ArrayOutofBounds

  #4  
Dec 4th, 2007
Thank, I needed to convert it into integer. Anyway, I encountered a new problem, How do I get the value of a special character like a PERIOD?
Reply With Quote  
Join Date: Nov 2007
Location: Belgium
Posts: 62
Reputation: Black Box is on a distinguished road 
Rep Power: 1
Solved Threads: 6
Black Box Black Box is offline Offline
Junior Poster in Training

Re: ArrayOutofBounds

  #5  
Dec 4th, 2007
You just cast it. In Java a char is actually just like an int, it only depends on how you use it.

  1. // Declaration of char c, initialized with the character 'a'.
  2. char c = 'a';
  3. // For int value of the character, cast to int: (int).
  4. int i = (int) c;
  5. // Do some work.
  6. i++;
  7. // Get the character back from the int by casting to char: (char).
  8. c = (char) i;
  9. // Now you see why 'b' comes after 'a' in the ascii table.
  10. System.out.println(c);

Black Box
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Java Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the Java Forum

All times are GMT -4. The time now is 9:25 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC