import java.util.Scanner;
	class ReverseWord {
	public static void main(String args[]) {
	Scanner myScanner = new Scanner(System.in);
	char c1, c2, c3, c4;
		c1 = myScanner.findInLine(“.”).charAt(0);
		c2 = myScanner.findInLine(“.”).charAt(0);
		c3 = myScanner.findInLine(“.”).charAt(0);
		c4 = myScanner.findInLine(“.”).charAt(0);

	System.out.print(c4);
	System.out.print(c3);
	System.out.print(c2);
	System.out.print(c1);
	System.out.println();
	}
}

the code above if you type
stop
in the windows prompt window will put the output of
pots
but im wondering what this part does
charAt(0);

at what would happen if i changed it to
charAt(1);

thanks, i know what the rest does though.

Dani AI

Generated

Quick summary: String.charAt(int index) returns the character at the given zero-based position in a Java String. For the input "stop" the positions are charAt(0) -> 's', charAt(1) -> 't', charAt(2) -> 'o', charAt(3) -> 'p'.

In the original snippet Scanner.findInLine(".") is using the argument as a regular expression. The pattern . matches a single character, so findInLine(".") returns a one-character String. Calling charAt(0) extracts that character; calling charAt(1) on that same returned string would throw a StringIndexOutOfBoundsException because the string length is 1. Also note that findInLine(...) can return null when no match is found, so calling .charAt(...) without a null check can cause a NullPointerException.

A few practical tips (ties back to earlier replies): correctly pointed out the compile problem caused by typographic quotes — use straight double quotes (") in source. If a literal period is the intent, escape the regex with \\.; otherwise . will match any character. Experimentation as suggested is the fastest way to see these effects.

A safer approach is to read a whole line or token, check its length, then index into it. Example:

Scanner s = new Scanner(System.in);
String line = s.nextLine();
if (line.length() >= 4) {
    System.out.print(line.charAt(3));
    System.out.print(line.charAt(2));
    System.out.print(line.charAt(1));
    System.out.print(line.charAt(0));
    System.out.println();
} else {
    System.out.println("Input too short");
}

This avoids regex surprises and prevents index/NULL errors.

Recommended Answers

All 5 Replies

Why don't you try it and find out?

I'm sorry, but you claim to be jumping into Java quickly, on your own, because you already know a couple of other languages. Well, I need to ask, what "other languages" do you know? Because in that thread you had no idea what "static" was and in this thead, the method you are looking at is extremely self-explanatory if you have nearly any real programming experience at all.

commented: This is so crazy it just might work!! :P +11

Why don't you try it and find out?

I'm sorry, but you claim to be jumping into Java quickly, on your own, because you already know a couple of other languages. Well, I need to ask, what "other languages" do you know? Because in that thread you had no idea what "static" was and in this thead, the method you are looking at is extremely self-explanatory if you have nearly any real programming experience at all.

nothing that related to object oriented, batch, javascript, html, and almost finished php. did a bit in binary but stopped, was a bit of a waste of time...

i got the code off a pdf file whats helping me learn, when i tested it i found 20 errors, witch is probably becasue the books pretty old. and i didint have the time today inbetween reading and working with my brother on his truck so i didint get the time to look into the problems.

plus even if i did test it i may not get the answer im looking for.

Man where is Salem when you need him...

Anyways, yeah Masijade is correct. You should really experiment and try things on your own until you come across an error. Then ask for help =P

First, the API docs, every Programmers best friend.
http://java.sun.com/javase/6/docs/

Second, the official Tutorials, every beginning Programmers second best friend.
http://java.sun.com/docs/books/tutorial/index.html

Third Bruce Eckels "Thinking In Java" 3rd Edition (the 4th Edition is not completely available online, yet).

this is the mistake:
in

findInLine(“.”)

you must have

"."

instead of

“.”
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.