how and where to use standard input flush in java? [closed] - java

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
import java.io.IOException;
import java.util.Scanner;
public class fflush{
public static void main(String [] args) throws IOException{
Scanner input=new Scanner(System.in);
int a;
System.out.print("Enter an integer:");
a = input.nextInt();
String b,c;
System.out.print("Enter a string:");
b = input.nextLine();
System.out.print("Enter another string:");
c = input.nextLine();
}
}
//result is//
Enter an integer:4
Enter a string:Enter a nother string:

You only "flush" the output in Java.
I suspect you mean, when to discard the rest of the line. To do this you can call
input.nextLine();
You need to do this after nextInt() as you expect to be reading from the next line.

Related

how to transform a char to a unicode java [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
import java.util.*;
public class Ex13 {
static Scanner in = new Scanner(System.in);
public static void main(String[] args) {
char firstL,secondL;
System.out.println("insert first l");
firstL=in.next().charAt(0);
System.out.println("insert second l");
secondL=in.next().charAt(0);
if (){
System.out.println("kk");
}
}
}
I need firstL and secondL to equal in the if section but I can't because the firstL and the secondL is a char type and I need to transform it to a Unicode so I can finish the project
Why don't you just check the value?
char firstL,secondL;
System.out.println("insert first l");
firstL=in.next().charAt(0);
System.out.println("insert second l");
secondL=in.next().charAt(0);
if (firstL == secondL){
System.out.println("kk");
}

How to input in array using java [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Hello i have been trying to make a program THAT ASK
1)How many students are there??
2)Asks a name and Test marks
can somebody help me with this . Im new to programming here is my code
Scanner input = new Scanner(System.in);
System.out.println("How many Students in class?");
int n = input.nextInt();
System.out.println("Enter the " + n + " names: ");
String [] names = new String[n];
for (int i = 0; i < names.length; i++)
{
names[i] = input.nextLine();
}
It is a "problem" with nextInt, the method only read the number and leave the <enter> so the next time you call nextLine, the <enter> being present, it will read this, so the value is empty. You need to clear the input first, simply by reading the line.
System.out.println("How many Students in class?");
int n = input.nextInt();
input.nextLine(); //will consume the \n

Java getString method [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
So I have the questions in the top part, but I want to have all the questions at the top, and when I need to ask the questions, I can just pull them down using a variable defined as the question. Right now however, the code is asking the questions from where the questions are, not using the variable "ask" and asking from System.out.print(ask). Any ideas on how to get it to do that?
import java.util.Scanner;
public class Greetings {
public static void main(String[] args) {
Scanner newscanner = new Scanner(System.in);
String ask = getString(newscanner, "Please enter your first name: ");
// String ask2 = getString(newscanner, "Please enter your last name: ");
// String ask3 = getString(newscanner, "Please enter your year of birth:
// ");
}
public static String getString(Scanner newscanner, String ask) {
System.out.print(ask);
String first = newscanner.next();
String firstletter = first.substring(0, 1).toUpperCase();
return firstletter;
}
}
Perhaps what you are looking to do is have the question be printed, and then the answer typed on the line below it? If so, what you need to do is change the first call in getString from System.out.print to System.out.println, which should add on a newline after the question, moving the input to the next line.
EDIT: This is what it might look like now:
Please enter your first name:John
And here's what it would change to:
Please enter your first name:
John

I want to print an Array ...Was Just checking for sorting but not able to print too? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
import java.util.*;
class Sort {
public static void main(String..s) {
int a[]=new a[5];
Scanner sc=new Scanner(System.in);
System.out.println("Enter 5 Numbers");
for(int i=0;i<5;i++) {
a[i]=sc.nextInt();
}
for(int i=0;i<5;i++) {
System.out.println("Before Sorting"+ a[i]);
}
}
}
Please change
int a[]=new a[5];
to
int a[]=new int[5];
Also change String..s to String s[]
Please comment for further help.
Try these utility functions:
java.util.Arrays.sort(...): Sorts the specified array
java.util.Arrays.toString(...): Returns a nicely formatted string representation of the array. Don't forget to print it out :-)

the result of using modulo in my java code is not correct why? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
this is my code, when I enter 25000 the result being (25000=1,5000=1) while it must be only 25000=1, but if I enter 50,000 the result correct 50,000=2 but only for 25000 is wrong I don't know why it is
import java.util.Scanner;
class Mawa{
public static void main(String[]a){
int bestwpenj,da,penj,num;
Scanner input=new Scanner(System.in);
System.out.println("enter a number");
System.out.println();
num=input.nextInt();
bestwpenj=num/25000;
da=(num%25000)/10000;
penj=(num%10000)/5000;
System.out.println("25000= "+bestwpenj+"\n"+"10000= "+da+"\n"+"5000= "+penj);
}}
thanks for helping.
You mean like this?
import java.util.Scanner;
class Mawa{
public static void main(String[]a){
int bestwpenj,da,penj,num;
Scanner input=new Scanner(System.in);
System.out.println("enter a number");
System.out.println();
num=input.nextInt();
bestwpenj=num/25000;
da=(num%25000)/10000;
penj=(num%25000%10000)/5000;
System.out.println("25000= "+bestwpenj+"\n"+"10000= "+da+"\n"+"5000= "+penj);
}}

Categories

Resources