9 Replies - 702 Views - Last Post: 17 April 2011 - 10:46 PM Rate Topic: -----

#1 CallMeHal  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 17-April 11

Beginner Question Regarding Nested If-Else Structure Implementation

Posted 17 April 2011 - 09:36 PM

I am taking a Java course at my local CC, and I have managed to write a fairly concise albeit basic program utilizing a scanner object. The code is below, and if you think it looks kind of repetitive...well it is, :online2long: . We're supposed to demonstrate a number of techniques. My question is a two-parter. First and foremost, where do you think a good implementation of an if-else structure would be? I was thinking of using one to provide parameters for the user inputed int for the ramCount question (if 1-6 else repeat question with additional print statement specifying the accepted int range). Second, is there anyway to restart the program based on a an if-else question at the end of the program? I have googled it quite a bit, and have seen a solutions using do-while, but I would be interested in completing it using if-else.

Thanks guys, I may be a total noob with code outside of HTML and CSS, but I'm here to learn.

public class Computer
{
   private String cpuType;  // This is the first name attribute of a Computer object
	private String moboType;
	private int ramCount;
	private int hddSize;   // This is the last name attribute of a Computer object
	
	// This is a constructor with no parameters for the Computer class 
	
	public Computer()
	{
	   setCpuType("");
		setMoboType("");
		setRamCount(0);
		setHddSize(0);
	}
	
	public void setCpuType(String cType)
	{
	   cpuType = cType;
	}
	
	public void setMoboType(String mType)
	{
	   moboType = mType;
	}

	public void setRamCount(int ramInSticks)
	{
	   ramCount = ramInSticks;
	}
	
	public void setHddSize(int hddInGigs)
	{
	   hddSize = hddInGigs;
	}
	
	public String getCpuType()
	{
	   return cpuType;
	}
	
	public String getMoboType()
	{
	   return moboType;
	}

	public int getRamCount()
	{
	   return ramCount;
	}
  public int getHddSize()
	{
	   return hddSize;
	} 
	
	// This is a special toString() method that returns the attributes of a Computer object
	// when the object used in a String context. 
   public String toString()
	{
	   return "CPU Type: "+getCpuType()+"\n"+
		       "Motherboard Type:  "+getMoboType()+"\n"+
				 "Sticks of RAM: "+getRamCount()+"\n"+
				 "Size of HDD (in Gigabytes): "+getHddSize();
	}
}




public class ComputerBuilder
{
	public static void main(String[] args)
	{
	
	   Scanner inputObject = new Scanner(System.in);

		Computer computer1 = new Computer();
	
		String cpuType;
		String moboType;
		int ramCount;
		int hddSize;
		
		System.out.print("What type of CPU would you like to choose for this build? ");
		cpuType = inputObject.nextLine();
		computer1.setCpuType(cpuType);
		
		System.out.print("What type of Motherboard would you like for this build? ");
		moboType = inputObject.nextLine();
		computer1.setMoboType(moboType);

		System.out.print("How many sticks of RAM would you like? ");
		ramCount = Integer.parseInt(inputObject.nextLine());
		computer1.setRamCount(ramCount);
		
		System.out.print("What size HDD (in GB) would you like as your boot drive? ");
		hddSize = Integer.parseInt(inputObject.nextLine());
		computer1.setHddSize(hddSize);
		
		System.out.println(computer1);
	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: Beginner Question Regarding Nested If-Else Structure Implementation

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 9025
  • View blog
  • Posts: 33,465
  • Joined: 27-December 08

Re: Beginner Question Regarding Nested If-Else Structure Implementation

Posted 17 April 2011 - 09:51 PM

You have to use loops for both of these. Because the goto statement will only cause compilation errors (and shouldn't be used in languages where it doesn't), you must use loops.

Some logic for the validation part.
boolean valid = false;
while(!valid){
    //get input
    //validate input
    //if input is valid, set valid = true
    //otherwise, print out message for user
}


Was This Post Helpful? 0
  • +
  • -

#3 CallMeHal  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 17-April 11

Re: Beginner Question Regarding Nested If-Else Structure Implementation

Posted 17 April 2011 - 10:13 PM

View Postmacosxnerd101, on 17 April 2011 - 09:51 PM, said:

You have to use loops for both of these. Because the goto statement will only cause compilation errors (and shouldn't be used in languages where it doesn't), you must use loops.

Some logic for the validation part.
boolean valid = false;
while(!valid){
    //get input
    //validate input
    //if input is valid, set valid = true
    //otherwise, print out message for user
}



Thank you for the prompt response. I'm having some trouble applying your response to my code, mostly because of syntax I believe. Is there any chance you could post an example of code using valid? Thanks.
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 9025
  • View blog
  • Posts: 33,465
  • Joined: 27-December 08

Re: Beginner Question Regarding Nested If-Else Structure Implementation

Posted 17 April 2011 - 10:16 PM

If I want to continue until I get a value of 1, I could do:
boolean valid = false;
while(!valid){
      int input = myScanner.nextInt();
      myScanner.nextLine();

      valid = (input == 1);
      if(!valid){
          System.out.println("Invalid input. Try again");
      } 
}


Was This Post Helpful? 1
  • +
  • -

#5 CallMeHal  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 17-April 11

Re: Beginner Question Regarding Nested If-Else Structure Implementation

Posted 17 April 2011 - 10:30 PM

View Postmacosxnerd101, on 17 April 2011 - 10:16 PM, said:

If I want to continue until I get a value of 1, I could do:
boolean valid = false;
while(!valid){
      int input = myScanner.nextInt();
      myScanner.nextLine();

      valid = (input == 1);
      if(!valid){
          System.out.println("Invalid input. Try again");
      } 
}



Thanks, that's the basic example I was looking for. Now I'm getting an error though at the myScanner which says int cannot be deferenced. I'm sure it's something ridicously dumb I'm missing.
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 9025
  • View blog
  • Posts: 33,465
  • Joined: 27-December 08

Re: Beginner Question Regarding Nested If-Else Structure Implementation

Posted 17 April 2011 - 10:34 PM

Post your revised code.

You cannot invoke methods or reference variables from primitives, including ints.
Was This Post Helpful? 0
  • +
  • -

#7 CallMeHal  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 17-April 11

Re: Beginner Question Regarding Nested If-Else Structure Implementation

Posted 17 April 2011 - 10:37 PM

View Postmacosxnerd101, on 17 April 2011 - 10:34 PM, said:

Post your revised code.

You cannot invoke methods or reference variables from primitives, including ints.


System.out.print("How many sticks of RAM would you like? ");
		ramCount = Integer.parseInt(inputObject.nextLine());
		computer1.setRamCount(ramCount);
		
		boolean valid = false;
		while(!valid){
			int input = ramCount.nextInt();
			ramCount.nextLine();
			
			valid = (input == 1);
			if(!valid){
			System.out.println("Invalid input. Try again");
			}
		}


Sorry, I thought I had already posted it. Obviously I didn't do much to change it, I'm just trying to see if I can figure out how to manipulate into my code first. Thanks for your help.
Was This Post Helpful? 0
  • +
  • -

#8 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 9025
  • View blog
  • Posts: 33,465
  • Joined: 27-December 08

Re: Beginner Question Regarding Nested If-Else Structure Implementation

Posted 17 April 2011 - 10:38 PM

The ramCount variable is an int, not a Scanner object. You cannot invoke methods off of an int.
Was This Post Helpful? 0
  • +
  • -

#9 CallMeHal  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 17-April 11

Re: Beginner Question Regarding Nested If-Else Structure Implementation

Posted 17 April 2011 - 10:44 PM

View Postmacosxnerd101, on 17 April 2011 - 10:38 PM, said:

The ramCount variable is an int, not a Scanner object. You cannot invoke methods off of an int.


Well I initially tried Computer which I thought was the name of my scanner. That got me the "cannot find symbol" error. Then I tried Scanner as the scanner name and I recieved "ComputerBuilder.java:34: non-static method nextInt() cannot be referenced from a static context"
Was This Post Helpful? 0
  • +
  • -

#10 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 9025
  • View blog
  • Posts: 33,465
  • Joined: 27-December 08

Re: Beginner Question Regarding Nested If-Else Structure Implementation

Posted 17 April 2011 - 10:46 PM

Look through your code. This is your Scanner. Invoke the Scanner methods from the inputObject variable.
Scanner inputObject = new Scanner(System.in);  


Was This Post Helpful? 1
  • +
  • -

Page 1 of 1