2

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!

pboulet
  • 53
  • 1
  • 7

2 Answers2

2

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
}
moveaway00
  • 918
  • 1
  • 5
  • 13
  • When I enter this part of code I get this kind of error 4 times: And I would want to know how can the user input his word in a single input also, not 4 separate input, thanks! 4 errors found: File: G:\uOttawa\02Winter2014\ITI1120\Assignments\A1\a1_6583832.java [line: 183] Error: unreported exception java.io.IOException; must be caught or declared to be thrown – pboulet Jan 27 '14 at 01:18
  • So when the user enters his 4 letter word in a single input, how do I store it to then read it with your solution? Thanks! – pboulet Jan 27 '14 at 01:20
  • If you're reading from input, you'll need to wrap in a try/catch block, because there's a possibility of an error with the IO. (welcome to checked exceptions of java). I've edited my answer to include this. read doesn't prompt 4 times, it will just read 4 characters, from a single input. – moveaway00 Jan 27 '14 at 05:29
0

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);
sebastian s.
  • 160
  • 4
  • Would be a great answer if I could use String variables but I can't. It's mentionned in the description of the problem. Thank you for your answer anyway! :) – pboulet Jan 27 '14 at 00:55