Turning a 9 digit user input into integers for calculations.

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 1170 Views - Last Post: 08 October 2012 - 07:17 PM Rate Topic: -----

#1 thorax232  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 94
  • Joined: 10-September 12

Turning a 9 digit user input into integers for calculations.

Posted 03 October 2012 - 06:04 PM

Ok, so I was able to successfully solve this problem with three digits by using the code:
int lotteryTemp = lottery;
int lotteryOneDigit = lottery1 % 10;
lotteryTemp = lotteryTemp / 10;
...


And so on with lottery2 and lottery3.

However, I'm having trouble with doing it with 9 digits. Here's the integer code I have, I'll post the whole program at the bottom. I'm able to successfully compile and run but I only get 2 digits for an output. =/ Not the 10 I'm hoping for.
int temp = isbn;
int d9 = temp % 10;
temp = temp / 10;
int d8 = temp % 10;
temp = temp / 10;
int d7 = temp % 10;
temp = temp / 10;
int d6 = temp % 10;
temp = temp / 10;
int d5 = temp % 10;
temp = temp / 10;
int d4 = temp % 10;
temp = temp / 10;
int d3 = temp % 10;
temp = temp / 10;
int d2 = temp % 10;
temp = temp / 10;
int d1 = temp % 10;
temp = temp / 10;



FULL Program (simpler parts left out)
//Have user enter 9 digit number ie. 123456789
int isbn = input.nextInt();

(INSERT ABOVE CODE HERE)

//Calculate 10th digit
int d10 = ((d1*1+d2*2+d3*3+d4*4+d5*5+d6*6+d7*7+d8*8+d9*9) % 11);

//If 10th digit = 10, use X
if (d10 == 10)
System.out.println(d1+d2+d3+d4+d5+d6+d7+d8+d9+"X");

//Else print all 10 digits ie. 1234567890
else
System.out.println(d1+d2+d3+d4+d5+d6+d7+d8+d9+d10);



=/ What am I doing to only get 2 digits on the output? Thanks for your patience and time. :)

Is This A Good Question/Topic? 0
  • +

Replies To: Turning a 9 digit user input into integers for calculations.

#2 fromTheSprawl  Icon User is offline

  • Monomania
  • member icon

Reputation: 513
  • View blog
  • Posts: 2,056
  • Joined: 28-December 10

Re: Turning a 9 digit user input into integers for calculations.

Posted 03 October 2012 - 06:08 PM

I'm not really sure how your program works, how about posting an example input and output and what's it supposed to do? Also, you could simplify all this with an array.
Was This Post Helpful? 1
  • +
  • -

#3 thorax232  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 94
  • Joined: 10-September 12

Re: Turning a 9 digit user input into integers for calculations.

Posted 03 October 2012 - 06:18 PM

A note on arrays, we haven't gone over those yet. =/ So I don't think I can use them.

Here's 2 output examples though.
Enter the first 9 digits of an ISBN as integer: 013601267
30
Enter the first 9 digits of an ISBN as integer: 013031997
41



I should be getting:
Enter the first 9 digits of an ISBN as integer: 013601267
0136012671
Enter the first 9 digits of an ISBN as integer: 013031997
013031997X



Basically it just plugs the 9 digits into the formula to calculate the 10th digit and prints it all out as one string of digits.

If you need full unedited code I can print that too. Thanks for quick response. :) This has been bothering me all night.
Was This Post Helpful? 0
  • +
  • -

#4 DoinWork  Icon User is offline

  • D.I.C Head

Reputation: 15
  • View blog
  • Posts: 69
  • Joined: 26-October 11

Re: Turning a 9 digit user input into integers for calculations.

Posted 03 October 2012 - 06:22 PM

Maybe it would be easier to read it in as a string then parse it back to a number that way instead of all the computations up front. Could just read in "012345678", run a loop to grab last(indexed at 0), add it to total, grab next x 10 and so forth.
Was This Post Helpful? 1
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Turning a 9 digit user input into integers for calculations.

Posted 03 October 2012 - 06:38 PM

Read them as a String (Scannner.nextLine())
convert your String to a char[] (String.toCharArray())

remove '0' to all digit representing a number
Was This Post Helpful? 1
  • +
  • -

#6 thorax232  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 94
  • Joined: 10-September 12

Re: Turning a 9 digit user input into integers for calculations.

Posted 03 October 2012 - 06:39 PM

lol, we haven't done loops either. However, in my powerpoint printout I see we're going over them in the next few days. Mind dropping a hint? What kind of loop would it be? :D
Was This Post Helpful? -1
  • +
  • -

#7 fromTheSprawl  Icon User is offline

  • Monomania
  • member icon

Reputation: 513
  • View blog
  • Posts: 2,056
  • Joined: 28-December 10

Re: Turning a 9 digit user input into integers for calculations.

Posted 03 October 2012 - 06:41 PM

Use a for loop. Come on dude, show some initiative to your teacher. ^^
Was This Post Helpful? 3
  • +
  • -

#8 thorax232  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 94
  • Joined: 10-September 12

Re: Turning a 9 digit user input into integers for calculations.

Posted 03 October 2012 - 07:03 PM

Looks like I'll be using a for loop. Thanks fromTheSprawl and everyone else. I'll get to work and post back when I figure it out.
Was This Post Helpful? -3
  • +
  • -

#9 rfs02  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 26
  • View blog
  • Posts: 70
  • Joined: 30-September 12

Re: Turning a 9 digit user input into integers for calculations.

Posted 03 October 2012 - 09:26 PM

You were actually very close with your original posting. The problem you were having was with the lines that are actually printing to the screen:

System.out.println(d1+d2+d3+d4+d5+d6+d7+d8+d9+d10);


What this is doing is asking the java runtime to add the numbers and then print the sum, which is why you were getting a 2 digit number (same for the other printing line).

In order to force Java to do what you are looking for (print every digit separately) - add a "" to print first:

System.out.println(""+d1+d2+d3+d4+d5+d6+d7+d8+d9+d10);

Was This Post Helpful? 0
  • +
  • -

#10 thorax232  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 94
  • Joined: 10-September 12

Re: Turning a 9 digit user input into integers for calculations.

Posted 04 October 2012 - 08:42 AM

View Postrfs02, on 03 October 2012 - 09:26 PM, said:

In order to force Java to do what you are looking for (print every digit separately) - add a "" to print first:

System.out.println(""+d1+d2+d3+d4+d5+d6+d7+d8+d9+d10);


Ok, that makes sense, I'm giving it a math problem instead of printing out the digits.
With your suggestion I get these results:
Enter 9 digits: 013601267
01360121070
Enter 9 digits: 013031997
01303191007


So still not there. I get the same problem with.
System.out.println(isbn+"X");
System.out.println(isbn+d10);



It really seems like the isbn should work, at least with the X because it is only declared once when the user inputs it. =/
Was This Post Helpful? 0
  • +
  • -

#11 rfs02  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 26
  • View blog
  • Posts: 70
  • Joined: 30-September 12

Re: Turning a 9 digit user input into integers for calculations.

Posted 04 October 2012 - 08:48 AM

I don't see isbn in your original post. Is isbn an integer or a string? and what did you assign to it?
Was This Post Helpful? 0
  • +
  • -

#12 thorax232  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 94
  • Joined: 10-September 12

Re: Turning a 9 digit user input into integers for calculations.

Posted 04 October 2012 - 09:30 AM

View Postrfs02, on 04 October 2012 - 08:48 AM, said:

I don't see isbn in your original post. Is isbn an integer or a string? and what did you assign to it?


ISBN is the user input. Ex. 013601267 or 013031997

Ok here's the full unedited code with the different options I've tried:
import java.util.Scanner;
public class Prog13 {
public static void main (String[] args) {

	//Ask for variables
	System.out.println("Enter the first 9 digits of an ISBN integer: ");
	Scanner input = new Scanner(System.in);
		int isbn = input.nextInt();
		
	//Get digits
		int temp = isbn;
		int d9 = temp % 10;
		temp = temp / 10;
		int d8 = temp % 10;
		temp = temp / 10;
		int d7 = temp % 10;
		temp = temp / 10;
		int d6 = temp % 10;
		temp = temp / 10;
		int d5 = temp % 10;
		temp = temp / 10;
		int d4 = temp % 10;
		temp = temp / 10;
		int d3 = temp % 10;
		temp = temp / 10;
		int d2 = temp % 10;
		temp = temp / 10;
		int d1 = temp % 10;
	
	//Calculate 10 digit
		int d10 = ((d1*1+d2*2+d3*3+d4*4+d5*5+
		d6*6+d7*7+d8*8+d9*9) % 11);
		
	//If else
	if (d10 == 10)
		System.out.println(d1+d2+d3+d4+d5+d6+d7+d8+d9+"X");
        else
		System.out.println(d1+d2+d3+d4+d5+d6+d7+d8+d9+d10);
}}


Variants of //If else
Tried 1:
System.out.println(""+d1+d2+d3+d4+d5+d6+d7+d8+d9+"X");
System.out.println(""+d1+d2+d3+d4+d5+d6+d7+d8+d9+d10);

Tried 2:
System.out.println(isbn+"X");
Ststem.out.println(isbn+d10);


This post has been edited by thorax232: 04 October 2012 - 09:31 AM

Was This Post Helpful? 0
  • +
  • -

#13 rfs02  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 26
  • View blog
  • Posts: 70
  • Joined: 30-September 12

Re: Turning a 9 digit user input into integers for calculations.

Posted 04 October 2012 - 11:34 AM

If you think about it, you have the same issue you had with the original println.

If d10 is equal to 10, you will print isbn + "X" - in this case, you will print the right number as long as the first digit is not 0 (because it has no significance in an int).

If d10 is not equal to 10, you add d10 to isbn and then print the sum.

In all cases, if you want to force Java to convert all numbers to strings and then concatenate them, you can add a "" at the beginning. This forces the compiler to use the + operator for a string.
Was This Post Helpful? 0
  • +
  • -

#14 thorax232  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 94
  • Joined: 10-September 12

Re: Turning a 9 digit user input into integers for calculations.

Posted 08 October 2012 - 03:25 PM

Solved: http://pastebin.com/cMsJfBtX

It was the /10 > %10 that was creating wrong solutions, everything else was good from the beginning. >.< I just had to use a calculator to go through and find out how to find each integer. It's a little messy and could probably look better but it works. :D
Was This Post Helpful? 0
  • +
  • -

#15 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Turning a 9 digit user input into integers for calculations.

Posted 08 October 2012 - 03:55 PM

You will definitively have to learn how to use arrays before doing that type of thing
You have already wasted a lot of our time that can be dedicated to more serious problem
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2