Sorry for my unclear questions, I want to input a single line text and print out the length of it, then printout the first word of it then the rest of the text.
Like if input "I am ruby", then the output would be:
9
I
am ruby
How should I make it? I have searched for the questions and got some similar but doesn't help, The following code is what I make so far
import java.util.Scanner;
public class lab {
public static void main(String [] args)
{
Scanner kbd = new Scanner(System.in);
String s = kbd.next();
String s1 = kbd.nextLine();
System.out.println(s);
System.out.println(s1);
}
}
import java.util.Scanner;
public class lab {
public static void main(String [] args)
{
Scanner kbd = new Scanner(System.in);
String s = kbd.next();
String s1 = kbd.nextLine();
System.out.println(s.length());
System.out.println(s);
System.out.println(s1);
}
}
Scanner does not have lenght(), Length() method is only for strings variables
Which IDE are you using (Eclipse, Netbeans, Intellij?) because the IDE usually shows you the error
I don't get what you want.
import java.util.Scanner;
public class lab
{
public static void main(String[] args)
{
Scanner kbd = new Scanner(System.in);
System.out.print("1st string: ");
String s = kbd.nextLine();
System.out.print("2nd string: ");
String s1 = kbd.nextLine();
System.out.println(s.length()); // this is for the length
System.out.println(s1); // this if for the input only
}
}
Still the same.
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner kbd = new Scanner(System.in);
System.out.print("Word:");
String s = kbd.nextLine();
System.out.println("Input:" +s); // print the string
System.out.println("Length:" + s.length()); // the length of the string
}
}
see the picture if its what u want to do.
Related
This question already has answers here:
Scanner doesn't see after space
(7 answers)
Closed 1 year ago.
I am writing a very simple program to input a string with space and then output it, my problem is, it not printed out fully as I expected.
Here is my code, as you can see, very simple
import java.util.Scanner;
public class Testjapanese {
public static void main(String[] args) {
String x;
Scanner keyboard = new Scanner(System.in);
System.out.println(" Add a string");
x = keyboard.next();
System.out.println(x);
}
}
For example, it print "Add a string" but when I input a string "Today is very", it gave me "Today", not "Today is very".
I search and they said to me that I should use input.nextLine(), but I do not know how to use it.
May be I must use public java.lang.String nextLine() first ?
Sorry if my question is easy to solve. Thank you for your answer.
import java.util.Scanner;
public class MyClass {
public static void main(String args[]) {
String x;
Scanner keyboard = new Scanner(System.in);
System.out.println(" Add a string");
x = keyboard.nextLine();
System.out.println(x);
}
}
You should just replace next with nextLine as below
public static void main(String[] args) {
String x;
Scanner keyboard = new Scanner(System.in);
System.out.println(" Add a string");
x = keyboard.nextLine();
System.out.println(x);
}
import java.util.Scanner;
public class Hello {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int A = sc.nextInt().split(":");
int B = sc.nextInt();
System.out.println(A + B);
}
}
If I'm given an input like 1:2 then the output should be 3. Likewise 54:6 then 60.
But I'm getting an error. What should I do to achieve that output?
You cannot call split on an integer, it is meant for splitting a String. Try this:
public class Hello {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String[] numbers = sc.next().split(":");
int A = Integer.parseInt(numbers[0]);
int B = Integer.parseInt(numbers[1]);
System.out.println(A + B);
}
}
Of course some validation would be nice (check if the String contains a colon, if the parts are numeric, etc.), but this should point you in the right direction.
At first, read a whole input line into a String variable. Then just split it into two values and cast them to integer.
Code example:
Scanner sc = new Scanner(System.in);
String inputString = sc.nextLine();
String[] splittedValues = inputString.split(":");
int
A = Integer.parseInt(splittedValues[0]),
B = Integer.parseInt(splittedValues[1]);
System.out.println(A + B);
you will need to take input as string and then split your input by : and convert the string to integer and add them.
see example below
public class Hello {
public static void main(String[] args) {
String input;
Scanner sc = new Scanner(System.in);
input = sc.next();
String[] parts = input.split(":");
if(parts.length > 0) {
int sum = Integer.parseInt(parts[0])+Integer.parseInt(parts[1]);
System.out.println(sum);
} else{
System.out.println("Enter number in format example 12:2");
}
}
}```
You can try this:
import java.util.Scanner;
public class Hello {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int A = sc.nextInt();
sc.next().charAt(0);
int B = sc.nextInt();
System.out.println(A + B);
}
}
I think Splitting of two integers is not possible in java(it is possible in python by using the input.split() function) ,for that reason it is better take input as string and split input by using colon(:) operator and convert those input string to an integer and add the both to print the result.
java code:
import java.util.Scanner;
Public class TwoIntegers
{
public static void main(String args[])
{
Scanner s=new Scanner(System.in());
String[] two_numbers = s.next().split(":");
int fir_num = Int.parseInt(two_numbers[0]);
int sec_num = Int.parseInt(two_numbers[1]);
int sum=fir_num+sec_num;
System.out.println("The sum of two numbers is:"+sum);
}
}
I need to read spaces (present before string and after String) given as input using Scanner
Note : if there is no spaces given in input it should not add space in output
Please find the below code:
package practise;
import java.util.Scanner;
public class scanccls {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scan = new Scanner(System.in);
String name= scan.nextLine();
name+=scan.nextLine();
scan.close();
System.out.println("Enter your name"+name);
}
}
I am expecting output like:
Input :Enter Your name:Chandu Aakash
Output:chandu Aakash
Input: Enter Your name: (Space..)Chandu Aakash(Space..)
Output: (space.. )chandu Aakash(Space..)
Your code work fine. I just add little modification:
package practise;
import java.util.Scanner;
public class scanccls {
public static void main(String[] args) {
System.out.println("Enter your name:");
Scanner scan = new Scanner(System.in);
String name="";
name+=scan.nextLine();
scan.close();
System.out.println("Your name is :"+name);
}
}
One can use the delimiter function to segregate your input as shown below.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in).useDelimiter("\n");
String input = scanner.next();
System.out.println(input);
scanner.close();
}
}
import java.util.*;
public class Str{
public static void main(String[] args) throws IOException
{
Scanner scan = new Scanner(System.in);
int i = scan.nextInt();
double d = scan.nextDouble();
String s=" ";
s= scan.nextLine();
s+=scan.nextLine();
scan.close();
System.out.println("String: "+s);
System.out.println("Double: "+d);
System.out.println("Int: "+i);
}
}
/#esprittn solution didn't work./
my solution:
while(scan.hasNext()){
name+=scan.nextLine();
}
I use this function below, to read from all user input format, text inclusive spaces, then parse to specific datatype after.
package practice;
import java.io.*;
public class readInputSample{
public static void main(String[] args) {
String strVal = getInput("Enter string value: "); // Direct as string
Integer intVal = Integer.parseInt(getInput("Enter integer value: "));
Double dblVal = Double.parseDouble(getInput("Enter double value: "));
Float fltVal = Float.parseFloat(getInput("Enter float value: "));
System.out.println("String value: " + strVal);
System.out.println("Integer value: " + intVal);
System.out.println("Double value: " + dblVal);
System.out.println("Float value: " + fltVal);
}
// Special Function to read all user input
private static String getInput(String prompt){
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
System.out.print(prompt);
System.out.flush();
try{
return stdin.readLine();
} catch (Exception e){
return "Error: " + e.getMessage();
}
}
}
I have done a few changes in your code, it will run just fine for this code
package practise;
import java.util.Scanner;
public class scanccls {
public static void main(String[] args) {
//// TODO Auto-generated method stub
System.out.println("Enter your name:");
Scanner scan = new Scanner(System.in);
String name="";
name+=scan.nextLine();
scan.close();
System.out.println("Your name is :"+name);
}
}
package practise;
import java.util.Scanner;
public class scanccls
{
public static void main(String[] args)
{
System.out.println("Enter your name:");
Scanner scan = new Scanner(System.in);
String name = "";
name += scan.nextLine();
// Can also be done like
// String name=scan.next();
// name+=scan.nextLine();
// They Both Work as same
System.out.println("Your name is :" + name);
}
}
package practise;
import java.util.Scanner;
public class scanccls {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scan = new Scanner(System.in);
String name= scan.nextLine();
scan.close();
System.out.println("Enter your name"+name);
}
}
I have some of my code already done I just do not know how I would display the sentence in reverse order than what is entered.
For example; if one enters "my name is joe"
It should display in the output: Joe
is
name
my
import java.util.Scanner;
public class Sentence {
public static void main(String [] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a sentence: ");
String sentence = input.nextLine();
String[] words = sentence.split(" ");
// Display the array of words in
// reverse order
}
}
If I understand your problem, you can do something like that:
public static void main(String [] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a sentence: ");
String sentence = input.nextLine();
String[] words = sentence.split(" ");
for (int i = words.length - 1; i >= 0; i--) {
System.out.println(words[i]);
}
}
I'm getting this error on this piece of code and I can't figure out what's wrong.
public class enc {
//The Look-Up Table with the position of all the available characters
public static final String LUT="*, .abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
Scanner sc=new Scanner(System.in);
System.out.print("Input the sentence you want to encode.");
String s= sc.nextLine();
}
You need to put the code inside a method:
public class enc {
//The Look-Up Table with the position of all the available characters
public static final String LUT="*, .abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.print("Input the sentence you want to encode.");
String s= sc.nextLine();
}
}
The lines
Scanner sc = new Scanner(System.in);
System.out.print("Input the sentence you want to encode.");
String s = sc.nextLine();
should be in a code block such as a method rather than the class block
Try:
public class enc {
//The Look-Up Table with the position of all the available characters
public static final String LUT="*, .abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
public static void main (String args[]) {
Scanner sc=new Scanner(System.in);
System.out.print("Input the sentence you want to encode.");
String s= sc.nextLine();
}
}
Your lines:
System.out.print("Input the sentence you want to encode.");
String s = sc.nextLine();
Must be inside a method or a system initialiser block. Actually these are function calls which do not return anything. So cannot be in the class directly.
// In a method
public class enc {
Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
System.out.print("Input the sentence you want to encode.");
String s = sc.nextLine();
}
}
OR
// In a system initializer block
public class Test {
Scanner sc = new Scanner(System.in);
{
System.out.println("yoyo");
String s = sc.nextLine();
}
}
Actually those methods can be called directly in a class which actually return something. And must also be initialised to a variable of the respective type.