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);
Related
So I am trying to make a calculator in VSCode but for some reason, my scanner is not running. Here is the full code for my project so far...
import java.util.Scanner;
class main { //Main
public static void main(String[] args) {
int firstnumber = 0;
int secondnumber = 0;
String Operator = "-";
boolean erase = true;
Scanner scan = new Scanner(System.in);
while (erase = true) { //While Loop (main)
System.out.println("Enter First Number");
firstnumber = scan.nextInt();
System.out.println("Enter Second Number");
secondnumber = scan.nextInt();
System.out.println("Select Operator.");
System.out.println("For Multiplaction: Type X");
System.out.println("For Division: Type %");
System.out.println("For Addition: Type +");
System.out.println("For Subtraction: Type -");
Operator = scan.nextLine()
} //While Loop (main)
}
} //Main
For some reason when I run that code it runs the first two scanners but then fails to run the one that asks for the operator. If anyone knows why I would be pleased to know.
I did change the operator to char, andd now it works:-
import java.util.Scanner;
class main { //Main
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int firstnumber = 0;
int secondnumber = 0;
char operator;
boolean erase = true;
while (erase) { //While Loop (main)
System.out.println("Enter First Number");
firstnumber = scan.nextInt();
System.out.println("Enter Second Number");
secondnumber = scan.nextInt();
System.out.println("Select Operator.");
operator = scan.next().charAt(0);
System.out.println("For Multiplaction: Type X");
System.out.println("For Division: Type %");
System.out.println("For Addition: Type +");
System.out.println("For Subtraction: Type -");
} //While Loop (main)
}
} //Main
Maybe your nextLine() doesn't trigger because you already have two scanner prompts and so it behaves to be accepted input. Maybe you should debug more on that.
Still, scan.next(); works... Tried on Visual Studio Code 1.38.1 using Oracle JDK 1.8.0_u221
use scan.next(); instead.
public static void main(String[] args) {
int firstnumber;
int secondnumber;
String operator;
Scanner scan = new Scanner(System.in);
while (true) { //While Loop (main)
System.out.println("Enter First Number");
firstnumber = scan.nextInt();
System.out.println("Enter Second Number");
secondnumber = scan.nextInt();
System.out.println("Select Operator.");
System.out.println("For Multiplaction: Type X");
System.out.println("For Division: Type %");
System.out.println("For Addition: Type +");
System.out.println("For Subtraction: Type -");
operator = scan.next();
System.out.println(firstnumber);
System.out.println(secondnumber);
System.out.println(operator);
} //While Loop (main)
}
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);
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);
}
}
How do I change this while(true) into a do while so when the user enters a number they will be given back details but if they enter * the system will close.
while (true){
System.out.print("Enter number: ");
int option = keyboard.nextInt();
out.writeInt(option);
char option;
do {
System.out.print("Enter number: ");
option = keyboard.nextChar();
out.writeChar(option);
} while (option != '*')
You may want to use nextLine() or next() to receive the input and parse them accordingly:
do{
System.out.print("Enter number: ");
String str = keyboard.nextLine();
int option = 0;
if(str.matches"[0-9]+"){
option = Integer.parseInt(str);
System.out.println(option);
}
else if(str.equals("*"))
System.exit(0); //or use break; if you want to exit the loop
}while(whatever); //whatever == true
If you change it to allow the user to input a String and then convert to int if possible, you can catch any errors and break out if the user enters a '*' character:
import java.util.*;
class MainInput{
public static void main(String[] args){
Scanner keyboard = new Scanner(System.in);
int option;
String input;
do{
System.out.print("Enter number: ");
input = keyboard.nextLine();
try{
option = Integer.parseInt(input);
System.out.println("You entered the value: " + option);
}
catch(Exception ex) {
if (!input.equals("*")){
System.err.println("Invalid input, please enter numbers only.");
}
}
}while(!input.equals("*"));
}
}
If you use an exit value like (-1), you can continue to process input with nextInt() and becomes even easier. You can do this with a simple do/while:
import java.util.*;
class MainInput{
public static void main(String[] args){
Scanner keyboard = new Scanner(System.in);
int option = 0;
do{
System.out.print("Enter number: ");
option = keyboard.nextInt();
System.out.println("You entered the value: " + option);
}while(option != -1);
}
}
do{
System.out.print("Enter number: ");
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
}while(!str.equals("*"))
edit: if you want to play with numbers as integer or double. Just let me know and I'll add casted version as your needs.
int number;
do{
System.out.print("Enter number: ");
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
number = Integer.parseInt(str);
}while(!str.equals("*"))
now you have a integer number.
This is my java code...I want to make it double click run forever after compiling once..!
Please Guide me How can I make this .. ?
import java.util.Scanner;
public class Functions {
public static int addnumbers(){
Scanner input = new Scanner(System.in);
int a,b;
System.out.println("\nMethod Call - Addition");
System.out.print("\nEnter a = ");
a=input.nextInt();
System.out.print("Enter b = ");
b=input.nextInt();
return(a+b);
}
public static int subnumbers(){
Scanner input = new Scanner(System.in);
int a,b;
System.out.println("Method Call - Subtraction");
System.out.print("\nEnter a = ");
a=input.nextInt();
System.out.print("Enter b = ");
b=input.nextInt();
return(a-b);
}
public static int mulnumbers(){
Scanner input = new Scanner(System.in);
int a,b;
System.out.println("Method Call - Multiply");
System.out.print("\nEnter a = ");
a=input.nextInt();
System.out.print("Enter b = ");
b=input.nextInt();
return(a*b);
}
public static int quonumbers(){
Scanner input = new Scanner(System.in);
int a,b;
System.out.println("Method Call - Quotient");
System.out.print("\nEnter a = ");
a=input.nextInt();
System.out.print("Enter b = ");
b=input.nextInt();
return(a/b);
}
public static void main(String args[]) {
Scanner input = new Scanner(System.in);
int n;
System.out.println("**Calcultor**\n\n1.Addition\n2.Subtraction\n3.Multiplication\n4.Quotient");
System.out.print("\nEnter no : ");
n=input.nextInt();
if(n==1) {
System.out.println("\nResult = "+addnumbers());
}
if(n==2) {
System.out.println("\nResult = "+subnumbers());
}
if(n==3) {
System.out.println("\nResult = "+mulnumbers());
}
if(n==4) {
System.out.println("\nResult = "+quonumbers());
}
System.out.println("\nControl exit!");
}
}
Tell me any way to do this....
Launch4j
JSmooth
probably a lot more.
These are programs that wrap the Jar file and dependencies to create a native executable for Windows.