First Ever Java Program Incorrect Output - java

Sorry for really stupid question, I'm learning a new language and taking this code:
public class Exercise01 {
int i;
char c;
public static void main(String[] args) {
Exercise01 E = new Exercise01();
System.out.println("i = " + E.i);
System.out.println("c = [" + E.c + "]");
}
}
/* Output:
i = 0
c = [
*/
Why the output does not produce "]" character? Has it something to do with Unicode?
PostEdited: the variable E.c was not initialized for experimentation purpose.

It may be that the place your program is outputting to, a console or a window, is getting confused by the U+0000 character which is the value of E.c.
It works fine for me.
Initialize E.c and try again.

You are trying to print the null character as your char c hasn't need initialised. i.e. \0 Interestingly you can't copy and paste this character easily as most C code sees this as an end of string marker.
I see the ] when I run the code.
Try changing your code with
char c = '?';
gives me an output of
i = 0
c = [?]
One way to reproduce this problem is to run on unix
java Main | more
which outputs
i = 0
c = [

Probably has to do with the fact that E.c isn't initialized to anything

I think it is because c is not initialized and therefore holds \0, i.e. "end of line". So, println prints until end of line and does not print your ]

You should initialize your char C as well as the int i. Good code practice: It is important to initialize your variable once you declare a variable!

Related

How to allow Java to take in /n from scanner and print ASCII value

I'm doing a java project on codeZinger where I need to take in a character value from the scanner and print the ASCII value. So far the code I have works for everything besides the "/n" character. In that case, codezinger returns the error "Exception in thread "main" java.lang.NullPointerException scanner".
I have attached my code below, I've tried everything and it won't work. I'm new to java from c++.
I tried even manually testing for /n using an if statement and that didn't work
public class Solution {
public static void main(String[] args) {
//creating input stream
Scanner input = new Scanner(System.in);
// allows character input from input stream
char charIn = input.findInLine(".").charAt(0);
if(input.equals("\\n"))
{
System.out.print("10");
}
// casts character to type int to get ascii value
int intVal = (int)charIn;
System.out.print(intVal);
}
}
input.equals() method in java never takes its parameter in apostrophe.
Please go through this once :
https://www.jquery-az.com/learn-java-equals-method-5-examples/
Moreover /n doesn't exist in java. If you have to print a line(Give a break) then you have to use System.out.println() , simply, and it will be printed in next line.
Also go through this for printing ASCII value
https://www.javatpoint.com/how-to-print-ascii-value-in-java
int code;
while ((code = System.in.read()) != -1) {
System.out.format("0x%02X ", code);
}

How can I fix my code to find a certain character in an array and make changes to that array

while (scan_file.hasNext()) {
String b = scan_file.nextLine();
// checks if string b contains the tag <h>
if (b.contains("<h>")) {
char arrayString[] = b.toCharArray();
for (int i = 0; i < arrayString.length; i++) {
if (arrayString[i] == '<') {
arrayString[i] = arrayString[i + 2];
}
System.out.print(arrayString[i]);
}
}
}
What I was expecting the program to do was(for now) iterate through the while loop and store each line as string 'b'.
I want to check if that string b contains a certain string like <h> for this example. And I want to convert string b into an array if it contains said string like <h> and iterate through that array to check for '<' and move the array up 2 spaces.
For example, string b had <h>hello, I wanted to eventually print hello because the program would have moved up 2 elements.
I feel like I got the loops and general idea on how I want to tackle the problem.. but when I ran the program, nothing printed so I don't know if I did the loops and if statements correctly.
I really don't know how to word my problem well, so bear with me and I'm sorry in advance.
All feedbacks are greatly appreciated (:
System.out.print(arrayString[i]); just print the ith character of arrayString, it's definitely not what you want.
In fact you don't have to convert a String to char[], String has many utils method can help you with your goal.
I won't give you full code , but I can give you some tips.
You can use String.indexof('<') to find the index of '<'.
You can use String.subString(startIndex) to get the subString start with the specified index.
Suppose your code scan_file.hasNext() and scan_file.nextLine() is work well. You can try code below to remove all from current line:
if (b != null && b.contains("<h>")) {
System.out.println(b.replaceAll("<h>", ""));
}

Java: Will if-statments not work if I have two or more conditionals in the statement?

There is probably an obvious answer. This question might have already been asked but I don't know how to word the question. I'm working in Java and in this moment, I am reading the input text from the command line and converting that stuff over to strings.
I am, for sure, inputting the x character into the command line and whether I set the code to
!(first.equals("x")) or (first.equals("x"))
I still get the system.out output text. I noticed that if I remove the || and the following equals snippet it works as intended and continues onto the code. However, I have to have either x or y be options for the first arg string. Can someone please let me know what I am doing wrong. Thanks.
Here is the snippet of code:
private something(String[] args)
{
first = args[0];
second = args[1];
third = args[2];
if (!(first.equals("x")) || !(first.equals("y")))
{
System.out.println("First is the problem " + first);
}
}
Here is the ouput:
First is the problem x
Edit: I also did this and got the same result:
if (first.equals("x") == false || first.equals("y") == false)
{
System.out.println("First is the problem " + first);
}
I'm using this if-statement as a check for whether or not the two values are inputted. If they aren't then the if-statement should trigger.
It works when I have it like this, but I end up losing the y:
if (first.equals("x") == false)
{
System.out.println("First is the problem " + first);
}
first is either NOT "x" or NOT "y". This will always be true for whatever string first is set.
=> Are you not republican or are you not democrat? ;)
This code appears to work. The if statement returns as true if args[0] equals either x or y. !(first.equals("x")) means that it is true for every string except "x", so this would do the opposite of what you want. Also, you need to define first, second, and third otherwise it won't compile.
Edit:
Looking at your edit, it appears the issue is caused by ||. Since this means 'or', first only has to not equal either x or y for the if statement to run. Therefore, since it can't equal both at once, the if statement will always run. You should use &&(and) instead. Also, notice that == false can be replaced with !.
For example:
if (!(first.equals("x")) && !(first.equals("y")))
{
System.out.println("First is the problem " + first);
}

Can any one Explain this behaviour in char array

Please find the code snippet.
public static void main(String args[]) {
char[] tmpArray = new char[10];
tmpArray[0] = 'a';
tmpArray[1] = 'b';
tmpArray[3] = 'd';
for (char element : tmpArray) {
System.out.print(element);
}
System.out.println(String.valueOf(tmpArray));
}
I was excepting the abd would display . but it is not displaying anything in forloop and it is displaying just "ab" in sop .
while i try to debug, array still holds the 4th element as 'd'. if if give d in 3rd element it is displaying properly. is it in contract per spec ?. It basically creates new String instance using Arrays.copy of the char. even the new char[] (value in String instance contain the 'd' as 4th element) . Is this behavior due to iterating in array while displaying...
there is no real use case. i just tried to solve other SO question ( irrelevant to this) and got this situation.
Thanks in advance.
Update:
Comments describing, they are seeing the 'd' at the end. that is what i excepted. but i didnt showed in my machine.
Ubuntu 12, Jdk 1.8 , eclipse kepler. is it releated to eclipse console then ?
You are assuming the array will default to a space (' ') character if you do not explicitly set it. Instead it defaults to null. See
Can we assume default array values in Java? for example, assume that an int array is set to all zeros?

Missing identifier even though it's defined one line above?

Let's see this trivial Java program:
class A {
public static void main(String[] args) {
int а = 2;
System.out.println("a " + a);
}
}
It creates the variable a initialized to 2, and prints it. However, it doesn't compile:
$ javac A.java && java A
A.java:4: cannot find symbol
symbol : variable a
location: class A
System.out.println("a " + a);
^
1 error
Why?
You've got two different types of a there. If you retype the first one as ASCII a, it's fine.
The first a is U+0430, "Cyrrilic small letter a". (See the relevant Unicode chart.)
I would personally try to keep all identifiers in Java as ASCII characters where possible.
(Where did this code even come from to start with?)
I don't know what is that, but on the int а = 2; line, that thing most certainly is not an a, a.k.a ASCII 97.

Categories

Resources