Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Problem Making A String With Ints

  • 02-03-2012 1:45pm
    #1
    Closed Accounts Posts: 95 ✭✭


    First I apologise that my indentation is all over the place but I am struggling so hard with this course that I'm becoming depressed by it so please accept my apologies that I'm just not very good at this stuff.

    My problem is I can't get this part to work

    public void setTime (String time, int sec, int min, int hour)
    {
    time = hour + ":" + ":" + min + ":" + sec;
    }

    time.setTime();

    It just keeps telling me I need an identifier or something.

    As I said I'm really bad at this. What I have done so far is probably complete s h one T but if I fail having a go I guess its better than failing by not trying at all.
    import java.io.IOException;
    import java.util.Scanner;
    import java.io.File;
    
    public class ClockAssignment {
    
        /**
         * @param args
         */
        public static void main(String[] args) {
            /**
             * Read in the seconds, minutes and hours from clock.dat
             * The first three variables in the file are to set the time and the
             * second three are for the alarm.
             */
            
    		File file = new File ("clock.dat");
    		
    		String time = "";		
            int sec = 0;
            int min = 0;
            int hour = 0;
            int aSec = 0;
            int aMin = 0;
            int aHour = 0;
            
            try{
    
    			Scanner scanner = new Scanner (file);
    			sec = scanner.nextInt();
    			min = scanner.nextInt();
    			hour = scanner.nextInt();
    			aSec = scanner.nextInt();
    			aMin = scanner.nextInt();
    			aHour = scanner.nextInt();
    			/**
    			 * Read in the variables from clock.dat here
    			 */
                
            }catch(IOException e){
                System.out.println("Unable to open file: " + e.getMessage());
            }
    		
    		System.out.println(sec);
    		System.out.println(min);
    		System.out.println(hour);
    		System.out.println(aSec);
    		System.out.println(aMin);
    		System.out.println(aHour);
    
        }
    
    }
    
    abstract class Clock extends ClockAssignment
    {
    
    abstract void tick(int sec);
    
    public void seconds (int sec, int min)
    {
    	if (sec == 0)
    	{
    	sec = 0;
    	min = min + 1;
    	}
    }
    
    public void minutes (int min, int hour)
    {
    	if (min == 0)
    	{
    	min = 0;
    	hour = hour + 1;
    	}
    }
    
    abstract void setTime (String time, int sec, int min, int hour);
    
    public void hour (int hour)
    {
    	if (hour == 24)
    	{
    	hour = 0;
    	}
    }
    
    
    
    }
    
    class NormalClock extends Clock
    {
    public void tick (int sec)
    {
    	sec = sec + 1;
    }
    
    public void setTime (String time, int sec, int min, int hour)
    {
    	time = hour + ":" + ":" + min + ":" + sec;
    }
    
    time.setTime();
    
    }
    


Comments

  • Registered Users, Registered Users 2 Posts: 9,579 ✭✭✭Webmonkey


    Hi,

    First of all - Don't apologise for not being able to do something. You are learning and there's nothing wrong with that.

    Secondly...into the code...

    ClockAssignment is your main class with your main method. - Fine.
    However, you have another class, Clock that extends from this. This doesn't make much sense, especially since your Clock class is abstract. Abstract classes can't extend/inherit/subclass from non abstract classes.

    I don't think your Clock class should extend anything. It should just be a simple abstract class.

    Next, you have the "NormalClock" class that extends from the abstract Clock class and overrides the tick method which is good (When Clock class is a simple class that doesn't extend from ClockAssignment as described above).

    Now in your setTime method you are setting the string time to the concantenation of everything. Maybe you mean to have a String time instance variable declared in your class so you can access this. (this.time = hour+....).

    Also time.setTime() is away out on it's own in no mans land. It has to be in a method in the class. What are you trying to achieve by this.

    Maybe it would be better to explain exactly what the purpose of this class is.


  • Registered Users, Registered Users 2 Posts: 1,235 ✭✭✭Odaise Gaelach


    class NormalClock extends Clock
    {
    public void tick (int sec)
    {
    	sec = sec + 1;
    }
    
    public void setTime (String time, int sec, int min, int hour)
    {
    	time = hour + ":" + ":" + min + ":" + sec;
    }
    
    [B]time.setTime();[/B]
    
    }
    

    My explaination might be a bit verbose, but bear with me. :)

    The time.setTime(); you have at the bottom, highlighted in bold, is the problem. It's in a place where Java has method definitions and instance variables, rather than method calls (which is what I believe you have). Since it's not a properly-formatted method definition it's telling you that you need an identifier to complete the definition.

    At that position you should be telling your program what should happen when you set the time (a definition). Instead, you're trying to get your program to set the time (a call). So I'd suggest removing that time.setTime(); line. Your code should compile then, and you can finish the rest of your assignment. :)


  • Closed Accounts Posts: 816 ✭✭✭Opinicus


    you're trying to call time.setTime() in your clock class which doesn't make any sense to me.

    that should be called in main, and you need to pass values to it.


    Edit: Webmonkey covered all the bases there^^^^


  • Closed Accounts Posts: 95 ✭✭The Crab


    Thanks everyone.

    I'm still a little confused though. What I need to do is somehow set the time into a string. I want it to print the time to the screen then I'm going to run a for loop on tick 100,000 times and reprint the time. The problem is that I need to somehow create a string that looks something like 00:00:00.

    Slightly personal question: with programming and other IT issues, is it normal to feel extremely frustrated whilst learning it or is it something that some people take to like a duck to water and get a lot of enjoyment from an others (for example me) find incredibly frustrating because they'll never be any good at it? I've been on this course over four months and can't honestly say I'm enjoying it and am trying to stick in solely because its the one area these days where there is any chance of employment. I hope this doesn't seem offensive, I just want to know am I wasting my time or not.


  • Closed Accounts Posts: 95 ✭✭The Crab


    I've changed it to this. It prints the time to the screen but tonightTime.tick(); doesn't work. I really will never get anywhere with this stuff, my brain just won't allow me :(.
    import java.io.IOException;
    import java.util.Scanner;
    import java.io.File;
    
    public class ClockAssignment {
    
        /**
         * @param args
         */
        public static void main(String[] args) {
            /**
             * Read in the seconds, minutes and hours from clock.dat
             * The first three variables in the file are to set the time and the
             * second three are for the alarm.
             */
            
    		File file = new File ("clock.dat");
    		
    		String time = "";		
            int sec = 0;
            int min = 0;
            int hour = 0;
            int aSec = 0;
            int aMin = 0;
            int aHour = 0;
            
            try{
    
    			Scanner scanner = new Scanner (file);
    			sec = scanner.nextInt();
    			min = scanner.nextInt();
    			hour = scanner.nextInt();
    			aSec = scanner.nextInt();
    			aMin = scanner.nextInt();
    			aHour = scanner.nextInt();
    			/**
    			 * Read in the variables from clock.dat here
    			 */
                
            }catch(IOException e){
                System.out.println("Unable to open file: " + e.getMessage());
            }
    		
    		System.out.println(sec);
    		System.out.println(min);
    		System.out.println(hour);
    		System.out.println(aSec);
    		System.out.println(aMin);
    		System.out.println(aHour);
    
    		[B]String tonightTime = hour + ":" + min + ":" + sec;
    		System.out.println(tonightTime);[/B]
        }
    
    }
    
    abstract class Clock
    {
    
    abstract void tick(int sec);
    
    public void seconds (int sec, int min)
    {
    	if (sec == 0)
    	{
    	sec = 0;
    	min = min + 1;
    	}
    }
    
    public void minutes (int min, int hour)
    {
    	if (min == 0)
    	{
    	min = 0;
    	hour = hour + 1;
    	}
    }
    
    public void hour (int hour)
    {
    	if (hour == 24)
    	{
    	hour = 0;
    	}
    }
    
    
    
    }
    
    class NormalClock extends Clock
    {
    public void tick (int sec)
    {
    	sec = sec + 1;
    }
    
    [B]tonightTime.tick();[/B]
    
    }
    


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 2,494 ✭✭✭kayos


    Ok I'm no java head but....

    String time = ""
    setTime(time, 1,1,1);
    System.out.println(time);

    What do you think get's printed out? In pretty much any language I've used that would be a big fat empty string......


  • Registered Users, Registered Users 2 Posts: 2,013 ✭✭✭lynchie


    tonightTime.tick() wont work

    - Firstly because its position in the class is not valid. Its not defined in a method. Only class identifiers may be defined outside a method declaration.
    - Secondly, even if it were in a method, what is its type, where is it defined?


  • Closed Accounts Posts: 95 ✭✭The Crab


    Thanks Lynchie but I have no clue what you're saying. I need this in as simple terms as possible. My fault not your's. I've written the tick method in the class so how is the method not there?

    Kayos, I know you're trying to help but I've already said I'm hopeless at this stuff so I really wouldn't know if it would contain nothing or a million things.


  • Registered Users, Registered Users 2 Posts: 586 ✭✭✭Aswerty


    The second last line tonightTime.tick(); is never going to be called because your program will never reach it. Also the String tonightTime is local to the main function such that the NormalClock class has no understanding of it. Also a String will not be able to call the Tick() method because it is not a method of the String class. Finally calling the method Tick() in it's current form will not cause much to happen. For example if the following code was implemented:
    NormalClock normClock = new NormalClock();
    normClock.Tick(sec);

    Only the sec variable in the Tick method will increase because the sec variable in the main method is seperate from the sec variable in the Tick method. If we wanted to link these variable we could do the following:

    public static void main(String[] args)
    {
    // skipped the implemented code for brevity

    NormalClock normClock = new NormalClock();
    sec = normClock.Tick(sec);
    }

    class NormalClock extends Clock
    {
    public int tick (int sec)
    {
    sec = sec + 1;
    return sec;
    }
    }

    The sec variable in the main method is now increased by one because the Tick method returned it's local variable sec and the main method's local variable sec was given it's value.

    You do not seem to understand what the scope of your variables are. Scope is the context in which your variables exists. Your variables in your main method are only relevant to your main method. There are even scopes inside your main method; the scanner variables scope is the try block. If you tried to use your scanner variable outside of the try block in another part of the main method you would get compilation/runtime errors. Scopes are generally defined with { }. The variable name may be the same in both classes but that does not make them the same variable since they have different scopes. As I have shown above you can deal with scopes by passing values to a method by putting it inside the method call (e.g. normClock.Tick(sec)). The variable can then be passed back by using the return keyword as well as declaring the return type before the method name (e.g. int Tick(int sec)). Any returned value needs to be passed to a variable once it is returned (e.g. sec = normClock.Tick(sec)).

    I would advise you to not use an abstract class or the abstract keyword at all (if required for the assignment implement it after you have everything else working) and just go with the NormalClock class. Then try to increase the seconds value using the techniques shown above to call the Tick method. Once that is done you can look to see how you can update the min variable and reset the sec variable to 0 when the sec variable reaches 59.


  • Registered Users, Registered Users 2 Posts: 620 ✭✭✭Laika1986


    I'm gonna hijack this thread as it seems I am in the same class as the OP. I have implemented the solution that was giving to us however I cannot read in the file. I am sure it is in my default package but still it wont read. any help or advice would be great, ill post the code anyway but it seems fine.
    import java.io.FileReader;
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.IOException;
    import java.util.Scanner;
    
    
    
    public class ClockAssignment {
    
        /**
         * @param args
         */
        public static void main(String[] args) {
            /**
             * Read in the seconds, minutes and hours from clock.dat
             * The first three variables in the file are to set the time and the
             * second three are for the alarm.
             */
            
            int sec = 0;
            int min = 0;
            int hour = 0;
            int aSec = 0;
            int aMin = 0;
            int aHour = 0;
            
            NormalClock nClock= new NormalClock();
            ReverseClock rClock=new ReverseClock();
            AlarmClock aClock=new AlarmClock();
            
            try{
            	int i=0;
            	Scanner scanner=new Scanner(new File("clock.dat"));
            	int [] clock= new int[6];
            			while(scanner.hasNextInt()){
            				clock[i++]=scanner.nextInt();
            				
    		}
            	
            	File theFile=new File("clock.dat");
        		Scanner input = new Scanner(theFile);
            	
            	sec=input.nextInt();
            	min=input.nextInt();
            	hour=input.nextInt();
            	aSec=input.nextInt();
            	aMin=input.nextInt();
            	aHour=input.nextInt();
            	
            	nClock.setTime(sec,min,hour);
            	nClock.print();
            	rClock.setTime(sec,min,hour);
            	rClock.print();
            	aClock.setTime(aSec,aMin,aHour);
            	aClock.print();
            	
            	for (int j=0;j<100000;j++){
            		nClock.tick();
            		rClock.tick();
            		aClock.alarmTick();
            	}
            	
            	nClock.print();
            	rClock.print();
            	
            	
    			
            }catch(IOException e){
                System.out.println("Unable to open file: " + e.getMessage());
            }
    		}
    
        }
    


  • Advertisement
  • Closed Accounts Posts: 816 ✭✭✭Opinicus


    What IDE are you using?

    If netbeans don't put in the src folder, place it in the project folder alongside build, src and nbproject folders.


Advertisement