473,412 Members | 2,050 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,412 software developers and data experts.

java problem

Hey guys, I hope somebody can help. I'm writing a program to reverse a number, and I'm getting an error code and can't figure out why. Hope someone can help. Thanks

import java.util.*;
class Assign4B
{
public static void main(String[]args)
{
int number;
int x;
int reversenumber;
Scanner input = new Scanner(System.in);
System.out.println("Please type a four-digit number for x");
number = input.nextInt();
number = x;
int ones;
int tens;
int hundreds;
int thousands;
thousands = x / 1000;
x -= thousands * 1000;
hundreds = x / 100;
x -= hundreds * 100;
tens = x / 10;
x -= tens * 10;
ones = x;
x = ones*1000 + tens*100 + hundreds*10 + thousands;
System.out.printf("Reverse:%8.2f",x);
}
}


out:variable x has not been initialized??
Feb 10 '08 #1
4 959
Your taking in a string and trying to make it backwards right?
try a simple for loop.
Let's pretend your input string is called input and it contains 1234.

tempInt = input.length(); //might not need the () can't remember atm.
string outPut = ""
for (int c = 0; c <= tempInt; tempInt--)
{
char tempChar = input.getChar(tempInt);
outPut += tempChar
}

then just printline your outPut string.... and you should get 4321.

Hopefully that helps
Feb 10 '08 #2
import java.util.*;
class Assign4B
{
public static void main(String[]args)
{
int number;
int x;
int reversenumber;
Scanner input = new Scanner(System.in);
System.out.println("Please type a four-digit number for x");
number = input.nextInt();
//number = x;..THE OTHER WAY ROUND!!
x= number;
int ones;
int tens;
int hundreds;
int thousands;
thousands = x / 1000;
x -= thousands * 1000;
hundreds = x / 100;
x -= hundreds * 100;
tens = x / 10;
x -= tens * 10;
ones = x;
x = ones*1000 + tens*100 + hundreds*10 + thousands;
//System.out.printf("Reverse:%8.2f",x);..INTERGER CAN"T BE FORMATED TO 2 DECIMAL PLACE
System.out.println(x);
}
}
Feb 10 '08 #3
JosAH
11,448 Expert 8TB
Your taking in a string and trying to make it backwards right?
try a simple for loop.
Let's pretend your input string is called input and it contains 1234.

tempInt = input.length(); //might not need the () can't remember atm.
string outPut = ""
for (int c = 0; c <= tempInt; tempInt--)
{
char tempChar = input.getChar(tempInt);
outPut += tempChar
}

then just printline your outPut string.... and you should get 4321.

Hopefully that helps
If you want to reverse a String (that might accidentally look like a number) please
let the core classes do the dirty work. Have a look at the StringBuilder.reverse()
method.

kind regards,

Jos
Feb 10 '08 #4
import java.util.*;
class Assign4B
{
public static void main(String[]args)
{
int number;
int x;
int reversenumber;
Scanner input = new Scanner(System.in);
System.out.println("Please type a four-digit number for x");
number = input.nextInt();
//number = x;..THE OTHER WAY ROUND!!
x= number;
int ones;
int tens;
int hundreds;
int thousands;
thousands = x / 1000;
x -= thousands * 1000;
hundreds = x / 100;
x -= hundreds * 100;
tens = x / 10;
x -= tens * 10;
ones = x;
x = ones*1000 + tens*100 + hundreds*10 + thousands;
//System.out.printf("Reverse:%8.2f",x);..INTERGER CAN"T BE FORMATED TO 2 DECIMAL PLACE
System.out.println(x);
}
}
Thanks alot for the help. It worked out fine.
Feb 10 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Steven Green | last post by:
I have a java app at work I used when I had Windows 98 and never had a problem. I did a clean install of Windows XP and of course Java was not included. I went to Sun, download Java 2 Runtime...
2
by: Patrick | last post by:
I'm using Jakarta-POI to create a huge Excel spreadsheet. I get the error below when the spreadsheet grows to a large size. It seems to have something to do with the number of "cell" objects that I...
13
by: BlackHawke | last post by:
Our program, game program Andromeda Online (www.andromedaonline.net) uses two programs- one to play the game, another to patch the game as updates come out. Players actually launch the updater...
133
by: Gaurav | last post by:
http://www.sys-con.com/story/print.cfm?storyid=45250 Any comments? Thanks Gaurav
2
by: Kent Lewandowski | last post by:
hi all, Recently I wrote some stored procedures using java jdbc code (admittedly my first stab) and then tried to implement the same within java packages (for code reuse). I encountered...
2
by: Michael | last post by:
Running DB2 v7 UDB ("DB2 v7.1.0.93", "n031208" and "WR21333") on Windows XP, I am unable to find out why the "Build for Debug" option within Stored Procedure Builder is not enabled on Java stored...
2
by: Tim Murray | last post by:
First of all, I don't know much about Java, even its naming and version numbering nomenclature, and second, if there is a better group to ask this in, please let me know. System is Mac with...
1
by: David Van D | last post by:
Hi there, A few weeks until I begin my journey towards a degree in Computer Science at Canterbury University in New Zealand, Anyway the course tutors are going to be teaching us JAVA wth bluej...
6
by: Rhino | last post by:
I'm trying to debug a simple Java UDF written in the DB2General style within Eclipse. I'm getting a java.lang.UnsatisfiedLinkError when I execute the set() method in the UDF. I know that the...
14
by: mlw | last post by:
Do not take anything about this, it is not a flame or troll, while I'm not new to Java I favor C++. However, I may need to use it in a contract position, and am concerned that the restrictions it...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.