Results 1 to 3 of 3
  1. #1
    Sotsiak is offline Member
    Join Date
    Jun 2010
    Posts
    5
    Rep Power
    0

    Default check a string char by char

    Hi, first of all the code:

    Java Code:
    import java.io.*;
    import java.util.Calendar;
    import java.text.SimpleDateFormat;
    import java.text.DecimalFormat;
    
    class UserInput {
     public static String getString() {
      String line;
      InputStreamReader input=new InputStreamReader(System.in);
      BufferedReader in=new BufferedReader(input);
      try {
       line=in.readLine();
       return line;
      }
      catch(Exception e) {
      return "Exception";
      }
     }
    
     public static int getInteger() {
      String line;
       InputStreamReader input=new InputStreamReader(System.in);
       BufferedReader in=new BufferedReader(input);
     try {
      line=in.readLine();
      int i=Integer.parseInt(line);
      return i;
     }
     catch(Exception e) {
     return -1;
      }
     }
    }
    
    class Vehicle{
        private int number;
        private String sign;
        private String type;
        private String time;
        private int position;
    
        public Vehicle(int number, String sign, String type, String time, int position){
            this.number=number;
            this.sign=sign;
            this.type=type;
            this.time=time;
            this.position=position;
        }
    
        public void setNumber(int n){
            number=n;
        }
    
        public void setSign(String s){
            sign=s;
        }
    
        public void setType(String t){
            type=t;
        }
    
        public void setTime(String t){
            time=t;
        }
    
        public void setPosition(int p){
            position=p;
        }
    
        public int getNumber(){
            return number;
        }
    
        public String getSign(){
            return sign;
        }
    
        public String getType(){
            return type;
        }
    
        public String getTime(){
            return time;
        }
    
        public int getPosition(){
            return position;
        }
    
    }
    
    class MyUtils{
        public static final String DATE_FORMAT_NOW = "HH:mm:ss";
        public static String findTime(){
            Calendar cal = Calendar.getInstance();
            SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
            return sdf.format(cal.getTime());
        }
    
        public static int seq_search(Vehicle table[], String findSign, boolean theseis[]){
            for(int i=0; i<table.length; i++){
                if(theseis[i]==true)
                if(table[i].getSign().equals(findSign))
                    return i;
            }
            return -1;
        }
    
        public static int bin_search(Vehicle table[], int findPosition){
    
            int left=0;
            int right=table.length-1;
            int mid;
            int pos=-1;
    
            while(pos==-1&&left<=right){
                mid=(left+right)/2;
                if(findPosition<table[mid].getPosition())
                    right=mid-1;
                else if(findPosition>table[mid].getPosition())
                    left=mid+1;
                else pos=mid;
            }
            return pos;
        }
    
        public static int protropi(boolean table[]){
            for(int i=0; i<table.length; i++){
                if(table[i]==false)
                    return i;
            }
            return -1;
        }
    
        public static void printVehicle(int i, Vehicle table[], DecimalFormat df){
            System.out.println("Aukson Arithmos: "+table[i].getNumber());
            System.out.println("Pinakida: "+table[i].getSign());
            System.out.println("Typos: "+table[i].getType());
            System.out.println("Hmeromhnia: "+table[i].getTime());
            System.out.println("Thesi: "+table[i].getPosition()+1);
            System.out.println("H xreosi einai: "+df.format(calculateCost(table, table[i].getTime(), table[i].getPosition())));
        }
    
        public static double calculateCost(Vehicle table[], String time, int position){
    	double cost;
    	String a,b;
    	double  dx,dy,lx,ly;
    	double p1,p2;
    
    	a=table[position].getTime().substring(6,8);
    	b=time.substring(6,8);
    
    	dx=Double.parseDouble(a);
    	dy=Double.parseDouble(b);
    
    	lx=Double.parseDouble(table[position].getTime().substring(3,5));
    	ly=Double.parseDouble(time.substring(3,5));
    
    	p1=ly+(dy/100);
    	p2=lx+(dx/100);
    	cost=(p1-p2)*20;
    
            return cost;
        }
    
        [U]public static boolean checkSign(String sign){
            char ch;
    
            for(int i=0; i<3; i++){
                ch=sign.charAt(i);
                if(!(ch>=65&&ch<=90))
                    return true;
            }
    
            ch=sign.charAt(3);
            if(ch!='-')
                return true;
    
            for(int i=0; i<8; i++){
                ch=sign.charAt(i);
                if(!(ch>=48&&ch<=57))
                    return true;
            }[/U]
    
            return false;
        }
    
        //bubbleSort
    
        //insertionSort
    
        //selectionSort
    
        //quickSort
    
        //mergeSort
    }
    
    public class VehicleDemo{
        public static void main(String args[]) throws IOException{
    
            int choice;
            int counter=1;
            int number;
            String sign;
            String type;
            String time;
            int position;
            boolean rightSign=true;
            int signLength;
    
            Vehicle table[]=new Vehicle[10];
            boolean theseis[]=new boolean[10]; //edo apothikeuetai true an h thesi parking einai gemati, false an den einai
            DecimalFormat df = new DecimalFormat("###.##");
            
            for(int i=0; i<theseis.length; i++) //adeiazoume tis theseis
                theseis[i]=false;
    
            boolean menu=false;
            do{
    
            System.out.println("      Arxiko menu");
            System.out.println("1. Proselefsi Oximatos");
            System.out.println("2. Emfanisi Stixion Oximatos");
            System.out.println("3. Taksinomisi");
            System.out.println("4. Lista Theseon Parking");
            System.out.println("5. Telos");
            System.out.print("      Doste epilogi (1-4): ");
    
    
            choice=UserInput.getInteger();
    
                menu:{
            if(choice==1){
                boolean flag=false;
    
                for(int i=0; i<theseis.length; i++)
                    if(theseis[i]==false)
                        flag=true;
    
                if(flag==false)
                    System.out.println("Den uparxei keni thesi");
                    
    
                if(flag==true){
                    System.out.print("Sas protinoume autin tin thesi: ");
    
                    int protinomeno_pos;
                    protinomeno_pos=MyUtils.protropi(theseis);
    
                    System.out.println(protinomeno_pos+1);
    
                System.out.println();
    
                number=counter; 
    
                [U]do{ 
                System.out.println("Give Sign: ");
                sign=UserInput.getString();
                signLength=sign.length();
                
                if(signLength==8)
                rightSign=MyUtils.checkSign(sign);
                
                }while(rightSign=true);[/U]
    
                System.out.println("Dose ton typo tou autokinitou: ");
                type=UserInput.getString();
    
                time=MyUtils.findTime();
    
                System.out.println("Dose thn thesi parking");
                position=UserInput.getInteger();
                position--;
                if(position!=protinomeno_pos)
                    if(theseis[position]==true){
                        System.out.println("H thesi den einai keni");
                        break menu;
                    }
    
                if(position>10){
                    System.out.println("Lathos arithmos thesis");
                    break menu;
                }
                table[position]=new Vehicle(number, sign, type, time, position);
    
                theseis[position]=true;
    
                counter++;//to counter auksanete kata 1, etsi to epomeno autokinito tha exei +1 aukson arithmo
                }
    
            }
                }
            if(choice==2){
                System.out.println("Dialekse tropo anazitisis");
                System.out.println("1. Me bash thn pinakida");
                System.out.println("2. Me bash ton arithmo thesis");
    
                int choose;
                choose=UserInput.getInteger();
    
                if(choose==1){
                    System.out.println("Dose ton arithmo ths pinakidas");
                    String findSign;
                    findSign=UserInput.getString();
                    int done;
                    done=MyUtils.seq_search(table, findSign, theseis);
    
                    if(done==-1)
                        System.out.println("To oxima den brethike");
                    else{
                        System.out.println("To oxima brethike");
                        MyUtils.printVehicle(done, table, df);
                    }
                }
    
                if(choose==2){
                    System.out.println("Dose ton arithmo thesis"); //problhma an o pinakas Vehicle den einai gematos
                    int findPosition;
                    findPosition=UserInput.getInteger();
                    findPosition--;
                    int done;
                    done=MyUtils.bin_search(table, findPosition);
    
                    if(done==-1)
                        System.out.println("To oxima den brethike");
                    else{
                        System.out.println("To oxima brethike");
                        MyUtils.printVehicle(done, table, df);
                    }
                }
                
    
            }
    
            //choice 3
    
            if(choice==4){
                System.out.println("Lista theseon parking:");
                for(int i=0; i<theseis.length; i++){
                    if(theseis[i]==true)
                        System.out.println("Thesi "+(i+1)+": Kateillhmeni me arithmo pinakidas autokinitou "+table[i].getSign());
    
                    if(theseis[i]==false)
                        System.out.println("Thesi "+(i+1)+": Diathesimi");
                }
                
            }
    
            if(choice==5){
                System.out.println("Telos programmatos");
                menu=true;
                }
            }while(menu==false);
    
        }
    }
    I underline the code I am talking about.
    What I want to accomplish is to give a String with this format: XXX-####
    where X=a character A-Z and #=a number 0-9.

    The thing is that this doesnt work. I use the Ascii table to check every character, but I dont know if this is even possible with java(compared to Assembly I studied recently).

    Any idea? Thanks.

    EDIT: In other words I want the user to give a String and check if the string has the certain format, if not, let him give the string again.

    EDIT2: THe method gives false if the sign is right, and true if the sign is wrong, if I did it vice versa I had a problem with do-while..
    Last edited by Sotsiak; 10-23-2010 at 06:59 PM.

  2. #2
    Kay
    Kay is offline Member
    Join Date
    Oct 2010
    Posts
    1
    Rep Power
    0

    Default

    It is simple very simple but please try to think for yourself :) I know you can make it I believe in you huhu


    Very very very easy
    Last edited by Kay; 10-23-2010 at 07:07 PM.

  3. #3
    joshdgreen's Avatar
    joshdgreen is offline Senior Member
    Join Date
    Oct 2010
    Location
    Colorado Springs, CO
    Posts
    212
    Rep Power
    4

    Default

    In this line:

    Java Code:
            for(int i=0; i<8; i++){
                ch=sign.charAt(i);


    Why do you check for 8 numbers? Your example looks like you only want to check 4 numbers.
    Sincerely, Joshua Green
    Please REP if I help :)

Similar Threads

  1. replaceALL(char oldChar, char newChar) method
    By arson09 in forum New To Java
    Replies: 0
    Last Post: 04-28-2010, 05:48 AM
  2. Char to Bit String
    By Krooger in forum New To Java
    Replies: 2
    Last Post: 01-29-2010, 02:26 AM
  3. drawing char by char with Graphics
    By diggitydoggz in forum New To Java
    Replies: 5
    Last Post: 12-27-2008, 12:49 PM
  4. char to string
    By kian_hong2000 in forum New To Java
    Replies: 2
    Last Post: 08-25-2008, 01:51 PM
  5. Help with, String, Char
    By lenny in forum New To Java
    Replies: 1
    Last Post: 07-25-2007, 02:58 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •