简体   繁体   中英

How do I convert from Integer Queue to String?

OK, so I have this very simple, very inefficient program, but that's alright, the only thing that I would like to know is how to print the first five numbers of the queue as zeros without changing the values of ssn1 through ssn5.

For example I would like to print 000001111, and not 111111111, but still have those values stored in the queue. Could someone help me with that? I simply would like to hide them.

I am trying to convert the integers of the queue into a string, create a substring, and then print the dummyint five time followed by the substring.

import java.util.*;

public class SSNQueue {
public static void main (String args[]) {

  Scanner scan = new Scanner(System.in);
  Queue<Integer> ssn = new LinkedList<Integer>();

  int Dummyssn = 0;

  System.out.println("Please enter each digit number of SSN");

  System.out.print("Enter digit number 1: ");
  int ssn1 = scan.nextInt();
  System.out.print("Enter digit number 2: ");
  int ssn2 = scan.nextInt();
  System.out.print("Enter digit number 3: ");
  int ssn3 = scan.nextInt();
  System.out.print("Enter digit number 4: ");
  int ssn4 = scan.nextInt();
  System.out.print("Enter digit number 5: ");
  int ssn5 = scan.nextInt();
  System.out.print("Enter digit number 6: ");
  int ssn6 = scan.nextInt();
  System.out.print("Enter digit number 7: ");
  int ssn7 = scan.nextInt();
  System.out.print("Enter digit number 8: ");
  int ssn8 = scan.nextInt();
  System.out.print("Enter digit number 9: ");
  int ssn9 = scan.nextInt();

  ssn.add(ssn1);
  ssn.add(ssn2);
  ssn.add(ssn3);
  ssn.add(ssn4);
  ssn.add(ssn5);
  ssn.add(ssn6);
  ssn.add(ssn7);
  ssn.add(ssn8);
  ssn.add(ssn9);

  Integer.toString(ssn);
  String Subssn = ssn.substring(5);

  System.out.println("The SSN is:" + ssn);
  System.out.println("The last four digits of the SSN are:" + Dummyssn + Dummyssn + Dummyssn + Dummyssn + Dummyssn + Subssn);
   }
}

There are several issues here that make it unclear exactly what you are trying to do. For one thing, you read in multiple int values (ssn1, ssn2...ssn9) then add them to a queue. You then call Integer.toString(ssn) . . . I would expect an error on that line. It seems like what you want to do there is use a loop to read the values back out of the queue, assign them to a temporary String variable each time, and then call the substring function on your temporary variable and print out the result. As it is right now, you are calling Integer.toString(ssn) which is on the entire queue and I don't think that will do what you want. The queue is still typed to Integer (Queue). Not to mention that the toString method returns a value which you are supposed to read into another variable or use at the time you call it. Calling it on its own line like that doesn't really achieve anything because even if it is returning some value, it is not being kept by your code for later use.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM