New Posts  All Forums:Forum Nav:

help with java

post #1 of 30
Thread Starter 
just finish my first two programs for my homework, one more to go!

So for the assignment

3)Write a program that continually asks the user for a string. Check the string against the following set of criteria and output the required text. Your program should use methods we have explored in class.
a.If the string is empty (ie no text was entered) output “Empty Stringâ€.
b.If the string consists of the word “EXIT†or “exit†quit the calculator program.
c.If the string is a positive integer number (ie 0 or greater, no decimal place) output “Integer†and the number.
d.If the string is a positive decimal number (ie a double), output “Double†and the number.
e.Otherwise, output “Stringâ€, the string itself, and the length of the string.
f.The program should run continuously until the user quits.
Note: For this question, you cannot use a try-catch block and you cannot use the Scanner. Parse the string character by character to determine what type it is.


here is my code so far

import javax.swing.JOptionPane;

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

String word = JOptionPane.showInputDialog(null, "Enter a String!");
int wordFoInt = Integer.parseInt(word);
double wordFoDouble = Double.parseDouble(word);

if ( word.equals("") ) {
System.out.println("Empty String");
}else if ( word.equalsIgnoreCase("Exit")) {
System.out.println("shutting down");
System.exit(0);
}else if ( wordFoDouble >= 0.0 ) {
System.out.println("Double");
}else if ( wordFoInt >= 0 ) {
System.out.println("Integer");
}
}
}

i need help with the double, if i enter a 1.2 it doesn't output "double"
sig rig
(17 items)
 
  
CPUMotherboardGraphicsRAM
i5 2500k 4.4ghz ASRock P67 Extreme4 (B3) Gigabyte GTX 560 Ti SoC Patriot Viper DDR3 2x4gb 1600 MHZ CL9 
Hard DriveHard DriveHard DriveOS
WD 500GB BLACK Samsung 840 Maxtor 320 GB Windows 7 Home Prenium 64 bit 
MonitorMonitorKeyboardPower
ASUS VS247 Samsung BX2431 Razer BlackWidow OCZ STEALTHXSTREAM 2 600W 
CaseMouseMouse PadAudio
COOLERMASTER HAF 922 Logitech G9X SteelSeries QcK Creative X-Fi Titanium Fatality - Audio Techn... 
  hide details  
Reply
sig rig
(17 items)
 
  
CPUMotherboardGraphicsRAM
i5 2500k 4.4ghz ASRock P67 Extreme4 (B3) Gigabyte GTX 560 Ti SoC Patriot Viper DDR3 2x4gb 1600 MHZ CL9 
Hard DriveHard DriveHard DriveOS
WD 500GB BLACK Samsung 840 Maxtor 320 GB Windows 7 Home Prenium 64 bit 
MonitorMonitorKeyboardPower
ASUS VS247 Samsung BX2431 Razer BlackWidow OCZ STEALTHXSTREAM 2 600W 
CaseMouseMouse PadAudio
COOLERMASTER HAF 922 Logitech G9X SteelSeries QcK Creative X-Fi Titanium Fatality - Audio Techn... 
  hide details  
Reply
post #2 of 30
}else if ( wordFoDouble >= 0.01 )

try this
It will check if there is a few digit after the 0 other wise 0.0 means 0.
Right now it should in fact give you integer and not double.
Adding 0.01 will check if the number is not 0.

EDIT : thing is, if I enter 2, it will be >= 0.01.
Edited by Abs.exe - 10/25/11 at 1:48pm
post #3 of 30
Thread Starter 
0.001 doesn't work ;(

lol i'm hella confused right now -__-
sig rig
(17 items)
 
  
CPUMotherboardGraphicsRAM
i5 2500k 4.4ghz ASRock P67 Extreme4 (B3) Gigabyte GTX 560 Ti SoC Patriot Viper DDR3 2x4gb 1600 MHZ CL9 
Hard DriveHard DriveHard DriveOS
WD 500GB BLACK Samsung 840 Maxtor 320 GB Windows 7 Home Prenium 64 bit 
MonitorMonitorKeyboardPower
ASUS VS247 Samsung BX2431 Razer BlackWidow OCZ STEALTHXSTREAM 2 600W 
CaseMouseMouse PadAudio
COOLERMASTER HAF 922 Logitech G9X SteelSeries QcK Creative X-Fi Titanium Fatality - Audio Techn... 
  hide details  
Reply
sig rig
(17 items)
 
  
CPUMotherboardGraphicsRAM
i5 2500k 4.4ghz ASRock P67 Extreme4 (B3) Gigabyte GTX 560 Ti SoC Patriot Viper DDR3 2x4gb 1600 MHZ CL9 
Hard DriveHard DriveHard DriveOS
WD 500GB BLACK Samsung 840 Maxtor 320 GB Windows 7 Home Prenium 64 bit 
MonitorMonitorKeyboardPower
ASUS VS247 Samsung BX2431 Razer BlackWidow OCZ STEALTHXSTREAM 2 600W 
CaseMouseMouse PadAudio
COOLERMASTER HAF 922 Logitech G9X SteelSeries QcK Creative X-Fi Titanium Fatality - Audio Techn... 
  hide details  
Reply
post #4 of 30
Instead of checking if its greater than a certain value just check for a decimal in your string.

Edit:
Though I suppose that wouldnt work if someone put puncutation at the end of a sentance :/
My System
(13 items)
 
  
CPUMotherboardGraphicsRAM
AMD Phenom II x4 955 ASUS Crosshair 3 EVGA 465GTX 1GB 8GB DDR3 1600mhz GSkill/ADATA 
Hard DriveOSMonitorKeyboard
40GB Intel SSD 1TB Western Digital Black Edition Windows 7 x64 Pro Dual 21.5 Sceptre Saitek Eclipse 
PowerCaseMouseMouse Pad
850watt Silverstone Element Coolermaster Haf 932 Razer Lachesis Razer Vespula 
  hide details  
Reply
My System
(13 items)
 
  
CPUMotherboardGraphicsRAM
AMD Phenom II x4 955 ASUS Crosshair 3 EVGA 465GTX 1GB 8GB DDR3 1600mhz GSkill/ADATA 
Hard DriveOSMonitorKeyboard
40GB Intel SSD 1TB Western Digital Black Edition Windows 7 x64 Pro Dual 21.5 Sceptre Saitek Eclipse 
PowerCaseMouseMouse Pad
850watt Silverstone Element Coolermaster Haf 932 Razer Lachesis Razer Vespula 
  hide details  
Reply
post #5 of 30
How about using compareTo instead?



wordFoDouble.compareTo(0.0) >= 0
Once again...
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7 920 [4.28GHz, HT] Asus P6T + Broadcom NetXtreme II VisionTek HD5850 [900/1200] + Galaxy GT240 2x4GB G.Skill Ripjaw X [1632 MHz] 
Hard DriveOSMonitorKeyboard
Intel X25-M 160GB + 3xRAID0 500GB 7200.12 Window 7 Pro 64 Acer H243H + Samsung 226BW XARMOR-U9BL  
PowerCaseMouseMouse Pad
Antec Truepower New 750W Li Lian PC-V2100 [10x120mm fans] Logitech G9 X-Trac Pro 
  hide details  
Reply
Once again...
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7 920 [4.28GHz, HT] Asus P6T + Broadcom NetXtreme II VisionTek HD5850 [900/1200] + Galaxy GT240 2x4GB G.Skill Ripjaw X [1632 MHz] 
Hard DriveOSMonitorKeyboard
Intel X25-M 160GB + 3xRAID0 500GB 7200.12 Window 7 Pro 64 Acer H243H + Samsung 226BW XARMOR-U9BL  
PowerCaseMouseMouse Pad
Antec Truepower New 750W Li Lian PC-V2100 [10x120mm fans] Logitech G9 X-Trac Pro 
  hide details  
Reply
post #6 of 30
One way : trim your string in an array with , as trim char.
then for each char in array
if char == ","
type = "double"

V2
trim your string in an array with , as trim char.
int lenght = string.Lenght
int pos
for each char in array
if char == ","
pos = char.GetPosition
if array[pos+1] == 0
if array[pos+2] == 0
type = "integer"
end if
else
type = "double"

sorry it's messy but you can still see the point, have your array = theString.Trim(",")
then shuffle throught it to check the , location, then check if after that , there are 0's.
If there is 0 and 0, it's an integer (ie . 1,00)
Else if it isnt 0, it's something like 1.11

I think it's the best way to get the job done !
Edited by Abs.exe - 10/25/11 at 2:00pm
post #7 of 30
Thread Starter 
Quote:
Originally Posted by DuckieHo View Post
How about using compareTo instead?



wordFoDouble.compareTo(0.0) >= 0
tried that

"double cannot be dereference"
sig rig
(17 items)
 
  
CPUMotherboardGraphicsRAM
i5 2500k 4.4ghz ASRock P67 Extreme4 (B3) Gigabyte GTX 560 Ti SoC Patriot Viper DDR3 2x4gb 1600 MHZ CL9 
Hard DriveHard DriveHard DriveOS
WD 500GB BLACK Samsung 840 Maxtor 320 GB Windows 7 Home Prenium 64 bit 
MonitorMonitorKeyboardPower
ASUS VS247 Samsung BX2431 Razer BlackWidow OCZ STEALTHXSTREAM 2 600W 
CaseMouseMouse PadAudio
COOLERMASTER HAF 922 Logitech G9X SteelSeries QcK Creative X-Fi Titanium Fatality - Audio Techn... 
  hide details  
Reply
sig rig
(17 items)
 
  
CPUMotherboardGraphicsRAM
i5 2500k 4.4ghz ASRock P67 Extreme4 (B3) Gigabyte GTX 560 Ti SoC Patriot Viper DDR3 2x4gb 1600 MHZ CL9 
Hard DriveHard DriveHard DriveOS
WD 500GB BLACK Samsung 840 Maxtor 320 GB Windows 7 Home Prenium 64 bit 
MonitorMonitorKeyboardPower
ASUS VS247 Samsung BX2431 Razer BlackWidow OCZ STEALTHXSTREAM 2 600W 
CaseMouseMouse PadAudio
COOLERMASTER HAF 922 Logitech G9X SteelSeries QcK Creative X-Fi Titanium Fatality - Audio Techn... 
  hide details  
Reply
post #8 of 30
for the double do:

if(word.indexOf('.')) != -1){
System.out.println("Double");
}

That should fix it
post #9 of 30
Thread Starter 
lol god damn, I'm a newbie! I don't know what to do for this program, guess i'll do part b for the first two programs i finished. I'll have to tackle this program later. Thanks tho..
sig rig
(17 items)
 
  
CPUMotherboardGraphicsRAM
i5 2500k 4.4ghz ASRock P67 Extreme4 (B3) Gigabyte GTX 560 Ti SoC Patriot Viper DDR3 2x4gb 1600 MHZ CL9 
Hard DriveHard DriveHard DriveOS
WD 500GB BLACK Samsung 840 Maxtor 320 GB Windows 7 Home Prenium 64 bit 
MonitorMonitorKeyboardPower
ASUS VS247 Samsung BX2431 Razer BlackWidow OCZ STEALTHXSTREAM 2 600W 
CaseMouseMouse PadAudio
COOLERMASTER HAF 922 Logitech G9X SteelSeries QcK Creative X-Fi Titanium Fatality - Audio Techn... 
  hide details  
Reply
sig rig
(17 items)
 
  
CPUMotherboardGraphicsRAM
i5 2500k 4.4ghz ASRock P67 Extreme4 (B3) Gigabyte GTX 560 Ti SoC Patriot Viper DDR3 2x4gb 1600 MHZ CL9 
Hard DriveHard DriveHard DriveOS
WD 500GB BLACK Samsung 840 Maxtor 320 GB Windows 7 Home Prenium 64 bit 
MonitorMonitorKeyboardPower
ASUS VS247 Samsung BX2431 Razer BlackWidow OCZ STEALTHXSTREAM 2 600W 
CaseMouseMouse PadAudio
COOLERMASTER HAF 922 Logitech G9X SteelSeries QcK Creative X-Fi Titanium Fatality - Audio Techn... 
  hide details  
Reply
post #10 of 30
I suggested that solution earlier Jtvd but the problem is if someone wsa to type in a sentance and finish it with a period for punctuation that would evaluate as a double. What he could do (if they learned about try/catches) is try to parse the integer if it throws an error try to parse the double if the double parses its a double print out double if both throw errors we know its not a number
My System
(13 items)
 
  
CPUMotherboardGraphicsRAM
AMD Phenom II x4 955 ASUS Crosshair 3 EVGA 465GTX 1GB 8GB DDR3 1600mhz GSkill/ADATA 
Hard DriveOSMonitorKeyboard
40GB Intel SSD 1TB Western Digital Black Edition Windows 7 x64 Pro Dual 21.5 Sceptre Saitek Eclipse 
PowerCaseMouseMouse Pad
850watt Silverstone Element Coolermaster Haf 932 Razer Lachesis Razer Vespula 
  hide details  
Reply
My System
(13 items)
 
  
CPUMotherboardGraphicsRAM
AMD Phenom II x4 955 ASUS Crosshair 3 EVGA 465GTX 1GB 8GB DDR3 1600mhz GSkill/ADATA 
Hard DriveOSMonitorKeyboard
40GB Intel SSD 1TB Western Digital Black Edition Windows 7 x64 Pro Dual 21.5 Sceptre Saitek Eclipse 
PowerCaseMouseMouse Pad
850watt Silverstone Element Coolermaster Haf 932 Razer Lachesis Razer Vespula 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming