Put each character of an word inputted by the user into separate char variables in java

StackOverflow https://stackoverflow.com/questions/21345777

  •  02-10-2022
  •  | 
  •  

Pregunta

I'm a freshman in software engineering and I have a question that I really can't figure out( trust me I've been searching for a while now):

I have to get a 4 letter word input from the user in a single input and then put each character of the word into different char variables in java.

The thing is that the only variable types I can use are boolean, int, double and char because we havn't seen arrays and strings yet so we are not allowed to use them. ( e.g. I can't declare "String variable" and then use that "variable" in any way only the types I mentionned previously are accepted.

I thought about using bitwise operators but we havn't seen them in the course so I would rather use a solution that don't involve them. But if you have a solution involving them it maybe useful too so anything would help!

We can't use if, while, for or any loop concepts either( havn't seen it yet in the course).

Thank you very much for your input guys!!.

NOTE: It's my first question on the site so if there is something that is not specific enough or anything tell me I will take all constructive comments!

¿Fue útil?

Solución

Assuming you're reading from stdin, how about

try {
  char c1 = (char)System.in.read();
  char c2 = (char)System.in.read();
  char c3 = (char)System.in.read();
  char c4 = (char)System.in.read();
} catch(IOException e) {
  // Do nothing
}

Otros consejos

As a freshman in SE i guess we are talking about java. A possible way would be

char c1=string.charAt(0);
char c2=string.charAt(1);
char c3=string.charAt(2);
char c4=string.charAt(3);
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top