How to get user code with letter and number on one line? [closed] - java

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed yesterday.
Improve this question
I am trying to create a program in which the user enters the card number. The card consists of the first letter 'A' and a four-digit number. Unfortunately, the 'scanner' accepts these values in separate lines, and I would like the user to enter this in one.
System.out.print("Please write the number: ");
// number card A1111
String char1 = sc.nextLine();
int number3 = sc.nextInt();
It seems to me that in this case you need to use String char1 = sc.next(String.valueOf(sc.nextInt())); but the compiler prints me an error.

Related

Large string input isn't being read completely, on an online judge [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 days ago.
Improve this question
There's a question on Codechef. A test case in this, where a string of length 100000, is being input. But when i print the length of string after input, its only coming out to be 65526. Why is that so? Is the input not being taken properly?
For input I use,
JAVA CODE:
BufferedReader br = new BufferedReader(new InputStreamReader(System.in), 131072);
int n = Integer.parseInt(br.readLine());
String c = br.readLine();
NOTE : I also tried doing it in C++, but same problem occurs there.
I also tried, increasing the buffer size in case it causes problem, but no effect.
Also, Scanner didn't work in java!
Can someone please tell me what's going wrong?

Why i can't type String letter in the employee's department? [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 1 year ago.
Improve this question
What's wrong on my code? i can't even resolved it
This is a classic issue. When you read nextDouble(), the enter character at the end of the double will not be read in the double itself, but the next nextLine() will just read that enter character and will not allow you to type anything.
For your particular code, there are 2 ways to fix it
change the department and salary order, so that salary will be the last thing to read. Since you are not reading data in a loop, this should do it
If you want to read data in a loop, then only good solution will be read salary as nextLine() too, and use Double.parseDouble() to parse it to double. As double salary = Double.parseDouble(in.nextLine());

I have a string with a number and a letter, is there a way to move the integer into a separate int variable? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
So I'm working on a school project and it includes a Combined Gas Law calculator where the user may input the temperature in °celsius (e.g. 1°C) and the code converts it to kelvin; if the user's input is in kelvin already, it does not do that and continues with the equation. So does anyone know how I can separate the two data types into two different variables in Java?
You could split over a specific string (degree character for example), store in a String array and parse the first element. Something like this:
String str = "47°C"
String[] strArray = str.split("°");
int number = Integer.parseInt(strArray[0]);
Congratulations, you are working on something but not writing it's code. I can tell you a few tips about how to implement it's code.
You have a string that has some numbers in it, and also has unit. Try searching 'How to extract numbers from a string'. Now you have a number.
Find the unit in the string by looking the last character in the string.
If the condition is ok for your homework, calculate the new value.
Print the result.

How to split spacing and | in java [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I'm having trouble splitting both at once right now as I am trying to split the code below
RCMP|Colour Demon|Magenta Boxer
NUPI|Number Picker|Random Numburrs
QUFI|Quick Fingers|Digitology
and here is my method that involves it. It worked for the others but because this one has spacing I am now having trouble
while (input.hasNext()) {
str = input.nextLine();
temp = str.split("\\s|\\|")
System.out.println(temp[0]) // should print RCMP
// Array out of bounds error
System.out.println(temp[1]) // should print COLOUR
Game game = new Game(temp[0],(temp[1]+temp[2]),(temp[3] + temp[4]))
}
the intended output that I want is something like this or better
RCMP- temp[0]
Colour - temp[1]
Demon - temp[2]
Magenta - temp[3]
Boxer - temp[4]
It is so I can store them into the game
When i attempt to print temp[1] which should be Colour it gives an array out of bounds error instead
Use
scan.nextLine()
To get the string
And split using regex:
yourString.split("\\s|\\|");
for example the line: RCMP|Colour Demon|Magenta Boxer would make:
{"RCMP", "Colour", "Demon", "Magenta", "Boxer"}

Golf score array in 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 8 years ago.
Improve this question
I need help on approaching this problem for my CHS Intro to programming class... The problem states:
Writer a program that asks a user how many rounds of golf they played. Using an array, have the user enter what their scores were for each round of golf they played. Sum the users score and display the total to the user.
What I am confused on is the Array part of the problem... Would it be correct for the user input to be the array size? If so how can I code it so that the user is able to input the arrays size. Thanks in advance.
I'd use:
// TODO: Ask how many rounds
...
int total = 0;
int[] scores = new int[numberOfRounds];
for (int round = 0; round < numberOfRounds; round++) {
// TODO: Ask for score
scores[round] = score;
total += score;
}
I'm not sure of the purpose of putting the scores in an array, but if that's what's asked for, I guess you have to!

Categories