Java difference [duplicate] - java

This question already has answers here:
Integer.parseInt(scanner.nextLine()) vs scanner.nextInt()
(4 answers)
Integer.parseInt(scan.next()) or scan.nextInt()?
(4 answers)
Closed 5 years ago.
Hi I'm just beginner in Java and I don't undestand one thing what's the difference between this two codes:
System.out.println("Write a number: ");
int a = Integer.parseInt(sc.nextLine());
System.out.println("Write a number: ");
int b = sc.nextInt();

In nextLine() if you input number values that it will consider as String.
Then it is parsed to Integer and stored in int a
In nextInt() the entered numerical value is considered Int and stored in int b

Related

Scanner object doesn't read user input; java.util.NoSuchElementException: No line found [duplicate]

This question already has answers here:
Java -- Closing Scanner and Resource Leak
(3 answers)
Close a Scanner linked to System.in
(5 answers)
Closed 2 years ago.
I don't know why when I run this part of the code, it doesn't let me type in anything.
import java.util.Scanner;
public class IRA {
public void select() {
Scanner options = new Scanner(System.in);
System.out.println("What do you want to do: \na) Add cash \nb) Invest \nc) Transfer money \nd) Exchange currencies");
System.out.println("Please, enter a, b, c or d: ");
String letter = options.nextLine();
char c = letter.charAt(0);

Split a string using white space java [duplicate]

This question already has answers here:
How do I split a string in Java?
(39 answers)
Closed 4 years ago.
I have a String like the following 2 4 12 12 yellow Hi how are you
I want to split the string like this {2,2,12,12,yellow, Hi how are you} in order to pass the items in the list as parameters in a constructor.
Any help?
The trivial answer is to split the string:
String[] fragments = theString.split(" ", 6);
Given that the fragments have specific meanings (and presumably you want some of them as non-string types), it might be more readable to use a Scanner:
Scanner sc = new Scanner(theString);
int x = sc.nextInt();
int y = sc.nextInt();
int width = sc.nextInt();
int height = sc.nextInt();
String color = sc.next();
String message = sc.nextLine();
This approach is also easier if you are reading these strings, say, from a file or standard input: just create the Scanner over the FileReader/InputStream instead.

Unable to increment a variable scanned in java [duplicate]

This question already has answers here:
concatenating string and numbers Java
(7 answers)
Closed 5 years ago.
Hello I'm a beginner in Java and i'm finding issuses to increment the variable age as it is red as a string or it is an integer
This is the Code :
public static void main(String[] args)
{
Scanner name= new Scanner(System.in);
System.out.println("Hello your name is "+name.nextLine());
Scanner in = new Scanner(System.in);
int age = in.nextInt();
System.out.println("In 17 September i will become "+age+1+" years old");
}
Whenever you add a String in a print statement, all further + signs are considered to be the concatenation operator, instead of the addition sign.
To fix, enclose it in brackets, like (age+1).

char digit to integer conversion [duplicate]

This question already has answers here:
Using charAt method, won't add them as an Int, and wont print as string. Will explain better
(4 answers)
Using charAt in System.out.println displays integer?
(5 answers)
Why does this code print the ASCII value instead of the character
(2 answers)
How to use java.util.Scanner to correctly read user input from System.in and act on it?
(1 answer)
Closed 5 years ago.
I'm getting the user to enter a string of numbers, then a for loop should pick out each number from the string and add it to an ArrayList. I'm sure someone can help me out fairly quickly
My problem is as follows. When I print out all the values in the ArrayList, It is printing out much higher numbers e.g. 1234 = 49 50 51 52.
I think what is happening is that it is printing out the ASCII values rather than the numbers themselves. Can anyone spot where and why this is happening?
I have tried changing the int variable barcodeNumberAtI to a char, which yields the same result.
Apologies for lack of comments but this was only supposed to be a quick program
int tempNewDigit;
String barCode, ans;
int barcodeNumberAtI;
ArrayList <Integer> numbers = new ArrayList <Integer>();
public void addNumbers(){
Scanner s = new Scanner(System.in);
do{
System.out.println("Please enter a 12 digit barcode\n");
barCode = s.nextLine();
for(int i = 0; i < barCode.length(); i++){
barcodeNumberAtI = barCode.charAt(i);
System.out.println(barcodeNumberAtI);
numbers.add(barcodeNumberAtI);
}
System.out.print("Would you like to add another? y/n\n");
ans = s.nextLine();
} while (!ans.equals("n"));
}
public void displayNumbers(){
for(int i = 0; i < numbers.size(); i++){
System.out.print(numbers.get(i));
}
}
Happens at this line: barcodeNumberAtI = barCode.charAt(i);
barCode.charAt(i) returns a char which is converted to a int by using its ASCII value.
Use this instead:
barcodeNumberAtI = Character.digit(barCode.charAt(i), 10);
What Character.digit does is converting its first argument from the type char to the corresponding int in the radix specified by the second argument.
Here's a link to the documentation

Able to input name for the first object alone [duplicate]

This question already has answers here:
Scanner is skipping nextLine() after using next() or nextFoo()?
(24 answers)
Closed 7 years ago.
for(i=0;i<5;i++)
{
System.out.println("Enter name for student"+i);
stud[i].name=v.nextLine();
System.out.println("Enter number of the student"+i);
stud[i].regno=v.nextInt();
}
v is the scanner object. I am able to input name for only the 1st object in the array of object. when the loop accesses second object it directly asks me for the regno, not for the name!
Change to
System.out.println("Enter name for student"+i);
stud[i].name=v.next();

Categories

Resources