I am trying to figure out part of a Java assignment and I am lost on what to do. If anyone can help or guide me in the right direction I would really appreciate it!
The user has to enter a six digit "ticket number". I am trying to figure out how to drop the last digit of the number that the user input and place it into it's own variable.
For instance:
Ticket Number: 123456
How do I drop the "6" to make the number "12345" and put the 6 in its own variable?
I tried using the \\b to backspace the last digit after turning the input into a string, but that did not work. And I have realized that it would just delete the number anyway if it did work.
Once the last digit is dropped we have to do calculations to get a remainder... I'm not worried about that, I have found that described in our book, I just can't figure out how to drop the digit and put it by itself.
I am very new to Java (this is a Chapter 2 assignment) so please go easy on me!
Thank you everyone for your help. I appreciate it!
This is what I came up with and the code works:
import javax.swing.JOptionPane;
public class TicketNumber
{
public static void main(String[] args)
{
String ticketNumber;
int Ticket, Remainder, lastDigit;
ticketNumber = JOptionPane.showInputDialog(null,
"Enter the six digit ticket number",
"Verify Ticket", JOptionPane.INFORMATION_MESSAGE);
Ticket = Integer.parseInt(ticketNumber) / 10;
lastDigit = Integer.parseInt(ticketNumber) % 10;
Remainder = Ticket % 7;
boolean Valid = lastDigit == Remainder;
JOptionPane.showMessageDialog(null, "Valid Ticket" + "\n" + Valid);
}
}
It depends on what data type you are working with. If the value is a String
, you can use the substring
method:
String ticket = "123456";
String allButLast = ticket.substring(0, ticket.length() - 1); // becomes "12345"
If it is an int
value, you can use integer division:
int ticket = 123456;
int allButLast = ticket / 10; // truncates to 12345
If it's a float
or double
, you can divide by 10 and cast to an int
or you can use Math.floor()
to drop the fractional part after dividing by 10.
EDIT: Based on the edit to your question, you can do this:
public class TicketNumber
{
public static void main(String[] args)
{
String ticketNumber;
int ticket, prefix, remainder, lastDigit;
ticketNumber = JOptionPane.showInputDialog(null,
"Enter the six digit ticket number",
"Verify Ticket", JOptionPane.INFORMATION_MESSAGE);
ticket = Integer.parseInt(ticketNumber);
prefix = ticket / 10;
lastDigit = ticket % 10;
remainder = prefix % 7;
boolean valid = lastDigit == remainder;
JOptionPane.showMessageDialog(null, "Valid Ticket" + "\n" + valid);
}
}
Note that Ticket
, etc. are not good Java variable names; they should start with lower-case letters (eg, ticket
). I made that change in my version of the code.
Note that the calculation can be written more tersely as:
boolean valid = (ticket % 10) == ((ticket / 10) % 7);
and you can eliminate the variables prefix
, remainder
, and lastDigit
.
You can use modulo operator %
and division operator /
int number = 123456;
int remainder = 123456 % 10;
int newNumber = 123456 / 10;
System.out.println(newNumber); // output is 12345
System.out.println(remainder); // output is 6
int n=123456,m=n/10;
System.out.println("Number = "+m);//your code
Your Output:-
Number = 12345
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.