package day1.examples;
import java.util.*;
public class AdvancedInput {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
char a,b;
System.out.println("Enter Your First Name");
a = sc.next().charAt(0);
System.out.println("Enter Your Last Name");
b = sc.next().charAt(0);
System.out.println("Your Full name is:");
System.out.println(a+b);
}
}
Would this solve the problem?
System.out.println("Your Full name is:");
System.out.println(a+" "+b);
Related
I can get and print the integer value in java but I am confuse how to get and print string. Can someone help
package hello;
import java.util.Scanner;
public class hello {
public static void main(String[] args) {
int integer;
System.out.println("Please Enter Integer");
Scanner sc = new Scanner(System.in);
integer = sc.nextInt();
sc.close();
System.out.println("you entered : " +integer);
}
}
Program output
Please Enter Integer
5
you entered : 5
I am stuck in this program. I don't understand how to get string and print on screen
import java.util.Scanner;
public class hello {
public static void main(String[] args) {
int name;
Scanner sc = new Scanner(System.in);
System.out.println("Enter your name");
name = sc.nextInt();
sc.close();
System.out.println("Your name"+name);
}
}
You need to change your type value name from int to String. And replace sc.nextInt() by sc.nextLine() or sc.next().
Example
public static void main(String[] args) {
String name;
Scanner sc = new Scanner(System.in);
System.out.println("Enter your name");
name = sc.nextLine();
sc.close();
System.out.println("Your name " + name);
}
Use sc.nextLine() for reading string inputs
or
sc.next() (But this will read only a word before it encounters a space)
You can also use InputStreamReader for this purpose
eg.
BufferedReader br = new BufferedReader(new InputStreamReader(System.in()));
String input = br.readLine();
name = sc.nextInt(); doesn't work for strings, only for integers, you should use sc.nextline instead.
And also you have to change int name to String name, due to other type of variable.
Your code should look like this:
import java.util.Scanner;
public class hello {
public static void main(String[] args) {
String name;
Scanner sc = new Scanner(System.in);
System.out.println("Enter your name");
name = sc.nextLine();
sc.close();
System.out.println("Your name"+name);
}
}
change int name to string name and use sc.nextLine()
import java.util.Scanner;
public class hello {
public static void main(String[] args) {
String name;
Scanner sc = new Scanner(System.in);
System.out.println("Enter your name");
name = sc.nextLine();
sc.close();
System.out.println("Your name"+name);
}
}
import java.util.*;
public class MovieInfo
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.println("Enter the name of the film");
String name = in.nextLine();
System.out.println("Enter the film budget");
int bud = in.nextInt();
System.out.println("Enter the running time");
double time = in.nextDouble();
System.out.println("Enter the film production house");
String s1 = in.nextLine();
System.out.println("Film being screened : "+name);
System.out.println("Budget "+bud+" cores");
System.out.println("Running time "+time+" hours");
System.out.println("Production : "+s1);
}
}
In the output of the above program, it does not take input for String s1.
How do I fix it?
Any help would be appreciated.
Try to put the following additional line:
in.nextLine();
after that line:
double time = in.nextDouble();
to consume new line character (it's created when you accept input double with Enter key) that is not being consumed with .nextDouble() call.
Please refer to link to that question, provided by LenosV, to get detailed information on what's the reason of that behavior of Scanner.
This will work
import java.util.*;
public class MovieInfo
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.println("Enter the name of the film");
String name = in.nextLine();
System.out.println("Enter the film budget");
int bud = in.nextInt();
System.out.println("Enter the running time");
double time = in.nextDouble();
System.out.println("Enter the film production house");
String s1 = in.next();
System.out.println("Film being screened : "+name);
System.out.println("Budget "+bud+" cores");
System.out.println("Running time "+time+" hours");
System.out.println("Production : "+s1);
}
}
I'm trying to create a script that will ask a user to input a word and then display back the letters entered by the user in vertical order. The problem is that I'm required to use a while loop, any ideas??????
import java.util.Scanner;
public class VerticalWords {
public static void main(String[] args) {
System.out.println("Enter A word");
Scanner scan = new Scanner(System.in);
String word = scan.nextLine();
for(char a : word.toCharArray())
{
System.out.println("Letter: " + a);
}
}
}
I have tried that code, and it works but its not a while loop ^
Try this:
System.out.println("Enter A word");
Scanner scan = new Scanner(System.in);
int a = 0;
String word = scan.nextLine();
while(a < word.length){
System.out.println(word.charAt(a));
a++;
}
import java.util.Scanner;
public class VerticalWords {
public static void main(String[] args){
// TODO Auto-generated method stub
System.out.println("Enter A word: ");
Scanner scan = new Scanner(System.in);
int a = 0;
String word = scan.nextLine();
while(a < word.length())
{
System.out.println("Letter: " + (word.charAt(a)));
a++;
}
}
}
Here the input for string is " hi how are you" but I am getting output as "hi" alone. Do let me know where is the error. I have tried out with nextLine() also.
import java.util.Scanner;
public class Stringintalter {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
System.out.println("enter the num");
int a = sc.nextInt();
System.out.println("Enter the Double");
double b = sc.nextDouble();
System.out.println("Enter the string");
String c = sc.next(); //tried out with nextLine() also.
System.out.println(c);
System.out.println(b);
System.out.println(a);
}
}
This is basic error only. I browsed a lot still I am not able to sort out the error.
everytime you do
int a = sc.nextInt();
double b = sc.nextDouble();
your scanner is not reading the newLine token, so you should do something like:
System.out.println("enter the num");
int a = sc.nextInt();
sc.next();
System.out.println("Enter the Double");
double b = sc.nextDouble();
sc.next();
System.out.println("Enter the string");
String c = sc.next(); //tried out with nextLine() also.
This code worked out for me.
import java.util.Scanner;
public class Stringintalter {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("enter the num");
int a = sc.nextInt();
System.out.println("Enter the Double");
double b = sc.nextDouble();
System.out.println("Enter the string");
sc.nextLine();
String c = sc.nextLine();
System.out.println(c);
System.out.println(b);
System.out.println(a);
}
}
Maybe this code will help you.
Scanner u = new Scanner(System.in);
Scanner sc=new Scanner(System.in);
System.out.println("enter the num");
int a = sc.nextInt();
System.out.println("Enter the Double");
double b = sc.nextDouble();
System.out.println(b);
System.out.println(a);
String c;
c = u.nextLine();
System.out.println(c);
import java.util.Scanner;
public class Trial {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Please enter the first string: ");
String one = input.nextLine();
System.out.println("Please enter the second string: ");
String two = input.nextLine();
.....
Try this:
import java.util.Scanner;
public class Trial {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Please enter the first string: ");
String one = input.nextLine();
System.out.println("Please enter the second string: ");
String two = input.nextLine();
StringBuilder sb = new StringBuilder();
for (int i = one.length() - 1, j = two.length() - 1; i >= 0 && j >= 0; i--, j--) {
if (one.charAt(i) != two.charAt(j)) {
break;
}
sb.append(one.charAt(i));
}
System.out.println(sb.reverse().toString());
}
}
I hope, that this piece of code is self-explanatory.
You can also use Google Guava to find a common suffix:
com.google.common.base.Strings.commonSuffix(str1, str2)
import java.util.Scanner;
import com.google.common.base.Strings;
public class Trial {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Please enter the first string: ");
String one = input.nextLine();
System.out.println("Please enter the second string: ");
String two = input.nextLine();
System.out.println("Common suffix: " + Strings.commonSuffix(one, two));
}
}