Can you input a string of text from the console window into a char array?
Or do you have to save it in a string, and then do a "s1.getChars( 0, 5, charArray, 0 );"

Also,
I noticed that you can't initiate a char array using the same syntax
as C++ (char myArray[] = "Hello"; ) but you have to do "char[] myArray = {'H','e','l','l','o'};"
Somewhat annoying, is there another way?

I'm using scanner to get input.

Also,
does char fields in Java not put the \0 in the end?
In C++ you couldn't create char field[3] = "Hey";
Because it needs 4 spots - H, e, Y, \0
but in Java I did "char[] falt = {'h','e','j'};" and it worked fine.

s1.toCharArray() is simpler.
In Java a String is an object with many useful methods, and a char is a 16 bit numeric primitive which can be used to hold a single unicode value. A char array is just an array of 16 bit numbers, and it's whatever size you declare it to be. It can have one or more zero elements anywhere in it. It is NOT equivalent to a String.
A String object has a length attribute, so there's no concept of a terminator char.
Unless you want to do some special one-char-at-a-time processing of String data you will probably never use char arrays much. Just use Strings.

But does char arrays have terminating \0?
It doesn't seem like it because I can do char[] array = {'h','e','y'}; without any \0

But does char arrays have terminating \0?
It doesn't seem like it because I can do char[] array = {'h','e','y'}; without any \0

array is a class in java.

In C++ \0 will be stored automatically to detect end of character while reading.

But in java no need of that, based on your no. of elements, size will be stored automatically in field 'length'.

But does char arrays have terminating \0?
It doesn't seem like it because I can do char[] array = {'h','e','y'}; without any \0

Once again - a char array is just an array of chars. It's not a string. Exactly like every other kind of array it doesn't have the concept of a terminator. It has a length, which is fixed when the array is created. Any element or elements in a char array can have the value 0.

You may also want to look at the API doc for String, which makes a distinction between a char and a Unicode code point (viz: character) because some Unicode characters cannot be represented in 16 bits and thus require two chars to represent one character. The String class handles this stuff, but a humble char remains a 16 bit number.

Hm...

import java.io.IOException;
import java.util.Scanner;

public class mainX
{
	
	public static void main(String[] argv) throws IOException
	{
		
		Scanner scn = new Scanner(System.in);
		
		System.out.println("Derp, input char, herp: ");
		
		char[] b = new char[3];

		System.out.print("B0: ");
		b[0] = (char)System.in.read();

                System.out.print("B1: ");
		b[1] = (char)System.in.read();

                System.out.print("B2: ");
		b[2] = (char)System.in.read();
		
		
		System.out.println("Test nextLine: ");
		String test1 = scn.nextLine();
		
		System.out.println("Test nextLine 2: ");
		String test = scn.nextLine();
		
		System.out.println();
	}
}

If I were to input 'a', it would skip input to b[1] and b[2] and then take the 2 strings.
Here is what I got:

Derp, input char, herp:
B0: a
B1: B2: Test nextLine:
hello
Test nextLine 2:
there
B0 = a, B1 =
, B2 =

It seems that B1 is \n, but what the hell is B2?

To get a character and then other inputs, I need to
1. Get the character
2. Use System.in.read() (To get rid of \n or something?)
3. Use System.in.read() again (No idea why)

My thinking was...
if you input 'a' it gets saved in the char, then a \n is left in the buffer, right?
So 1 System.in.read() should be enough?

But I need 2.

Why?
Also, am I correct in thinking a \n gets saved in the buffer?
So next time you try to get input, it takes the \n ?

char a = (char)System.in.read();
String b = scn.nextLine() <--- it would jump over this input

I changed the last line to:
System.out.println("B0 = "+(int)b[0]+", B1 = "+(int)b[1]+", B2 = "+(int)b[2]);

The output I got is:
B0 = 97, B1 = 13, B2 = 10

B0 = a
B1 = Carriage return
B2 = NL line feed, new line

What does B1 and b2 really mean?

carriage return and line feed respectively. Exactly what you see at the end of a line depends on the operating system. Stick with Strings and just read the whole line. Let Java worry about terminators. If you must refer to the end-of-line delimiter use "\n" which will be interpreted according to the OS you're running on.
One last time: this is Java, stop worrying about char arrays.

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.