I'm getting an error. It's saying that it's expecting an int, but i'm giving it a string. I'm not sure how to fix this.
int DNAstrandlength, strandDNA;
Scanner DNAInfo = new Scanner (System.in);
System.out.println ("Please enter a DNA Strand!");
strandDNA = DNAInfo.next();
DNAstrandlength = strandDNA.length();
You have to declare strandDNA as String.
int DNAstrandlength;
String strandDNA;
Scanner DNAInfo = new Scanner (System.in);
System.out.println ("Please enter a DNA Strand!");
strandDNA = DNAInfo.next();
DNAstrandlength = strandDNA.length();
If you are reading an int then you have nextInt() from scanner.
Related
I want to understand why using Scanner.nextInt() to read a number and then using Scanner.nextLine() to read a sentence does not work as expected. I have the following code where I input a number but it skips listening to the sentence and my program terminates. Could someone explain why this is and what are alternate solutions?
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a number: ");
int x = scanner.nextInt();
System.out.println("Enter a sentence");
System.out.println(x);
String line = scanner.nextLine();
System.out.println(line);
In your code, you have to write scanner.nextLine() after int x = scanner.nextInt();
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a number: ");
int x = scanner.nextInt();
scanner.nextLine();
System.out.println("Enter a sentence");
System.out.println(x);/*be careful,
here you write the result of x and below you are going to write the sentence.
I think you better write
System.out.println(x);
above
System.out.println("Enter a sentence");*/
String line = scanner.nextLine();
System.out.println(line);
You need a different Scanner instance for String receiving and another for Integer
Scanner scanNumbers = new Scanner(System.in);
Scanner scanString = new Scanner(System.in);
System.out.println("Enter a number: ");
int x = scanNumbers.nextInt();
System.out.println(x);
System.out.println("Enter a sentence");
String line = scanString.nextLine();
System.out.println(line);
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a number: ");
int x = scanner.nextInt();
System.out.println(x);
scanner.nextLine(); // skip the newline character
System.out.println("Enter name");
String name1 = scanner.nextLine();
System.out.println(name1);
I have a for loop and the method to call it is printMe() and I want the user to be able to determine how many times it runs with their input. This is along the lines of what I think it should be:
System.out.println("Enter a number: ");
loop = input.nextInt();
This should work:
Scanner input = new Scanner(System.in);
int loop = input.nextInt();
for(int i = 0; i<loop;i++){
//do the thing
}
Read the user input thanks to a scanner
Scanner sc = new Scanner(System.in);
int loopNum = sc.nextInt();
for (int i=); i<loopNum; i++){
printMe();
}
Should be something like:
for (int i=0; i<loopNum; i++)
printMe();
Cheers
yes it is correct if your variable input from class Scanner. You can declare variable sc like this and call it many times if you want to read more input from users.
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number: ");
loopNum = sc.nextInt();
I'm trying to read the input which is in the following format.
2
asdf
asdf
3
asd
df
2
Following is the code:
Scanner scanner = new Scanner(System.in);
int t = scanner.nextInt();
System.out.println(t);
while(t>0){
String a = scanner.next();
String b = scanner.next();
int K = scanner.nextInt();
}
But when I'm scanning, I'm getting empty t=2 , a="" , b=asdf, K=asdf
Can't figure out the issue. There is no space/new line between 2 and asdf.
I have tried using scanner.nextLine() instead of scanner.next() but no change
nextInt() doesn't cosume the newline token, so the following read will get it. You could introduce a nextLine after the nextInt to skip it:
Scanner scanner = new Scanner(System.in);
int t = scanner.nextInt();
scanner.nextLine(); // Skip the newline character
System.out.println(t);
while(t > 0) {
String a = scanner.next();
String b = scanner.next();
int K = scanner.nextInt();
scanner.nextLine(); // Skip the newline character
}
Another approach I prefer:
Scanner scanner = new Scanner(System.in);
int t = Integer.parseInt(scanner.nextLine());
System.out.println(t);
while(t>0){
String a = scanner.nextLine();
String b = scanner.nextLine();
int K = Integer.parseInt(scanner.nextLine());
}
But note it will throw NumberFormatException when input is incorrect.
This question already exists:
Scanner issue when using nextLine after nextXXX [duplicate]
Closed 8 years ago.
I am trying to input values of certain string and integer variables in Java.
But if I am taking the input of string after the integer, in the console the string input is just skipped and moves to the next input.
Here is the code
String name1;
int id1,age1;
Scanner in = new Scanner(System.in);
//I can input name if input is before all integers
System.out.println("Enter id");
id1 = in.nextInt();
System.out.println("Enter name"); //Problem here, name input gets skipped
name1 = in.nextLine();
System.out.println("Enter age");
age1 = in.nextInt();
This is a common problem, and it happens because the nextInt method doesn't read the newline character of your input, so when you issue the command nextLine, the Scanner finds the newline character and gives you that as a line.
A workaround could be this one:
System.out.println("Enter id");
id1 = in.nextInt();
in.nextLine(); // skip the newline character
System.out.println("Enter name");
name1 = in.nextLine();
Another way would be to always use nextLine wrapped into a Integer.parseInt:
int id1;
try {
System.out.println("Enter id");
id1 = Integer.parseInt(input.nextLine());
} catch (NumberFormatException e) {
e.printStackTrace();
}
System.out.println("Enter name");
name1 = in.nextLine();
Why not just Scanner.next() ?
I would not use Scanner.next() because this will read only the next token and not the full line. For example the following code:
System.out("Enter name: ");
String name = in.next();
System.out(name);
will produce:
Enter name: Mad Scientist
Mad
It will not process Scientist because Mad is already a completed token per se.
So maybe this is the expected behavior for your application, but it has a different semantic from the code you posted in the question.
This is your updated working code.
package myPackage;
import java.util.Scanner;
public class test {
/**
* #param args
*/
public static void main(String[] args) {
String name1;
int id1,age1;
Scanner in = new Scanner(System.in);
//I can input name if input is before all integers
System.out.println("Enter id");
id1 = in.nextInt();
System.out.println("Enter name"); //Problem here, name input gets skipped
name1 = in.next();
System.out.println("Enter age");
age1 = in.nextInt();
}
}
May be you try this way..
Instead of this code
System.out.println("Enter name"); //Problem here, name input gets skipped
name1 = in.nextLine();
try this
System.out.println("Enter name");
name1 = in.next();
How can I get 2 or more integer values of user with Scanner class then check them; if they all are integer, run some statements and if aren't just show a warning not crash!
I wrote this code but it can't be ok with Java! Of course I know where is the problem from. I only want something like this :
Scanner sc = new Scanner(System.in);
System.out.println("Enter the first value: ");
String v1 = sc.nextLine() ;
System.out.println("Enter the second value: ") ;
String v2 = sc.nextLine() ;
if(v1.hasNextInt() && v2.hasNextInt()){ }
Change your code to:
Scanner sc = new Scanner(System.in);
System.out.println("Enter the first value: ");
int v1 = sc.nextInt() ; // could also use hasNextInt() before this line
System.out.println("Enter the second value: ") ;
int v2 = sc.nextInt() ;// could also use hasNextInt() before this line
// you have 2 int values..
org.apache.commons.lang3.StringUtils has some great utility methods for checking strings. In this scenario I would do:
Scanner sc = new Scanner(System.in);
System.out.println("Enter the first value: ");
String v1 = sc.nextLine() ;
System.out.println("Enter the second value: ") ;
String v2 = sc.nextLine() ;
if(StringUtils.isNumeric(v1) && StringUtils.isNumeric(v2)){ }
You can use sc.nextInt() for taking input as an integer
However if you want to take input as a string then
just use Integer.parseInt(String value);
This method will take String value as an argument and convert it to Integer.However If the String value can be converted to integer then fine,else this method throws NumberFormatException which you can catch by implementing Exception Handling and avoid your program from crashing.
See various ,methods of Scanner class for taking different type of
input
Scanner in=new Scanner(System.in);
integer = in.nextInt();
longInteger = in.nextLong();
realNumber = in.nextFloat();
doubleReal = in.nextDouble();
string1 = in.nextLine();
You can use .matches("-?\\d+") expression on a String to check whether it's an Integer.
{
int number;
if (v1.matches("-?\\d+"))
number = Integer.parseInt(v1);
else
System.out.println("It's not a number!")
}