- Java string input and output string [duplicate]
- Java string input and output string [duplicate]
- Problems with Input and output regarding Java String
- Modifying Output in String Input By Text File (Java)
- How to test output/input in java
- Java Basic Input and Output
- Difference between println(), print() and printf()
- Example: print() and println()
- Example: Printing Variables and Literals
- Example: Print Concatenated Strings
- Example: Get Integer Input From the User
- Example: Get float, double and String Input
- Table of Contents
- Java IO : Input-output in Java with Examples
Java string input and output string [duplicate]
Solution 1: The most important part writing unit tests is separating the code you want to test from boilerplate/well-tested code out of given frameworks — so you might create classes which create the output but separate the part of reading the input from and writing it to from your business logic to concentrate on the «serious» parts. The hashcode value indicates that, s is an array and it’s has String values.
Java string input and output string [duplicate]
You are printing the array object itself, not the content of the array. The hashcode value [Ljava.lang.String;@65d4ab0e indicates that, s is an array and it’s has String values. The leading [ says, the object you have printed is an array.
Take a look at http://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html#toString(java.lang.Object[])
Strings in Java, This class is used to allow character buffers to be used in place of CharSequences. An example of such usage is the regular-expression package java.util.regex. String: String is a sequence of characters. In java, objects of String are immutable which means a constant and cannot be changed once …
Problems with Input and output regarding Java String
As you wrote ja without quotes, it expects to find a variable defined with that name, like
String ja = "ja"; if (Spielstart.equalsIgnoreCase(ja))
But what you want is just
if (Spielstart.equalsIgnoreCase("ja")) < System.out.println("Gut"); >else < System.exit(0); >// end of if-else
Java — How to use JFrame to display strings and read, 1) Swing GUIs should be created and manipulated on the EDT. 2) A combination rich client / command prompt app. typically raises more problems than it solves. 3) I got the impression the OP wanted to both accept input and show output on the frame itself. – Andrew Thompson Sep 17, 2015 at 4:20
Modifying Output in String Input By Text File (Java)
Here’s my recommendation. Don’t use streams for something so trivial and non-load intensive. Stick to the basics, use a Scanner and read your file line-by-line.
Here’s the method to success!
- Learn how to use a Scanner to read Strings from a text file line-by-line.
- Make sure you split the Strings apart with the str.split() method accordingly.
- Store each line’s String value into a array/list/table.
- Modify your stored Strings to remove the last two letters. Look into the str.subString(s,f) method.
- Learn how to use a PrintWriter to output your modified Strings to a file.
Comment Reply
Read in a line as a String from texfile.
File file = new File("fileName.txt"); Scanner input = new Scanner(file); while (input.hasNextLine()) < String line = input.nextLine(); //Go on to nextLine >
Java Program to Print a String, The String class is immutable, so that once it is created a String object cannot be changed. If there is a necessity to make a lot of modifications to Strings of characters, then use String Buffer & String Builder Classes. Input. Suppose our input is −. Hello my name is John! Output. The desired output …
How to test output/input in java
The most important part writing unit tests is separating the code you want to test from boilerplate/well-tested code out of given frameworks — so you might create classes which create the output but separate the part of reading the input from System.in and writing it to System.out from your business logic to concentrate on the «serious» parts.
I came against this problem when refactoring some legacy code recently. I extracted input/output into new classes and made them dependency of my program. Using dependency injection I inserted mocks/fakes so I could define what will be the input and what the output should be.
public class InputReader < private static final Scanner SCANNER = new Scanner(System.in); public String nextLine() < return SCANNER.nextLine(); >> public class OutputPrinter < public void println(String string) < System.out.println(string); >>
And then the test using mockito:
public class MainProgramTest < private final InputReader inputReader = mock(InputReader.class); private final OutputPrinter outputPrinter = mock(OutputPrinter.class); @Test public void playsSimpleGame() < when(inputReader.nextLine()) .thenReturn("hello") .thenReturn("help") .thenReturn("exit"); MainProgram mainProgram = new MainProgram(inputReader, outputPrinter); mainProgram.run(); InOrder inOrder = inOrder(inputReader, outputPrinter); inOrder.verify(inputReader).nextLine(); // ->"hello" inOrder.verify(outputPrinter).println("\"hello\" is not recognised command, type \"help\" for known commands"); inOrder.verify(inputReader).nextLine(); // -> "help" inOrder.verify(outputPrinter).println("This is help, the only known commands are \"help\" and \"exit\""); inOrder.verify(inputReader).nextLine(); // -> "exit" inOrder.verify(outputPrinter).println("Goodbye"); verifyNoMoreInteractions(inputReader, outputPrinter); > >
How to get an input word in the output? Beginning Java, System.out.println («The time between » + departure + » and » + arrival + » is:»); To Java, your departure and arrival variables are already strings, so it will just insert them between your pre-typed strings. Don’t forget to have spaces included in the strings (after «between», on either side of «and», and before «is». …
Java Basic Input and Output
Don’t worry if you don’t understand it. We will discuss class , public , and static in later chapters.
Let’s take an example to output a line.
Java programming is interesting.
Here, we have used the println() method to display the string.
Difference between println(), print() and printf()
- print() — It prints string inside the quotes.
- println() — It prints string inside the quotes similar like print() method. Then the cursor moves to the beginning of the next line.
- printf() — It provides string formatting (similar to printf in C/C++ programming).
Example: print() and println()
1. println 2. println 1. print 2. print
In the above example, we have shown the working of the print() and println() methods. To learn about the printf() method, visit Java printf().
Example: Printing Variables and Literals
When you run the program, the output will be:
Here, you can see that we have not used the quotation marks. It is because to display integers, variables and so on, we don’t use quotation marks.
Example: Print Concatenated Strings
Here, we have used the + operator to concatenate (join) the two strings: «I am » and «awesome.» .
System.out.println("Number Number input">Java Input Java provides different ways to get input from the user. However, in this tutorial, you will learn to get input from user using the object of Scanner
class.
In order to use the object of Scanner
, we need to import java.util.Scanner
package.
import java.util.Scanner;
To learn more about importing packages in Java, visit Java Import Packages.
Then, we need to create an object of the Scanner class. We can use the object to take input from the user.
// create an object of Scanner Scanner input = new Scanner(System.in); // take input from the user int number = input.nextInt();
Example: Get Integer Input From the User
import java.util.Scanner; class Input < public static void main(String[] args) < Scanner input = new Scanner(System.in); System.out.print("Enter an integer: "); int number = input.nextInt(); System.out.println("You entered " + number); // closing the scanner object input.close(); >>
Enter an integer: 23 You entered 23
In the above example, we have created an object named input of the Scanner class. We then call the nextInt() method of the Scanner class to get an integer input from the user.
Similarly, we can use nextLong() , nextFloat() , nextDouble() , and next() methods to get long , float , double , and string input respectively from the user.
Note: We have used the close() method to close the object. It is recommended to close the scanner object once the input is taken.
Example: Get float, double and String Input
Table of Contents
Java IO : Input-output in Java with Examples
Java brings various Streams with its I/O package that helps the user to perform all the input-output operations. These streams support all the types of objects, data-types, characters, files etc to fully execute the I/O operations.

Before exploring various input and output streams lets look at 3 standard or default streams that Java has to provide which are also most common in use:

- System.in: This is the standard input stream that is used to read characters from the keyboard or any other standard input device.
- System.out: This is the standard output stream that is used to produce the result of a program on an output device like the computer screen. Here is a list of the various print functions that we use to output statements:
- print(): This method in Java is used to display a text on the console. This text is passed as the parameter to this method in the form of String. This method prints the text on the console and the cursor remains at the end of the text at the console. The next printing takes place from just here.
Syntax:
System.out.println(parameter);
Printing simple integer: x = 100 Formatted with precision: PI = 3.14 Formatted to specific width: n = 5.2000 Formatted to right margin: n = 2324435.2500
Enter characters, and '0' to quit. G e e k s f o r G e e k s 0
- Depending on the type of operations, streams can be divided into two primary classes:
- Input Stream: These streams are used to read data that must be taken as an input from a source array or file or any peripheral device. For eg., FileInputStream, BufferedInputStream, ByteArrayInputStream etc.

- Output Stream: These streams are used to write data as outputs into an array or file or any output peripheral device. For eg., FileOutputStream, BufferedOutputStream, ByteArrayOutputStream etc.

- Depending on the types of file, Streams can be divided into two primary classes which can be further divided into other classes as can be seen through the diagram below followed by the explanations.
- ByteStream: This is used to process data byte by byte (8 bits). Though it has many classes, the FileInputStream and the FileOutputStream are the most popular ones. The FileInputStream is used to read from the source and FileOutputStream is used to write to the destination. Here is the list of various ByteStream Classes:
Stream class Description BufferedInputStream It is used for Buffered Input Stream. DataInputStream It contains method for reading java standard datatypes. FileInputStream This is used to reads from a file InputStream This is an abstract class that describes stream input. PrintStream This contains the most used print() and println() method BufferedOutputStream This is used for Buffered Output Stream. DataOutputStream This contains method for writing java standard data types. FileOutputStream This is used to write to a file. OutputStream This is an abstract class that describe stream output.
Example: