The problem with this line:

String output = h + three + one + ten + " " + "w" + zero + "r" + won + "d " + two + " " + t;

is that operations are performed left to right, so it first sums h + three (which evaluates to an int) and then one and then ten. Up to that point you have a numerical value (an int) that then will be "summed" to a String. Try something like this:

String output = "" + h + three + one + ten + " " + "w" + zero + "r" + won + "d " + two + " " + t;

In this second case your expression will start with a String object, evaluating the rest of the operations as Strings.

You of course could use "" at the beginning or any other value that evaluates to String, like String.valueOf(h). In this last case you wouldn't need to use String.valueOf() for the other operands, as the first one is already a String.

Answer from morgano on Stack Overflow
🌐
TutorialsPoint
tutorialspoint.com › printing-integer-between-strings-in-java
Printing Integer between Strings in Java
July 4, 2020 - public class Demo{ public static void main(String[] args){ System.out.println("The equals symbol is present between two integer values "); System.out.println(45+5 + "=" +(56+11)); System.out.println(45+5 + " equals symbol " +(56+11)); } }
Top answer
1 of 7
6

The problem with this line:

String output = h + three + one + ten + " " + "w" + zero + "r" + won + "d " + two + " " + t;

is that operations are performed left to right, so it first sums h + three (which evaluates to an int) and then one and then ten. Up to that point you have a numerical value (an int) that then will be "summed" to a String. Try something like this:

String output = "" + h + three + one + ten + " " + "w" + zero + "r" + won + "d " + two + " " + t;

In this second case your expression will start with a String object, evaluating the rest of the operations as Strings.

You of course could use "" at the beginning or any other value that evaluates to String, like String.valueOf(h). In this last case you wouldn't need to use String.valueOf() for the other operands, as the first one is already a String.

2 of 7
2

You can either convert your numbers into a string using the toString or valueOf methods of the wrapper classes (guess you are not there yet), or just stuff all your primitives into the printline without the String output.

system.out.println(h + three + one + ten + " " + "w" + zero + "r" + won + "d " + two + " " + t);

All you need to look for is that there is a String in the printline statement. Meaning if you only want to print our number based datatype you can use system.out.println("" + youNumberVariable).

There would also be the option to add an empty string at the beginning of your declaration of output output = "" + theRest; to force all following values into the string like it does in the printline statement.

Most of it is not very pretty coding but will completly suffice for the learning process.

Discussions

Java print string and int on same line - Stack Overflow
You should read it. printf() takes ... and read the javadoc. ... Regarding your second question, stackoverflow.com/questions/40480/… would be a good read. ... You need to use the printf method and not println. println is used to print primitive types, Strings and objects as ... More on stackoverflow.com
🌐 stackoverflow.com
java - How do I combine strings and integers in a print statement? - Stack Overflow
I have just started learning Java in a college course. If/then statements haven't been covered yet but I figured I could try them on my own. I ended up with some syntax errors which I think I fixed... More on stackoverflow.com
🌐 stackoverflow.com
Printing 2 variables, same line without them adding together
What language are you using? More on reddit.com
🌐 r/learnprogramming
10
1
September 13, 2020
concatenating string and numbers Java - Stack Overflow
You get the result of the multiplication, an integer, and then the concatenation of a String and an int due to the +. ... You need the parentheses to express the precedence you wish for. ... I know ! But that is not my question . 2012-08-19T18:15:43.697Z+00:00 ... It requires nothing more than actually reading the Java ... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Stack Overflow
stackoverflow.com › questions › 20207771 › how-do-i-combine-strings-and-integers-in-a-print-statement
java - How do I combine strings and integers in a print statement? - Stack Overflow
System.out .println("After how many hours are you paid for overtime?"); OTy = input.nextInt(); if (hours > OTy) System.out.println("Your weekly pay is " + ((40 * payRate) + (hours - OTy) * (payRate * 1.5))); // <-- at the start of this line. else System.out.println("Your weekly pay is " + payRate * hours); } else // <-- That brace to close. System.out.println("Your weekly pay is " + payRate * hours); ... import java.util.Scanner; public class JavaApplication2 { public static void main(String[] args) { Scanner input = new Scanner(System.in); String name; double payRate; int hours; Boolean OTyn; int OTy; System.out.println("What is your name?"); name = input.nextLine(); System.out.println("What is your hourly rate of pay?"); payRate = input.nextDouble(); System.out.println("How any hours have you worked?"); hours = input.nextInt(); System.out.println("Your boss pays overtime.
🌐
Sololearn
sololearn.com › en › Discuss › 185759 › printing-string-and-integer-or-float-in-the-same-line
https://www.sololearn.com/en/Discuss/185759/printi...
Sololearn is the world's largest community of people learning to code. With over 25 programming courses, choose from thousands of topics to learn how to code, brush up your programming knowledge, upskill your technical ability, or stay informed about the latest trends.
🌐
W3Schools
w3schools.com › java › java_variables_print.asp
Java Print/Display Variables
String firstName = "John "; String lastName = "Doe"; String fullName = firstName + lastName; System.out.println(fullName); Try it Yourself » · In Java, the + symbol has two meanings: For text (strings), it joins them together (called ...
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › java › gfact-49-print-integer-between-strings-in-java
Printing Integer between Strings in Java - GeeksforGeeks
December 11, 2018 - Java · public class Test { public static void main(String[] args) { System.out.println(45+5 + "=" +45+5); } } Output: 50=455 · The reason behind this is - Initially the integers are added and we get the L.H.S. as 50. But, as soon as a string is encountered it is appended and we get "50=" .
🌐
Codehubindia
codehubindia.in › learn › java › tutorials › java printing variables
Java Printing Variables
Pass variable names directly to `println()` to print their values. ... The `+` operator performs string concatenation when at least one operand is a string. ... Java automatically converts primitive types (like `int`, `double`, `boolean`) to strings when concatenated with other strings.
🌐
Oracle
docs.oracle.com › javase › tutorial › java › data › numberformat.html
Formatting Numeric Print Output (The Java™ Tutorials > Learning the Java Language > Numbers and Strings)
There are many converters, flags, and specifiers, which are documented in java.util.Formatter ... The %d specifies that the single variable is a decimal integer. The %n is a platform-independent newline character. The output is: ... The printf and format methods are overloaded. Each has a version with the following syntax: public PrintStream format(Locale l, String format, Object...
🌐
Reddit
reddit.com › r/learnprogramming › printing 2 variables, same line without them adding together
r/learnprogramming on Reddit: Printing 2 variables, same line without them adding together
September 13, 2020 -

Using Java, format is on mobile, only using “1. and 2.... so it page breaks)

The output is suppose to be: 1.Chen 2. 5050

Instructions:

  1. create string called Chen, I did this

  2. Create int variable age=50, did this

  3. Create int variable iq=age, did this

  4. Print value of name

  5. Print the value of age without skipping a new line

  6. Print the value of iq

My output is

  1. Chen

  2. 100

It’s adding age and iq making it 100 instead of 5050

My code:

  1. class Main {

  2. public static void main(String[] ages) {

  3. String name=“Chen”;

  4. Int age=50;

  5. Int iq=age;

  6. System.out.println(name);

  7. System.out.println(age+iq);

When I run this it’s giving me:

  1. Chen

  2. 100 (it’s adding age and iq) But when I check how it’s suppose to print out it should be:

  3. Chen

  4. 5050

🌐
TutorialsPoint
tutorialspoint.com › java-program-to-concatenate-a-string-and-integers
Java Program to concatenate a String and Integers
JavaObject Oriented ProgrammingProgramming · To concatenate a String and some integer values, you need to use the + operator. Let's say the following is the string. String str = "Demo Text"; Now, we will concatenate integer values. String res = str + 1 + 2 + 3 + 4 + 5; The following is the ...
🌐
Kansas State University
textbooks.cs.ksu.edu › cc210 › 02-data-types-math › 08-java › 08-printing-text-numbers
Printing Text & Numbers :: CC 210 Textbook
December 29, 2022 - In Java, we can also concatenate multiple outputs together using the + symbol. So, we could produce similar output using this example: int x = 1; int y = 2; System.out.println("Variable x: " + x + " , Variable y: " + y); ... Notice that we needed to include an extra space after 1 in the output.
🌐
JA-VA Code
java-performance.info › home › exploring various methods for adding int to string in java
Adding Int To String Java: Adding Integers Made Easy
September 25, 2023 - Let’s delve deeper into the practical implementation of using the + operator to add integers to strings in Java. Consider a scenario where you’re building a program that asks the user for their age and then displays a message using that ...
🌐
DaniWeb
daniweb.com › programming › software-development › threads › 340568 › printing-more-than-one-int-with-system-out-print
Printing More than one int with System.out.print( )
symbol : method println(java.lang.String,java.lang.String) location: class java.io.PrintStream System.out.println("\n" + var1,"\n" + var2); I understand that \n is new line, and My instructor told me to just use + "Integer name" for my previous ...
🌐
Reddit
reddit.com › r/learnjava › how to use string and int in the same project?
r/learnjava on Reddit: How to use String and int in the same project?
April 19, 2022 -

i'm a Java beginner and just recently picked up my Java book again. so, i'm trying to make a simple program. the code is this:

class MyAwesomeProgram {
    public static void main(String [] args) {
        String test = "bro";
        int test2 = "1234";
            if (test.equals ("bro")){
                System.out.println("hallo!");
            }
            else {
                System.out.println("test successful.your computer will be h");
            }
            System.out.println(test2);
    }
}

when i try to run this with Java, i get the error error: incompatible types: String cannot be converted to int

i'm not trying to convert anything, i'm just trying to use something other than a string! i will provide more info if needed

EDIT: i'm an idiot. in my original post, i tried assigning a LETTER to an int, which can only handle numbers. i know this but i just didn't notice it before i hit post. the same error happens when assigning a number to the int. also happens with any other variable type like boolean or byte.

EDIT 2: solved! i had to remove the quotation marks on 1234

🌐
Quora
quora.com › Does-Java-allow-strings-and-integers-to-be-concatenated
Does Java allow strings and integers to be concatenated? - Quora
Answer (1 of 11): Two people have suggested that it's something to do with what println does or expects but this is wrong. "foo"+10 is an expression in Java whether it is being passed to println or another method or used in an if() statement etc. It is evaluated and the result of evaluation, wha...