Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Beginner Java Help !

  1. #1
    Junior Member
    Join Date
    Apr 2014
    Posts
    6
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Beginner Java Help !

    Hey,
    I was trying to play around a little bit after learning creating multiple classes and stuff.However,i encountered a strange problem with reading a value from the user and then storing it in a variable.The usual way i do it is
    Scanner variableName=new Scanner(System.in);
    System.out.println(variableName.nextLine());
    .But when i trying to print the contents of the variable "variableName" the compiler throws a lot of errors .I am attaching how i have tried that out in my code
    import java.util.Scanner;
    class laptop{
    	private String modelNumber;
    	private boolean hasFan;
    	private float ramSpeed;
    	protected int numCores;
    	//private String input;
    	//public int num;
    	private boolean newInput;
    	public laptop(String modelNumber,boolean hasFan,float ramSpeed,int numCores){
    		this.modelNumber=modelNumber;
    		this.hasFan=hasFan;
    		this.ramSpeed=ramSpeed;
    		this.numCores=numCores;
    		}
     
    	public void getInfo(){
    		System.out.print("This Laptop is "+modelNumber+" model of laptop\n"+
    						 "Fan :"+hasFan+" Number of Cores :"+numCores+
    						 "Ram Speed :"+ramSpeed);
    						 }
    	public void setInfo(){
    		System.out.println("What do you want to modify?");//Assuming user types no:1 for changing the status of hasFan
    		Scanner num=new Scanner(System.in);
    		System.out.println(num.nextInt());
    		switch(num) {
    			case 1:
    			{
    			System.out.println("Enter the desired status");
    			Scanner newInput=new Scanner(System.in);
    			System.out.println(newInput.nextBoolean());
    			System.out.println(newInput);
    			this.hasFan=newInput;
    			break;
    			}
    		}
     
    		}
    	}

    And the main program is
    public class Implement{
    	public static void main(String[] args){
    	laptop objectLaptop=new laptop("G580",true,3.2f,8);
    	//apple objectApple=new apple(true);
    	objectLaptop.getInfo();
    	objectLaptop.setInfo();
    	}
    	}

    As you can see this is not a very functional code but only to cement my learning of the concept.Without the setInfo() in the laptop class the program functions as desired but i intend to ask the user if he wants to modify something and then reflect the same.


  2. #2
    Junior Member JavaMistress's Avatar
    Join Date
    Apr 2014
    Location
    The Milky Way :3
    Posts
    9
    My Mood
    Stressed
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Beginner Java Help !

    The problem is at "switch(num)".

    "num" is, at the moment, a Scanner-object and not an int.

    You may have printed out "num.nextInt()" in that println-method, but you have not changed the variable to an int yet. But the "switch"-statement can only work with ints.

    Instead of

          Scanner num=new Scanner(System.in);
    		System.out.println(num.nextInt());
    		switch(num) {

    you could write something like:

    Scanner num=new Scanner(System.in);
    int numb = num.nextInt();
    		System.out.println(num.nextInt());
    		switch(numb) {

    ... it should work now.

    PS:

    You could alternatively use this:

    Scanner num=new Scanner(System.in);
    System.out.println(num.nextInt());
    switch(num.nextInt()) {

    Itīs much shorter, but not my style.
    Last edited by JavaMistress; April 25th, 2014 at 06:04 AM.
    One day I got the chance to speak to a hacker.. of course, I asked him to tell me where to start. i asked him what to do and even requested homework...

    He said: "Hehe, you have to work hard on your own little girl.. donīt change your dreams, change the world.."

    ... and thatīs what Iīm going to do. If he is somewhere out there and reads this someday, I thank him for encouraging me. His words have boosted my will to fight!

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Beginner Java Help !

    the compiler throws a lot of errors
    Please copy the full text of the error message and paste it here.
    If you don't understand my answer, don't ignore it, ask a question.

  4. #4
    Junior Member JavaMistress's Avatar
    Join Date
    Apr 2014
    Location
    The Milky Way :3
    Posts
    9
    My Mood
    Stressed
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Beginner Java Help !

    Oh, sorry, I forgot something...

    the same counts for

    System.out.println(newInput);
    			this.hasFan=newInput;
    			break;

    Instead of newInput, use newInput.nextBoolean().

    System.out.println(newInput);
    			this.hasFan=newInput.nextBoolean();
    			break;

    ... Iīm sorry, forgot this one ^^;
    One day I got the chance to speak to a hacker.. of course, I asked him to tell me where to start. i asked him what to do and even requested homework...

    He said: "Hehe, you have to work hard on your own little girl.. donīt change your dreams, change the world.."

    ... and thatīs what Iīm going to do. If he is somewhere out there and reads this someday, I thank him for encouraging me. His words have boosted my will to fight!

  5. The Following User Says Thank You to JavaMistress For This Useful Post:

    thebenman (April 25th, 2014)

  6. #5
    Junior Member
    Join Date
    Apr 2014
    Posts
    6
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Beginner Java Help !

    Yeah,Thanks that helped a lot.It now works fine.But i wanted to add more functionality by asking the user if he wanted to modify them once more,so i added a do..while loop.However when i wanted to scan a char from the user i found out that the method nextChar() does not exist.Could you tell me on how to scan a char input from the user ?
    import java.util.*;
    class laptop{
    	private String modelNumber;
    	private boolean hasFan;
    	private float ramSpeed;
    	protected int numCores;
    	private int newNum;
    	private boolean newInput;
    	public laptop(String modelNumber,boolean hasFan,float ramSpeed,int numCores){
    		this.modelNumber=modelNumber;
    		this.hasFan=hasFan;
    		this.ramSpeed=ramSpeed;
    		this.numCores=numCores;
    		}
     
    	public void getInfo(){
    		System.out.print("\nThis Laptop is "+modelNumber+" model of laptop\n"+
    						 "Fan :"+hasFan+" \nNumber of Cores :"+numCores+
    						 "\nRam Speed :"+ramSpeed+"\n");
    						 }
    	public void setInfo(){
    	do{
    		System.out.println("What do you want to modify?\n1.Presence of Fan\n2.Number of Cores.");
    		Scanner num=new Scanner(System.in);
    		newNum=num.nextInt();
     
    		switch(newNum) {
    			case 1:
    			{
    			System.out.println("Enter the desired status");
    			Scanner newInput=new Scanner(System.in);
    	        this.hasFan=newInput.nextBoolean();
    		    break;
    			}
    			case 2:
    			{
    			System.out.println("Enter the desired status");
    			Scanner newInputCores=new Scanner(System.in);
    			this.numCores=newInputCores.nextInt();
    			break
    			}
    			default:
    			{
    			System.out.println("\nPlease enter a proper value");
    			System.out.println("Wanna try again (Select y/n) ?");
    			}
    		}
    	}
    		System.out.println("Wanna modify again (y/n)?");
    		char newChar=            //Scan newchar here
    		while
     
    		}
    	}


    --- Update ---

    The error was because is tried to switch a Scanner before converting it into a int data type

  7. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Beginner Java Help !

    how to scan a char input from the user ?
    Read the user's input into a String and use a String class method to get the char that are in the String.
    If you don't understand my answer, don't ignore it, ask a question.

  8. The Following User Says Thank You to Norm For This Useful Post:

    thebenman (April 25th, 2014)

  9. #7
    Junior Member JavaMistress's Avatar
    Join Date
    Apr 2014
    Location
    The Milky Way :3
    Posts
    9
    My Mood
    Stressed
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Beginner Java Help !

    there is no method to scan a char directly, unfortunately... so Iīd go with Normīs idea.

    SOmething like:

    Scanner chars = new Scanner(System.in);
    String toBeChar = chars.toString();
    char newChar = toBeChar.charAt(0);

    Does this work? ^^
    One day I got the chance to speak to a hacker.. of course, I asked him to tell me where to start. i asked him what to do and even requested homework...

    He said: "Hehe, you have to work hard on your own little girl.. donīt change your dreams, change the world.."

    ... and thatīs what Iīm going to do. If he is somewhere out there and reads this someday, I thank him for encouraging me. His words have boosted my will to fight!

Similar Threads

  1. java beginner
    By vitalisT in forum Member Introductions
    Replies: 2
    Last Post: November 5th, 2013, 09:47 AM
  2. Beginner Java HELP!
    By finals88 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 30th, 2013, 01:14 AM
  3. Replies: 2
    Last Post: January 18th, 2013, 11:12 AM
  4. Replies: 1
    Last Post: January 6th, 2013, 06:32 AM
  5. Beginner in java, looking for all the help I can get
    By Travis.Nichols777 in forum Member Introductions
    Replies: 2
    Last Post: February 26th, 2012, 07:31 AM