8 Replies - 1197 Views - Last Post: 22 October 2009 - 03:38 PM Rate Topic: -----

#1 GiveMeYourBagel  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 68
  • Joined: 24-September 09

Simple Strings Program

Post icon  Posted 22 October 2009 - 12:53 PM

I'm trying to make a simple strings program where user inputs a string and the program outputs the string backwards. So i got it to get the number of chars in the string, but charAt() doesn't take anything but numbers even if the variable is an int.

Program:
import java.util.Scanner;

public class MainReverse {

	public static void main(String[] args) {
		

		Scanner keyboard = new Scanner(System.in);
		
		System.out.println ("Please enter a string");
		String word = keyboard.nextLine();
		
		int length = word.length();
		charAt(length);
		System.out.print (length);
		
		
		

	}
}


The system out print at the end is just to see if its getting the right length, im not done the program yet just a little stuck

This post has been edited by GiveMeYourBagel: 22 October 2009 - 12:54 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Simple Strings Program

#2 Matey  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 03-October 09

Re: Simple Strings Program

Posted 22 October 2009 - 02:37 PM

It is not working because for exemple String keyboard="Hello", keyboard.length will be 5 !

In java arrays starts from 0, so :

keyboard[0]="H";
keyboard[1]="e";
keyboard[2]="l";
keyboard[3]="l";
keyboard[4]="o";

There is no keyboard[5], where in this case keyboard.length=5 !!!

Try with charAt(length-1), to get the last character of the word !
Was This Post Helpful? 0
  • +
  • -

#3 GiveMeYourBagel  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 68
  • Joined: 24-September 09

Re: Simple Strings Program

Posted 22 October 2009 - 02:44 PM

it still gives the same error
Was This Post Helpful? 0
  • +
  • -

#4 GiveMeYourBagel  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 68
  • Joined: 24-September 09

Re: Simple Strings Program

Posted 22 October 2009 - 02:51 PM

Am i suppose to use substrings to get the characters and put them out backwards?
Was This Post Helpful? 0
  • +
  • -

#5 painkiller102  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 16
  • View blog
  • Posts: 281
  • Joined: 27-February 08

Re: Simple Strings Program

Posted 22 October 2009 - 02:51 PM

View PostMatey, on 22 Oct, 2009 - 01:37 PM, said:

It is not working because for exemple String keyboard="Hello", keyboard.length will be 5 !

In java arrays starts from 0, so :

keyboard[0]="H";
keyboard[1]="e";
keyboard[2]="l";
keyboard[3]="l";
keyboard[4]="o";

There is no keyboard[5], where in this case keyboard.length=5 !!!

Try with charAt(length-1), to get the last character of the word !


True true, and for code length sake, you can use a for loop:

String str = "hello";
String backwards ="";
char[] content = str.toCharArray();
for(int i = content.length - 1; i >= 0; i--)
{
		   backwards = backwards +Character.toString(content[i]);
}
System.out.println(backwards);




This will reverse a string and print out the new backwards string ^_~! AND its nice short and compact code. Of course if you do not understand fully how this works, well then i wouldn't bother using it, but it will be good to know in the future!
Was This Post Helpful? 0
  • +
  • -

#6 GiveMeYourBagel  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 68
  • Joined: 24-September 09

Re: Simple Strings Program

Posted 22 October 2009 - 02:59 PM

The thing is, im not suppose to use the backwards command. im only allowed to use charAt, indexOf, length, and substring
Was This Post Helpful? 0
  • +
  • -

#7 painkiller102  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 16
  • View blog
  • Posts: 281
  • Joined: 27-February 08

Re: Simple Strings Program

Posted 22 October 2009 - 03:03 PM

View PostGiveMeYourBagel, on 22 Oct, 2009 - 01:59 PM, said:

The thing is, im not suppose to use the backwards command. im only allowed to use charAt, indexOf, length, and substring


hmmm. that does complicate things a bit doesn't it! Loops are the best way to reverse a string... doing it without one is pretty strange (and in my opinion pointless).

This post has been edited by painkiller102: 22 October 2009 - 03:05 PM

Was This Post Helpful? 0
  • +
  • -

#8 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10783
  • View blog
  • Posts: 40,158
  • Joined: 27-December 08

Re: Simple Strings Program

Posted 22 October 2009 - 03:24 PM

Looking at the code from the original post, I see a few things to correct. First, the charAt(index) method is associated with String objects, not your MainReverse class. When you call it by saying charAt(length), the compiler checks your class for the method. Since it doesn't exist an error will be thrown. Next, remember that Strings and arrays are indexed the same way; they go from 0 to length-1. So when you try to reference the character at the length, you will get an index out of bounds exception. Lastly, you need a for loop to print the String in reverse, like so:

for(int i = word.length()-1; i > -1; i--)
  System.out.print(word.charAt(i));



This will print the String in reverse order on the same line. So if your String is "123" then your program will print "321."
Was This Post Helpful? 1
  • +
  • -

#9 GiveMeYourBagel  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 68
  • Joined: 24-September 09

Re: Simple Strings Program

Posted 22 October 2009 - 03:38 PM

Thanks that was really helpful ;D
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1