• Welcome to TechPowerUp Forums, Guest! Please check out our forum guidelines for info related to our community.

Rhino's "I need Java help" thread

Easy Rhino

Linux Advocate
Staff member
Joined
Nov 13, 2006
Messages
15,643 (2.35/day)
Location
Mid-Atlantic
System Name Desktop
Processor i5 13600KF
Motherboard AsRock B760M Steel Legend Wifi
Cooling Noctua NH-U9S
Memory 4x 16 Gb Gskill S5 DDR5 @6000
Video Card(s) Gigabyte Gaming OC 6750 XT 12GB
Storage WD_BLACK 4TB SN850x
Display(s) Gigabye M32U
Case Corsair Carbide 400C
Audio Device(s) On Board
Power Supply EVGA Supernova 650 P2
Mouse MX Master 3s
Keyboard Logitech G915 Wireless Clicky
Software Fedora KDE Spin
got a new small program that i finished but im having decimal issues. essentially it keeps cutting off the last "0" in the digits even tho i have DecimalFormat set to "0.0" and it is keeping the last counting to the thousandths place...
Code:
import java.io.*;
import java.text.DecimalFormat;

public class exam1 
{
	private BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
	
	DecimalFormat oneDecimal = new DecimalFormat("0.0");
	
	private String input_string;
	private double purchase_order, total_charge, initial_charge, bags_purchased, discount;
	
	public void user_interface() throws IOException
	{
		System.out.println("How many bags of delicious coffee would you like to purchase?");
		input_string = keyboard.readLine();
		bags_purchased = Double.parseDouble(input_string);
		
			if(bags_purchased < 25)
			{
				initial_charge = bags_purchased * 5.50;
				discount = initial_charge * 0.00;
				total_charge = initial_charge - discount;
			}
			if(bags_purchased >= 25)
			{
				initial_charge = bags_purchased * 5.50;
				discount = initial_charge * 0.05;
				total_charge = initial_charge - discount;
			}
			if(bags_purchased >= 50)
			{
				initial_charge = bags_purchased * 5.50;
				discount = initial_charge * 0.10;
				total_charge = initial_charge - discount;
			}
			if(bags_purchased >= 100)
			{
				initial_charge = bags_purchased * 5.50;
				discount = initial_charge * 0.15;
				total_charge = initial_charge - discount;
			}
			if(bags_purchased >= 150)
			{
				initial_charge = bags_purchased * 5.50;
				discount = initial_charge * 0.20;
				total_charge = initial_charge - discount;
			}
			if(bags_purchased >= 200)
			{
				initial_charge = bags_purchased * 5.50;
				discount = initial_charge * 0.25;
				total_charge = initial_charge - discount;
			}
			if(bags_purchased >= 300)
			{
				initial_charge = bags_purchased * 5.50;
				discount = initial_charge * 0.30;
				total_charge = initial_charge - discount;
			}
		System.out.println("Number of Bags Ordered: " + bags_purchased + " - $" + initial_charge);
		System.out.println("Discount: $" + discount ++);
		System.out.println("Your total charge is: $" + total_charge + "");
	}
	public static void main(String[] args) throws IOException
	{
		exam1 x = new exam1();
			x.user_interface();
	}
}
 
T

twilyth

Guest
I'm just learning java, but don't you have to recast it or something? When you go to print a double, what is the default conversion? I'm guessing int maybe?
 

FordGT90Concept

"I go fast!1!11!1!"
Joined
Oct 13, 2008
Messages
26,263 (4.41/day)
Location
IA, USA
System Name BY-2021
Processor AMD Ryzen 7 5800X (65w eco profile)
Motherboard MSI B550 Gaming Plus
Cooling Scythe Mugen (rev 5)
Memory 2 x Kingston HyperX DDR4-3200 32 GiB
Video Card(s) AMD Radeon RX 7900 XT
Storage Samsung 980 Pro, Seagate Exos X20 TB 7200 RPM
Display(s) Nixeus NX-EDG274K (3840x2160@144 DP) + Samsung SyncMaster 906BW (1440x900@60 HDMI-DVI)
Case Coolermaster HAF 932 w/ USB 3.0 5.25" bay + USB 3.2 (A+C) 3.5" bay
Audio Device(s) Realtek ALC1150, Micca OriGen+
Power Supply Enermax Platimax 850w
Mouse Nixeus REVEL-X
Keyboard Tesoro Excalibur
Software Windows 10 Home 64-bit
Benchmark Scores Faster than the tortoise; slower than the hare.
You got to use oneDecimal.format(value) to make use of that formatter. You are defining, but not using it.

e.g.
Code:
		System.out.println("Number of Bags Ordered: " + bags_purchased + " - $" + oneDecimal.format(initial_charge));
		System.out.println("Discount: $" + oneDecimal.format(discount) ++);
		System.out.println("Your total charge is: $" + oneDecimal.format(total_charge) + "");

If you don't use a formatter, it will return the default string for the type.
 
Last edited:

Easy Rhino

Linux Advocate
Staff member
Joined
Nov 13, 2006
Messages
15,643 (2.35/day)
Location
Mid-Atlantic
System Name Desktop
Processor i5 13600KF
Motherboard AsRock B760M Steel Legend Wifi
Cooling Noctua NH-U9S
Memory 4x 16 Gb Gskill S5 DDR5 @6000
Video Card(s) Gigabyte Gaming OC 6750 XT 12GB
Storage WD_BLACK 4TB SN850x
Display(s) Gigabye M32U
Case Corsair Carbide 400C
Audio Device(s) On Board
Power Supply EVGA Supernova 650 P2
Mouse MX Master 3s
Keyboard Logitech G915 Wireless Clicky
Software Fedora KDE Spin
im attempting to use arrays...

essentially i need to enter info for three separate fields into an array and have the program spit the data i entered back out to me. i can enter the data but it gives me an exception when it tries to print it... :banghead: here is what i have so far. it is in 3 diff classes...

Code:
import java.util.StringTokenizer;

public class Employee 
{
	String employeeName, employeeSalary, employeeDepartment;
	String employee3;
	String[] employeeArray = new String[3];
	int subScript = 0;
	
	public void splitUpEmployee(String employee1)
	{
		StringTokenizer st = new StringTokenizer(employee1);
		employeeName = st.nextToken();
		employeeSalary = st.nextToken();
		employeeDepartment = st.nextToken();
	}
	
	public void addToArray(String employee2)
	{
		employeeArray[subScript] = employee2;
		subScript++;
	}
	
	public void extractFromArray()
	{
		employee3 = employeeArray[subScript];
		splitUpEmployee(employee3);
		subScript++;
	}
	
	public void initializeCounter()
	{
		subScript = 0;
	}
}

Code:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Manager extends Employee
{
	private int n;
	private String inputString;
	private final BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
	
	public void user_interface() throws IOException
	{
		n = 0;
		while (n < 1)
		{
			System.out.println("Enter employee name: ");
			inputString = keyboard.readLine();
			addToArray(inputString);
			System.out.println("Enter employee department: ");
			inputString = keyboard.readLine();
			addToArray(inputString);
			System.out.println("Enter employee salary: ");
			inputString = keyboard.readLine();
			addToArray(inputString);
			n++;
		}
	}
	
	public void outputData() throws IOException
	{
		n = 0;
		subScript = 0;
		while (n < 1)
		{
			extractFromArray();
			System.out.println("Employee Name: " + employeeName);
			System.out.println("Employee Department: " + employeeDepartment);
			System.out.println("Employee Salary: " + employeeSalary);
			n++;
		}
	}

}

Code:
import java.io.*;

public class testproject3 
{
	public static void main(String[] args) throws IOException
	{
		Manager x = new Manager();
		x.user_interface();
		x.outputData();
	}

}
 

FordGT90Concept

"I go fast!1!11!1!"
Joined
Oct 13, 2008
Messages
26,263 (4.41/day)
Location
IA, USA
System Name BY-2021
Processor AMD Ryzen 7 5800X (65w eco profile)
Motherboard MSI B550 Gaming Plus
Cooling Scythe Mugen (rev 5)
Memory 2 x Kingston HyperX DDR4-3200 32 GiB
Video Card(s) AMD Radeon RX 7900 XT
Storage Samsung 980 Pro, Seagate Exos X20 TB 7200 RPM
Display(s) Nixeus NX-EDG274K (3840x2160@144 DP) + Samsung SyncMaster 906BW (1440x900@60 HDMI-DVI)
Case Coolermaster HAF 932 w/ USB 3.0 5.25" bay + USB 3.2 (A+C) 3.5" bay
Audio Device(s) Realtek ALC1150, Micca OriGen+
Power Supply Enermax Platimax 850w
Mouse Nixeus REVEL-X
Keyboard Tesoro Excalibur
Software Windows 10 Home 64-bit
Benchmark Scores Faster than the tortoise; slower than the hare.
What's the exception?
 

Easy Rhino

Linux Advocate
Staff member
Joined
Nov 13, 2006
Messages
15,643 (2.35/day)
Location
Mid-Atlantic
System Name Desktop
Processor i5 13600KF
Motherboard AsRock B760M Steel Legend Wifi
Cooling Noctua NH-U9S
Memory 4x 16 Gb Gskill S5 DDR5 @6000
Video Card(s) Gigabyte Gaming OC 6750 XT 12GB
Storage WD_BLACK 4TB SN850x
Display(s) Gigabye M32U
Case Corsair Carbide 400C
Audio Device(s) On Board
Power Supply EVGA Supernova 650 P2
Mouse MX Master 3s
Keyboard Logitech G915 Wireless Clicky
Software Fedora KDE Spin
ok i changed some code around. when i enter employee info i enter the name<space>department<space>and salary and the next.Token() essentially breaks that down into 3 fields using the space as the delimiter. It spits back
name: name i entered
department: null
salary: department i entered

part of the assignment is to put the department field in a different class to practice using "extends." so i need to get the program to assign department correctly. here is what i have.

Code:
import java.util.StringTokenizer;

public class Employee 
{
	String employeeName, employeeSalary;
	String employee3;
	String[] employeeArray = new String[3];
	int subScript = 0;
	
	public void splitUpEmployee(String employee1)
	{
		StringTokenizer st = new StringTokenizer(employee1);
		employeeName = st.nextToken();
		employeeSalary = st.nextToken();
	}
	
	public void addToArray(String employee2)
	{
		employeeArray[subScript] = employee2;
		subScript++;
	}
	
	public void extractFromArray()
	{
		employee3 = employeeArray[subScript];
		splitUpEmployee(employee3);
		subScript++;
	}
	
	public void initializeCounter()
	{
		subScript = 0;
	}
}

Code:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Manager extends Employee
{
	private int n;
	public String employeeDepartment;
	private String inputString;
	private final BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
	
	public void addDepartment(String employee1)
	{
		StringTokenizer st = new StringTokenizer(employee1);
		employeeDepartment = st.nextToken();
	}
	
	public void user_interface() throws IOException
	{
		n = 0;
		while (n < 1)
		{
			System.out.println("Enter employee name, department and salary: ");
			inputString = keyboard.readLine();
			addToArray(inputString);
			n++;
		}
	}
	
	public void outputData() throws IOException
	{
		n = 0;
		subScript = 0;
		while (n < 1)
		{
			extractFromArray();
			System.out.println("Employee Name: " + employeeName);
			System.out.println("Employee Department: " + employeeDepartment);
			System.out.println("Employee Salary: " + employeeSalary);
			n++;
		}
	}

}

Code:
import java.io.*;

public class testproject3 
{
	public static void main(String[] args) throws IOException
	{
		Manager x = new Manager();
		x.user_interface();
		x.outputData();
	}

}
 

FordGT90Concept

"I go fast!1!11!1!"
Joined
Oct 13, 2008
Messages
26,263 (4.41/day)
Location
IA, USA
System Name BY-2021
Processor AMD Ryzen 7 5800X (65w eco profile)
Motherboard MSI B550 Gaming Plus
Cooling Scythe Mugen (rev 5)
Memory 2 x Kingston HyperX DDR4-3200 32 GiB
Video Card(s) AMD Radeon RX 7900 XT
Storage Samsung 980 Pro, Seagate Exos X20 TB 7200 RPM
Display(s) Nixeus NX-EDG274K (3840x2160@144 DP) + Samsung SyncMaster 906BW (1440x900@60 HDMI-DVI)
Case Coolermaster HAF 932 w/ USB 3.0 5.25" bay + USB 3.2 (A+C) 3.5" bay
Audio Device(s) Realtek ALC1150, Micca OriGen+
Power Supply Enermax Platimax 850w
Mouse Nixeus REVEL-X
Keyboard Tesoro Excalibur
Software Windows 10 Home 64-bit
Benchmark Scores Faster than the tortoise; slower than the hare.
So, what's wrong with it now?

Also, I'd recommend using some other character as a delimitter. Space is common. I like using tlide (~) or the bar (|) and make sure those chars are rejected as input.
 

Easy Rhino

Linux Advocate
Staff member
Joined
Nov 13, 2006
Messages
15,643 (2.35/day)
Location
Mid-Atlantic
System Name Desktop
Processor i5 13600KF
Motherboard AsRock B760M Steel Legend Wifi
Cooling Noctua NH-U9S
Memory 4x 16 Gb Gskill S5 DDR5 @6000
Video Card(s) Gigabyte Gaming OC 6750 XT 12GB
Storage WD_BLACK 4TB SN850x
Display(s) Gigabye M32U
Case Corsair Carbide 400C
Audio Device(s) On Board
Power Supply EVGA Supernova 650 P2
Mouse MX Master 3s
Keyboard Logitech G915 Wireless Clicky
Software Fedora KDE Spin
So, what's wrong with it now?

Also, I'd recommend using some other character as a delimitter. Space is common. I like using tlide (~) or the bar (|) and make sure those chars are rejected as input.

it returns null for the department field. im guessing it has something to do with how i have the department field setup. i tried to write it so that the department field is parto of the EmployeeArray. obviously i am missing something.
 

FordGT90Concept

"I go fast!1!11!1!"
Joined
Oct 13, 2008
Messages
26,263 (4.41/day)
Location
IA, USA
System Name BY-2021
Processor AMD Ryzen 7 5800X (65w eco profile)
Motherboard MSI B550 Gaming Plus
Cooling Scythe Mugen (rev 5)
Memory 2 x Kingston HyperX DDR4-3200 32 GiB
Video Card(s) AMD Radeon RX 7900 XT
Storage Samsung 980 Pro, Seagate Exos X20 TB 7200 RPM
Display(s) Nixeus NX-EDG274K (3840x2160@144 DP) + Samsung SyncMaster 906BW (1440x900@60 HDMI-DVI)
Case Coolermaster HAF 932 w/ USB 3.0 5.25" bay + USB 3.2 (A+C) 3.5" bay
Audio Device(s) Realtek ALC1150, Micca OriGen+
Power Supply Enermax Platimax 850w
Mouse Nixeus REVEL-X
Keyboard Tesoro Excalibur
Software Windows 10 Home 64-bit
Benchmark Scores Faster than the tortoise; slower than the hare.
Code:
		n = 0;
		while (n < 1)
		{
			System.out.println("Enter employee name, department and salary: ");
			inputString = keyboard.readLine();
			addToArray(inputString);
			n++;
		}
That while loop will only execute once so your array will only ever have an item at index 0.

If you are trying to populate an array (which you are), you should always use Array.getLength() so that the indexor will never exceed the number of indexes in the array.


FYI: Name, Department, and salary should really be properties of the Employee class, not an array. A good candidate for an array, for example, is the lines of an address. Name should be two strings (first and last), Department should be an enumerator, and Salary should be a double or float.



Example of what I think you are trying to do:
Code:
	public static String[] user_interface() throws IOException
	{
		System.out.println("New Manager...");
		String[] output = new String[] = { "Name", "Department", "Salary" };
		for (int i = 0; i < output.getLength(); i++)
		{
			System.out.println("Enter " + output[i] + ": ");
			output[i] = keyboard.readLine();
		}
		return output;
	}
Most likely in your Main method, you would do this:
Code:
Manager e = new Manager(Manager.user_interface());
That would prompt for the information and create a new instance of Employee. The Employee constructor would accept an array of strings and break it out into its respective parts (make sure the array is the correct length and throw an exception if it is not).
 
Last edited:

Easy Rhino

Linux Advocate
Staff member
Joined
Nov 13, 2006
Messages
15,643 (2.35/day)
Location
Mid-Atlantic
System Name Desktop
Processor i5 13600KF
Motherboard AsRock B760M Steel Legend Wifi
Cooling Noctua NH-U9S
Memory 4x 16 Gb Gskill S5 DDR5 @6000
Video Card(s) Gigabyte Gaming OC 6750 XT 12GB
Storage WD_BLACK 4TB SN850x
Display(s) Gigabye M32U
Case Corsair Carbide 400C
Audio Device(s) On Board
Power Supply EVGA Supernova 650 P2
Mouse MX Master 3s
Keyboard Logitech G915 Wireless Clicky
Software Fedora KDE Spin
i dont know. im completely lost now :cry: my prof didn't do a very good job of providing examples for this project. im trying to create and define an array and i keep getting syntax errors. this is how you make an array right?

Code:
String Array[] = new String[3];             
Array[0] = new String(employeeName);
Array[1] = new String(employeeDepartment);
Array[2] = new String(employeeSalary);

but i get a syntax error on the first line. it wants a { instead of a ;

it wants me to create a block for this even tho every example in my book shows otherwise. and when i do insert a { it makes me chane all my ; to , and then it messes up all the blocks. something is screwy.
 

FordGT90Concept

"I go fast!1!11!1!"
Joined
Oct 13, 2008
Messages
26,263 (4.41/day)
Location
IA, USA
System Name BY-2021
Processor AMD Ryzen 7 5800X (65w eco profile)
Motherboard MSI B550 Gaming Plus
Cooling Scythe Mugen (rev 5)
Memory 2 x Kingston HyperX DDR4-3200 32 GiB
Video Card(s) AMD Radeon RX 7900 XT
Storage Samsung 980 Pro, Seagate Exos X20 TB 7200 RPM
Display(s) Nixeus NX-EDG274K (3840x2160@144 DP) + Samsung SyncMaster 906BW (1440x900@60 HDMI-DVI)
Case Coolermaster HAF 932 w/ USB 3.0 5.25" bay + USB 3.2 (A+C) 3.5" bay
Audio Device(s) Realtek ALC1150, Micca OriGen+
Power Supply Enermax Platimax 850w
Mouse Nixeus REVEL-X
Keyboard Tesoro Excalibur
Software Windows 10 Home 64-bit
Benchmark Scores Faster than the tortoise; slower than the hare.
Code:
String[] fields = new String[3];
fields[0] = employeeName;
fields[1] = employeeDepartment;
fields[2] = employeeSalary;
...assuming employeeName, employeeDepartment, and employeeSalary are already defined as strings...

WHERE
String = type
[] = an array of type
fields = name of the array
new String[3] = initialize a new array of Strings with three null indices. fields.getLength() will return 3
fields[0] = index zero of the fields array
fields[1] = index one of the fields array
fields[2] = index two of the fields array


A way to condense those four lines into one:
Code:
String[] fields = { employeeName, employeeDepartment, employeeSalary };

More information on arrays can be found here:
http://java.sun.com/docs/books/tutorial/java/nutsandbolts/arrays.html



Indicies are always zero-based. Counting numbers are always 1 based. Counting numbers are used for the Length and not much else.
 
Last edited:

Easy Rhino

Linux Advocate
Staff member
Joined
Nov 13, 2006
Messages
15,643 (2.35/day)
Location
Mid-Atlantic
System Name Desktop
Processor i5 13600KF
Motherboard AsRock B760M Steel Legend Wifi
Cooling Noctua NH-U9S
Memory 4x 16 Gb Gskill S5 DDR5 @6000
Video Card(s) Gigabyte Gaming OC 6750 XT 12GB
Storage WD_BLACK 4TB SN850x
Display(s) Gigabye M32U
Case Corsair Carbide 400C
Audio Device(s) On Board
Power Supply EVGA Supernova 650 P2
Mouse MX Master 3s
Keyboard Logitech G915 Wireless Clicky
Software Fedora KDE Spin
ok i am pretty much finished with this array project. i set up 2 classes. 1 is an employee class with an array of 2 subscripts and the other is a manager class with an array of 3 subscripts. both have methods to enter and extract data from their respective classes. now i am writing the class for the main method called TestProgram. i instantiate TestProgram with the typical command and then i want to execute the program using a method i created that allows the user to choose which array he wants to enter information for. so...

Code:
public static void main(String[] args)
	{
		TestProgram x = new TestProgram();
		x.user_choice();
	}

instantiates the TestProgram class and will run the user_choice method. i want the user_choice method to call either the user_interfaceEmployee() or the user_interfaceManager().

Code:
public void user_choice()
	{
		userInput = System.out.println("Enter 1 to enter manager info or 2 to enter employee info.");
		chooseOne = Integer.parseInt(userInput);
		
		switch(chooseOne)
		{
		case 1:
			x.user_interfaceManager;
			x.outputDataManager;
		case 2:
			x.user_interfaceEmployee;
			x.outputDataEmployee();
		}
	}

but as you can imagine it says it cannot resolve x for these cases. any ideas? is this not even possible? should i move some of the code around? here are the employee and manager classes.

Code:
import java.util.StringTokenizer;

public class Employee
{
	String employeeName, employeeSalary;
	String employee3;
	int subScript = 0;
	String[] employeeArray = new String[2];
	
	public void splitUpEmployee(String employee1)
	{
		StringTokenizer emp = new StringTokenizer(employee1);
		employeeName = emp.nextToken();
		employeeSalary = emp.nextToken();
	}
	
	public void addToEmployeeArray(String employee2)
	{
		employeeArray[subScript] = employee2;
		subScript++;
	}
	
	public void extractFromEmployeeArray()
	{
		employee3 = employeeArray[subScript];
		splitUpEmployee(employee3);
		subScript++;
	}
	
	public void initializeCounter()
	{
		subScript = 0;
	}
}//end Employee class

Code:
import java.util.StringTokenizer;

public class Manager extends Employee
{
	String managerName, managerSalary, managerDepartment;
	String manager3;
	int subScript = 0;
	String[] managerArray = new String[3];
		
	public void splitUpManager(String manager1)
	{
		StringTokenizer man = new StringTokenizer(manager1);
		managerName = man.nextToken();
		managerSalary = man.nextToken();
		managerDepartment = man.nextToken();
		subScript++;
	}
	
	public void addToManagerArray(String manager2)
	{
		managerArray[subScript] = manager2;
		subScript++;
	}
	
	public void extractFromManagerArray()
	{
		manager3 = managerArray[subScript];
		splitUpManager(manager3);
		subScript++;
	}
	
	public void initializeCounter()
	{
		subScript = 0;
	}
	
	
}//end Manager class
 
Joined
Aug 15, 2008
Messages
5,941 (0.99/day)
Location
Watauga, Texas
System Name Univac SLI Edition
Processor Intel Xeon 1650 V3 @ 4.2GHz
Motherboard eVGA X99 FTW K
Cooling EK Supremacy EVO, Swiftech MCP50x, Alphacool NeXXos UT60 360, Black Ice GTX 360
Memory 2x16GB Corsair Vengeance LPX 3000MHz
Video Card(s) Nvidia Titan X Tri-SLI w/ EK Blocks
Storage HyperX Predator 240GB PCI-E, Samsung 850 Pro 512GB
Display(s) Dell UltraSharp 34" Ultra-Wide (U3415W) / (Samsung 48" Curved 4k)
Case Phanteks Enthoo Pro M Acrylic Edition
Audio Device(s) Sound Blaster Z
Power Supply Thermaltake 1350watt Toughpower Modular
Mouse Logitech G502
Keyboard CODE 10 keyless MX Clears
Software Windows 10 Pro





Really hope i dont get in trouble for that
 

FordGT90Concept

"I go fast!1!11!1!"
Joined
Oct 13, 2008
Messages
26,263 (4.41/day)
Location
IA, USA
System Name BY-2021
Processor AMD Ryzen 7 5800X (65w eco profile)
Motherboard MSI B550 Gaming Plus
Cooling Scythe Mugen (rev 5)
Memory 2 x Kingston HyperX DDR4-3200 32 GiB
Video Card(s) AMD Radeon RX 7900 XT
Storage Samsung 980 Pro, Seagate Exos X20 TB 7200 RPM
Display(s) Nixeus NX-EDG274K (3840x2160@144 DP) + Samsung SyncMaster 906BW (1440x900@60 HDMI-DVI)
Case Coolermaster HAF 932 w/ USB 3.0 5.25" bay + USB 3.2 (A+C) 3.5" bay
Audio Device(s) Realtek ALC1150, Micca OriGen+
Power Supply Enermax Platimax 850w
Mouse Nixeus REVEL-X
Keyboard Tesoro Excalibur
Software Windows 10 Home 64-bit
Benchmark Scores Faster than the tortoise; slower than the hare.
If TestProgram isn't an instance of a Manager or Employee then you just need to instantiate your Manager or Employee in the switch:
Code:
public void user_choice()
	{
		userInput = System.out.println("Enter 1 to enter manager info or 2 to enter employee info.");
		chooseOne = Integer.parseInt(userInput);
		
		switch(chooseOne)
		{
		case 1:
			Manager manager = new Manager();
			manager.user_interfaceManager;
			manager.outputDataManager;
			break;
		case 2:
			Employee employee = new Employee();
			employee.user_interfaceEmployee;
			employee.outputDataEmployee();
			break;
		}
	}
 

Easy Rhino

Linux Advocate
Staff member
Joined
Nov 13, 2006
Messages
15,643 (2.35/day)
Location
Mid-Atlantic
System Name Desktop
Processor i5 13600KF
Motherboard AsRock B760M Steel Legend Wifi
Cooling Noctua NH-U9S
Memory 4x 16 Gb Gskill S5 DDR5 @6000
Video Card(s) Gigabyte Gaming OC 6750 XT 12GB
Storage WD_BLACK 4TB SN850x
Display(s) Gigabye M32U
Case Corsair Carbide 400C
Audio Device(s) On Board
Power Supply EVGA Supernova 650 P2
Mouse MX Master 3s
Keyboard Logitech G915 Wireless Clicky
Software Fedora KDE Spin
what i did was move the user_interfaceEmployee, outputDataEmployee to the Employee class and moved the user_interfaceManager(), outputDataManager() to the Manager class. i added the lines you showed me in the case however when i run the program i can choose the 1 or 1 option and enter the info but i get...

Code:
Exception in thread "main" java.util.NoSuchElementException
	at java.util.StringTokenizer.nextToken(StringTokenizer.java:332)
	at Manager.splitUpManager(Manager.java:52)
	at Manager.extractFromManagerArray(Manager.java:66)
	at Manager.outputDataManager(Manager.java:40)
	at TestProgram.user_choice(TestProgram.java:19)
	at TestProgram.main(TestProgram.java:32)
 

FordGT90Concept

"I go fast!1!11!1!"
Joined
Oct 13, 2008
Messages
26,263 (4.41/day)
Location
IA, USA
System Name BY-2021
Processor AMD Ryzen 7 5800X (65w eco profile)
Motherboard MSI B550 Gaming Plus
Cooling Scythe Mugen (rev 5)
Memory 2 x Kingston HyperX DDR4-3200 32 GiB
Video Card(s) AMD Radeon RX 7900 XT
Storage Samsung 980 Pro, Seagate Exos X20 TB 7200 RPM
Display(s) Nixeus NX-EDG274K (3840x2160@144 DP) + Samsung SyncMaster 906BW (1440x900@60 HDMI-DVI)
Case Coolermaster HAF 932 w/ USB 3.0 5.25" bay + USB 3.2 (A+C) 3.5" bay
Audio Device(s) Realtek ALC1150, Micca OriGen+
Power Supply Enermax Platimax 850w
Mouse Nixeus REVEL-X
Keyboard Tesoro Excalibur
Software Windows 10 Home 64-bit
Benchmark Scores Faster than the tortoise; slower than the hare.
The error is in here:
Code:
	public void splitUpManager(String manager1)
	{
		StringTokenizer man = new StringTokenizer(manager1);
		managerName = man.nextToken();
		managerSalary = man.nextToken();
		managerDepartment = man.nextToken();
		subScript++;
	}
On line 52. My guess is it doesn't like it what it was handed in "manager1."
 

Easy Rhino

Linux Advocate
Staff member
Joined
Nov 13, 2006
Messages
15,643 (2.35/day)
Location
Mid-Atlantic
System Name Desktop
Processor i5 13600KF
Motherboard AsRock B760M Steel Legend Wifi
Cooling Noctua NH-U9S
Memory 4x 16 Gb Gskill S5 DDR5 @6000
Video Card(s) Gigabyte Gaming OC 6750 XT 12GB
Storage WD_BLACK 4TB SN850x
Display(s) Gigabye M32U
Case Corsair Carbide 400C
Audio Device(s) On Board
Power Supply EVGA Supernova 650 P2
Mouse MX Master 3s
Keyboard Logitech G915 Wireless Clicky
Software Fedora KDE Spin
The error is in here:
Code:
	public void splitUpManager(String manager1)
	{
		StringTokenizer man = new StringTokenizer(manager1);
		managerName = man.nextToken();
		managerSalary = man.nextToken();
		managerDepartment = man.nextToken();
		subScript++;
	}
On line 52. My guess is it doesn't like it what it was handed in "manager1."

strangely i got it to work randomly. but sometimes i start the program i can choose option 1 or 2 and but then i cannot write anything in console. other times i can write in console. clearly i screwed something up.

EDIT! i fixed that bug. ok so i can get employee to work because i am not breaking down the inputStrings into catagories. i am just asking to enter information and it breaks it down using nextToken(). so i am ONE step away from finishing!
 

Easy Rhino

Linux Advocate
Staff member
Joined
Nov 13, 2006
Messages
15,643 (2.35/day)
Location
Mid-Atlantic
System Name Desktop
Processor i5 13600KF
Motherboard AsRock B760M Steel Legend Wifi
Cooling Noctua NH-U9S
Memory 4x 16 Gb Gskill S5 DDR5 @6000
Video Card(s) Gigabyte Gaming OC 6750 XT 12GB
Storage WD_BLACK 4TB SN850x
Display(s) Gigabye M32U
Case Corsair Carbide 400C
Audio Device(s) On Board
Power Supply EVGA Supernova 650 P2
Mouse MX Master 3s
Keyboard Logitech G915 Wireless Clicky
Software Fedora KDE Spin
project finished! it works so i hope i receive full credit. now onto the next project. this class moves fast...

i am essentially creating a program that uses graphics. ive already created the JFrame no problem and have associated most of the necessary panels. the program requires the user to enter a word into an input dialog box and then put it on the JFrame. it is a word guessing game and if you guess 5 letters incorrectly you lose. so the user enters the secret word and i need the program to somehow break that word down by letter and mask it using *****. the guesser will then see the ***** and be asked to guess a letter until either they get it right or they lose. im thinking about using an array to break down the user entered secret word. however i dont know how to get the word entered in the JOptionpane to go to my JPanel. this is what i have so far. ignore the actionPerformed method as i just have it there right now to get the program to actually execute.

Code:
import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class TimeBomb extends JFrame implements ActionListener
{
	private final JLabel lblTitle = new JLabel("Time Bomb Game!");
	private final JButton btnOK = new JButton("OK");
	private final JTextField tfInput = new JTextField(2);
	private final JLabel lblMessage = new JLabel("Enter A Letter: ");
	private final JLabel userWord = new JLabel("");
	
	String inputString;
	
	private char timebombArray[] = new char[8];
	
	public TimeBomb()//constructor for GUI
	{
		super("Time Bomb Game");
		
		inputString = JOptionPane.showInputDialog("Enter Secret Word: ");
		getContentPane().setLayout(new BorderLayout(1, 1));
		
		Font f = new Font("Dialog", Font.PLAIN, 24);
		lblTitle.setFont(f);
		lblMessage.setFont(f);
		tfInput.setFont(f);
		
		
		JPanel northPanel = new JPanel();
		northPanel.add(lblTitle);
		getContentPane().add(northPanel, "North");
		
		JPanel eastPanel = new JPanel();
		eastPanel.add(btnOK);
		getContentPane().add(eastPanel, "East");
		btnOK.addActionListener(this);
		
		JPanel centerPanel = new JPanel();
		centerPanel.add(tfInput);
		getContentPane().add(centerPanel, "Center");
		
		JPanel westPanel = new JPanel();
		westPanel.add(lblMessage);
		getContentPane().add(westPanel, "West");
		
		JPanel southPanel = new JPanel();
		southPanel.add(userWord);
		getContentPane().add(southPanel, "South");
	
		setSize(310, 200);
		setVisible(true);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
	}
	
	public void actionPerformed(ActionEvent e)
	{
		if ("OK".equals(e.getActionCommand()))
		{
			tfInput.setText("");
		}
	}
	
	public static void main(String[] args)
	{
		new TimeBomb();
	}
}//end TimeBomb class
 

FordGT90Concept

"I go fast!1!11!1!"
Joined
Oct 13, 2008
Messages
26,263 (4.41/day)
Location
IA, USA
System Name BY-2021
Processor AMD Ryzen 7 5800X (65w eco profile)
Motherboard MSI B550 Gaming Plus
Cooling Scythe Mugen (rev 5)
Memory 2 x Kingston HyperX DDR4-3200 32 GiB
Video Card(s) AMD Radeon RX 7900 XT
Storage Samsung 980 Pro, Seagate Exos X20 TB 7200 RPM
Display(s) Nixeus NX-EDG274K (3840x2160@144 DP) + Samsung SyncMaster 906BW (1440x900@60 HDMI-DVI)
Case Coolermaster HAF 932 w/ USB 3.0 5.25" bay + USB 3.2 (A+C) 3.5" bay
Audio Device(s) Realtek ALC1150, Micca OriGen+
Power Supply Enermax Platimax 850w
Mouse Nixeus REVEL-X
Keyboard Tesoro Excalibur
Software Windows 10 Home 64-bit
Benchmark Scores Faster than the tortoise; slower than the hare.
I think if you take your string and treat it as an array, you'll get the char index out of it... e.g.
Code:
String mystring = "blamo";
mystring[0] // b
mystring[1] // l
mystring[2] // a
mystring[3] // m
mystring[4] // o
The alternative would be to break it down into a byte array:
Code:
String mystring = "blamo";
byte[] bytes = mystring.getBytes(); // Get the bytes from the string.
String mynewstring = new String(bytes); // Convert bytes back to a string.
 

Easy Rhino

Linux Advocate
Staff member
Joined
Nov 13, 2006
Messages
15,643 (2.35/day)
Location
Mid-Atlantic
System Name Desktop
Processor i5 13600KF
Motherboard AsRock B760M Steel Legend Wifi
Cooling Noctua NH-U9S
Memory 4x 16 Gb Gskill S5 DDR5 @6000
Video Card(s) Gigabyte Gaming OC 6750 XT 12GB
Storage WD_BLACK 4TB SN850x
Display(s) Gigabye M32U
Case Corsair Carbide 400C
Audio Device(s) On Board
Power Supply EVGA Supernova 650 P2
Mouse MX Master 3s
Keyboard Logitech G915 Wireless Clicky
Software Fedora KDE Spin
I think if you take your string and treat it as an array, you'll get the char index out of it... e.g.
Code:
String mystring = "blamo";
mystring[0] // b
mystring[1] // l
mystring[2] // a
mystring[3] // m
mystring[4] // o

ive done some tweaking but i keep running into the same problem. the basic outline of the program is this.

user inputs a secret word in JOptionPane.showInputDialog

Code:
inputString = JOptionPane.showInputDialog("Enter Secret Word: ");

that secret word needs to go into an array i created

Code:
private String timebombArray[] = new String[10];

the array will break the word down by letter. and then it needs to go into the southPanel of my JFrame.

Code:
JPanel southPanel = new JPanel();
		southPanel.add(userWord);
		getContentPane().add(southPanel, "South");

but obviously userWord is a JFrame and inputString is a String. this is where the problem is.
 

Kreij

Senior Monkey Moderator
Joined
Feb 6, 2007
Messages
13,817 (2.10/day)
Location
Cheeseland (Wisconsin, USA)
Sorry Easy, but I have not done any Java coding.
I just downloaded the JDK, so give me a couple of days to learn the language and I will be able to be of more help :toast:
 

Easy Rhino

Linux Advocate
Staff member
Joined
Nov 13, 2006
Messages
15,643 (2.35/day)
Location
Mid-Atlantic
System Name Desktop
Processor i5 13600KF
Motherboard AsRock B760M Steel Legend Wifi
Cooling Noctua NH-U9S
Memory 4x 16 Gb Gskill S5 DDR5 @6000
Video Card(s) Gigabyte Gaming OC 6750 XT 12GB
Storage WD_BLACK 4TB SN850x
Display(s) Gigabye M32U
Case Corsair Carbide 400C
Audio Device(s) On Board
Power Supply EVGA Supernova 650 P2
Mouse MX Master 3s
Keyboard Logitech G915 Wireless Clicky
Software Fedora KDE Spin
Sorry Easy, but I have not done any Java coding.
I just downloaded the JDK, so give me a couple of days to learn the language and I will be able to be of more help :toast:

thanks. ive got 2 weeks to get this done. problem is we have another project assigned next week as well. so im trying to stay ahead of the game since i have a decent learning curve.
 

Easy Rhino

Linux Advocate
Staff member
Joined
Nov 13, 2006
Messages
15,643 (2.35/day)
Location
Mid-Atlantic
System Name Desktop
Processor i5 13600KF
Motherboard AsRock B760M Steel Legend Wifi
Cooling Noctua NH-U9S
Memory 4x 16 Gb Gskill S5 DDR5 @6000
Video Card(s) Gigabyte Gaming OC 6750 XT 12GB
Storage WD_BLACK 4TB SN850x
Display(s) Gigabye M32U
Case Corsair Carbide 400C
Audio Device(s) On Board
Power Supply EVGA Supernova 650 P2
Mouse MX Master 3s
Keyboard Logitech G915 Wireless Clicky
Software Fedora KDE Spin
ok this may be an easier way for me to break down the problems i am having. there are 3 big problems to accomplish and im taking them one at a time. the first is to get user input as a string and put it into an array of characters. i decided to write a small program to help me sort out the problem without messing with my JFrame code. here is what i have which attempts to take inputString from the keyboard which is input by the user using a dialog box and putting it into an array called timebombArray. i just cant figure out how to put it into the array since one is a String and the other is a char array.

Code:
import javax.swing.*;
import java.io.*;

public class ArrayDemo2 
{
	String inputString;
	private BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));

	public void wordInput() throws IOException
	{
		inputString = JOptionPane.showInputDialog("Enter Word: ");
		inputString = keyboard.readLine();
	}
	
	char[] timebombArray = new char[9];
	
	
	public void wordOutput()
	{
		for(int i = 0; i < timebombArray.length; i++)
			System.out.println(timebombArray[i]);
	}
	
	
	public static void main(String[] args) throws IOException
	{
		ArrayDemo2 x = new ArrayDemo2();
		x.wordInput();
		x.wordOutput();
	}//end of main
}//end of class
 

Kreij

Senior Monkey Moderator
Joined
Feb 6, 2007
Messages
13,817 (2.10/day)
Location
Cheeseland (Wisconsin, USA)
I'm not sure on this in Java.
In C# a string is basically a character array.
So I can say..
Code:
string myString = "Vent";
myString[0] = "B";

The resulting string in myString will equal "Bent".
 

Easy Rhino

Linux Advocate
Staff member
Joined
Nov 13, 2006
Messages
15,643 (2.35/day)
Location
Mid-Atlantic
System Name Desktop
Processor i5 13600KF
Motherboard AsRock B760M Steel Legend Wifi
Cooling Noctua NH-U9S
Memory 4x 16 Gb Gskill S5 DDR5 @6000
Video Card(s) Gigabyte Gaming OC 6750 XT 12GB
Storage WD_BLACK 4TB SN850x
Display(s) Gigabye M32U
Case Corsair Carbide 400C
Audio Device(s) On Board
Power Supply EVGA Supernova 650 P2
Mouse MX Master 3s
Keyboard Logitech G915 Wireless Clicky
Software Fedora KDE Spin
I'm not sure on this in Java.
In C# a string is basically a character array.
So I can say..
Code:
string myString = "Vent";
myString[0] = "B";

The resulting string in myString will equal "Bent".

yes exactly, but in my case the String literal value isn't preset, it is actually set when the user inputs the word into the dialog box. that is what i am having trouble with.
 
Top