Overclock.net banner

Help in Java

970 Views 8 Replies 3 Participants Last post by  dangerousHobo
I was wondering, is there away to read an integer and then take that integer and pull it apart. Like take one int at a time from that longer integer.

I did it by reading it as a String and took one char at a time, but was wondering if it could be done the above way.
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);

System.out.print("Enter a positive integer greater than 10: ");
String user = keyboard.next();

int userLength = user.length();
//System.out.println(user);
//System.out.println(userLength);
char i=' ';
int total=0,r,numInPosition=1,numSum,userInteger=0;
for (r=0; r<=userLength-1; r++)
{
i=user.charAt(r);

switch (i)
{
case '1':
total=total+1;
numInPosition=1;
break;
case '2':
total=total+2;
numInPosition=2;
break;
case '3':
total=total+3;
numInPosition=3;
break;
case '4':
total=total+4;
numInPosition=4;
break;
case '5':
total=total+5;
numInPosition=5;
break;
case '6':
total=total+6;
numInPosition=6;
break;
case '7':
total=total+7;
numInPosition=7;
break;
case '8':
total=total+8;
numInPosition=8;
break;
case '9':
total=total+9;
numInPosition=9;
break;
}
int position = userLength-1-r, b=1;
for (int t=1; t<=position; t++)b=b*10;
numSum=numInPosition*b;
userInteger=userInteger+numSum;
b=1;
System.out.println(numSum+" "+userInteger);
}
See less See more
1 - 9 of 9 Posts
If it was me, I would do it by parsing it to a string and using Stringtokenizer to pull each part into an array String.length() big. Then you can just make a FOR loop to increment through the whole array and print out each individual character (or number, whatever). Dont really have any time to code atm but that should at least be a start. How new are you to java?
Thanks Killnine.
I'm pretty new to Java. I've done arrays before in other languages but not in java. I was going to use the scanner class and use nextInt(). but I just couldn't figure out another way to pull it apart and get each integer.
that's a really complicated program. Just do a FOR loop to grab each integer instead of grabbing each yourself.
Quote:


Originally Posted by Sideburns

that's a really complicated program. Just do a FOR loop to grab each integer instead of grabbing each yourself.

I'm not sure what you mean. I'm having the user input a number and then I want to pull that number apart and get every int from it. I want it so the user only enters the integer once and needs to hit enter once.
See less See more
anyone have ideas or help.
sideburns-could you explain your idea abit more
thanks
I found a different way to do it ussing the scanner class's .nextInt();

import java.util.Scanner;

public class Program5a {

/**
* @param args
*/
public static void main(String[] args) {
Scanner kb = new Scanner(System.in);
int userInt;
int total=0, m=0, t=10, num=1;
do {
System.out.print("Enter a positive integer greater than 10: ");
userInt = kb.nextInt();
}
while (userInt < 10);
int userInt2 = userInt;

//variable declaration
System.out.println(userInt+" "+userInt2);
while (userInt2 != 0)
{
if (m<userInt2)
{
if (num == 9){
num = 1;
t = t * 10;
System.out.println("**");
}
else
{
m = t * num;
num++;
System.out.println("*");
}
}
if ( m==userInt2)
{
total = total + num;
userInt2 = 0;
}
else
{
total =total + (num -1);
userInt2 = userInt2 - (num-1)*t;
num = 1;
m = 1;
t = 1;
}
}
System.out.print("Sum: "+total);
}

}

I have only one Problem. SOMEtimes I get stuck in an infinate loop . So I made it print out ** and * in two different loops and it will get stuck in the one that prints out the * non stop. so there must be a bug in that loop. But I can't find it. Any HELP PLEASE?
thank you all
See less See more
heres the .java file

May be easier to read
didn't work here it in .txt format
1 - 9 of 9 Posts
This is an older thread, you may not receive a response, and could be reviving an old thread. Please consider creating a new thread.
Top