Using Switch Statement to Read and Write Data to File C

#1

  • New D.I.C Head

Reputation: 0

  • View blog
  • Posts: 16
  • Joined: 07-March fifteen

How to combine Switch Statement and Reading Information from Text file

Posted 07 March 2015 - 08:08 AM

Hello All,

I know how to apply Switch Argument. I also know How to read data from text file and display onto IDE console.
But, I am not sure even where to begin where I accept to combine this 2 together.
I am assigned a homework where I have 15 lines of data and values are separated by comma. we tin can say xv rows and 3 columns.

Posted Image

public static void main (String args[]) throws FileNotFoundException 	{ 		Scanner file = new Scanner(new File("data.txt")); 	   	file.close(); 		 	   	 	       		Scanner input = new Scanner(System.in); 	    String strResponse=""; 	    practise 	    { 	      Arrangement.out.println("\nChapter 7"); 	       	      Organisation.out.println("L - Bail Length"); 	       	      System.out.println("North - Bond Numbers (i=unmarried, 2=double, 3=triple)\n"); 	      System.out.println("Q - quit program."); 	      strResponse = input.nextLine(); 	      strResponse = strResponse.toUpperCase(); 	       	    	    	       	    }  	    	        	    while (!strResponse.equals("Q")); 	    System.out.println("\north\nThank y'all and have a nice day."); 	 	     	   input.shut(); 	         }            


Is This A Proficient Question/Topic? 0

  • +

#2 g00se User is offline

Reputation: 3744

  • View blog
  • Posts: 17,121
  • Joined: twenty-September 08

Re: How to combine Switch Statement and Reading Data from Text file

Posted 07 March 2015 - 09:36 AM

Quote

Merely, I am non sure even where to begin where I have to combine this two together.

The reason for that is simple: there is no identify for switch in that assignment

#3 Java.Coder User is offline

  • New D.I.C Head

Reputation: 0

  • View blog
  • Posts: 16
  • Joined: 07-March 15

Re: How to combine Switch Statement and Reading Data from Text file

Posted 07 March 2015 - x:27 AM

And so what exercise you lot advise? How tin can I read information if my input is either bond length or number of bond

#4 g00se User is offline

Reputation: 3744

  • View blog
  • Posts: 17,121
  • Joined: xx-September 08

Re: How to combine Switch Statement and Reading Data from Text file

Posted 07 March 2015 - 10:35 AM

Yes, i suppose if yous need to ask them which column they are using, then yous could use a switch

I would tend to make a List of some kind of data-encapsulating object. You could and so iterate that List

#5 jon.kiparsky User is offline

Reputation: 12351

  • View blog
  • Posts: xx,991
  • Joined: 19-March xi

Re: How to combine Switch Statement and Reading Data from Text file

Posted 07 March 2015 - 10:40 AM

You should beginning past stating explicitly what y'all're trying to do. This is useful for people like me, who are interested in helping yous effigy out what you lot need to do, only it'due south much more of import for yous, considering spelling out everything yous're trying to do will greatly help you in thinking about how to get in that location. Don't starting time with the tools you desire to use, start with the effect yous're trying to produce. And then state how you recall yous want to get that effect.

#6 Java.Coder User is offline

  • New D.I.C Head

Reputation: 0

  • View blog
  • Posts: xvi
  • Joined: 07-March 15

Re: How to combine Switch Statement and Reading Information from Text file

Posted 07 March 2015 - 11:23 AM

@jon.kiparsky

Let say I take these data in (data.txt)

C|C,370,0.154
C||C,680,0.13
C|||C,890,0.12
C|H,435,0.11
C|North,305,0.15
C|O,360,0.14
C|F,450,0.14
C|C,340,0.18
O|H,500,0.x
O|O,220,0.15
O|Si,375,0.16
Due north|H,430,0.10
N|O,250,0.12
F|F,160,0.14
H|H,435,0.074

Covalent bail(|=Unmarried, ||=double, and |||= triple bond), Bail Energy, Bond Length

And so If I enter N (number of bonds) in console so side by side thing it asks me select from: unmarried, double, or triple

If I select Double or Two then my Output would exist

C||C,680,0.13

Example 2 If I enter L(length of bonds) in console
then in nextline I enter 0.12
then out would be
C|||C,890,0.12
N|O,250,0.12

In uncomplicated words. If i input something my output would other two associated chemic and concrete backdrop
and if at that place are multiple row that match my input then print all of them.

Thanks for your assist

I am not sure why it is not displaying Epitome I accept attached in Starting time post. It would have made trouble simple for you lot guys to empathize.

#7 g00se User is offline

Reputation: 3744

  • View blog
  • Posts: 17,121
  • Joined: 20-September 08

Re: How to combine Switch Statement and Reading Information from Text file

Posted 07 March 2015 - 12:37 PM

Quote

Information technology would have made problem simple for y'all guys to empathise.

I have seen it, understood information technology and made my recommendation ;)

#eight jon.kiparsky User is offline

Reputation: 12351

  • View blog
  • Posts: 20,991
  • Joined: 19-March xi

Re: How to combine Switch Statement and Reading Data from Text file

Posted 07 March 2015 - 04:13 PM

Quote

I am not certain why it is not displaying Image I have attached in First post.

Images are non a useful way to communicate data in text-axial medium. (such every bit, for example, a forum). Some people have visual impairments and won't get anything from them, others (like me) cake ads, which ends up blocking images - which is generally fine, since images posted on forums are for the most function content-free.
Furthermore, it'due south useful for you to state the trouble, and your proposed solution, in your own words. After all, what yous ultimately want is to state the solution in words the machine understands, isn't it?

So how do y'all want to go about solving this? Y'all say you lot want to use a switch, merely yous don't say what yous desire to apply it for. Again, I think your focus should be on figuring out the contours of your solution, and simply afterwards the tools that you'll utilize to implement that solution. In my mind, this trouble is non an exception to my general rule that the correct number of switches in a well-written Java programme is naught.

One question yous might ask yourself: is there an object here? Is there a office of this problem which looks like a petty slice of information that knows how to do a few tricks? Thinking large-motion picture, if your goal is to learn Java, that means learning to write Java-like solutions to issues. That ways thinking of your solutions in terms of objects and their interactions.

#9 Java.Coder User is offline

  • New D.I.C Head

Reputation: 0

  • View blog
  • Posts: 16
  • Joined: 07-March 15

Re: How to combine Switch Statement and Reading Data from Text file

Posted 07 March 2015 - 09:21 PM

Ok I have decided to put information into ArrayList encounter I get to motility little forward with this assignment

Now I have few questions

How do I read specific line from arraylist?

{ 	public static void main (String args[]) throws FileNotFoundException{ 	   System.out.println("\nChapter 7 P7.21\north"); 	{ 	Cord token1 = ""; 	{ 	    Scanner file = new Scanner(new File("data.txt")).useDelimiter(",\\s*"); 	    List<String> temps = new ArrayList<Cord>(); 	    while (file.hasNextLine()){ 	    	token1 = file.next(); 	    	temps.add(token1); 	    } 	      	      file.shut(); 	     Cord[] tempsArray = temps.toArray(new String[0]); 	     for (String s : tempsArray){ 	    	 [b]System.out.println(s)[/b]; 	     } 	     Scanner input = new Scanner(System.in); 	     String strResponse=""; { 	    	    do 	    { 	      System.out.println("Fifty - Bond Length"); 	      System.out.println("North - Bond Numbers (1 = single, 2 =double, 3=triple)\north"); 	       	      System.out.println("Q - quit program."); 	      strResponse = input.nextLine(); 	      strResponse = strResponse.toUpperCase(); 	    	    } 	      while (!strResponse.equals("Q")); 	    System.out.println("\due north\nThank you and take a nice 24-hour interval."); 		   input.close();  		   file.close(); 	   } 	     	           } 	 	} 	 } }            

#10 jon.kiparsky User is offline

Reputation: 12351

  • View blog
  • Posts: 20,991
  • Joined: xix-March 11

Re: How to combine Switch Statement and Reading Data from Text file

Posted 07 March 2015 - 09:41 PM

Quote

How do I read specific line from arraylist?

You lot can access elements of an ArrayList by index:

ArrayList<String> al = getAnArrayListFromSomewhere(); String someString = al.get(3); // get the fourth chemical element in the list            

You can also access all of the elements with an enhanced for loop:

ArrayList<String> al = getAnArrayListFromSomewhere(); for (String s: al) {   if (matchesSomeCondition(s)) {     takeSomeAction(s);   }            

The latter is probably the correct manner to find the elements from the file which match some condition.

You can as well use the indexOf method to find the index of a particular object in the list, if you already have a reference to that object. This won't exercise y'all much good here.

#11 g00se User is offline

Reputation: 3744

  • View blog
  • Posts: 17,121
  • Joined: 20-September 08

Re: How to combine Switch Statement and Reading Data from Text file

Posted 08 March 2015 - 06:27 AM

Re-read my comment. What you're doing currently volition get you a long listing of unrelated strings, which won't help you

#12 Java.Coder User is offline

  • New D.I.C Head

Reputation: 0

  • View blog
  • Posts: 16
  • Joined: 07-March fifteen

Re: How to combine Switch Statement and Reading Data from Text file

Posted 08 March 2015 - 06:twenty PM

ok guys I am trying difficult to become this affair to piece of work
when I start Program

It should Give me Three Selection

Quote

Chapter 7 P7.21
Fifty - Bond Length
N - Bond Numbers (1 = unmarried, 2 =double, 3=triple)
Q - quit program.

It did that before
Now After calculation Switch statement
it gives me

Quote

Chapter seven P7.21
Fifty - Bond Length
N - Bond Numbers (1 = single, 2 =double, 3=triple)
Number of bonds

my trouble is I want number of bonds to announced merely if user input"N"
Second Trouble is It does not show Q = Quit Program
It shows this after I printing merely input of N

Quote

Chapter seven P7.21

50 - Bond Length
Northward - Bond Numbers (1 = single, 2 =double, 3=triple)

Number of bonds
ane
C|C,370,0.154
Q - quit program.
Exception in thread "main" java.util.NoSuchElementException: No line establish
at java.util.Scanner.nextLine(Unknown Source)
at Patel_Prafulkumar.main(Patel_Prafulkumar.coffee:47)

public static void main (String args[]) throws FileNotFoundException 	   	{ 		Scanner file = new Scanner(new File("data.txt")); 		file.close(); 		 	    Scanner input = new Scanner(System.in); 	     String strResponse="";  	    do 	    {  	    System.out.println("\nChapter seven P7.21\n"); 	      System.out.println("\nL - Bail Length"); 	       	      System.out.println("N - Bond Numbers (ane = unmarried, 2 =double, three=triple)\n"); 	       	      input.equals("n"); 	    	  { 	    	  String bondnumber = ""; 	    	  System.out.println("Number of bonds"); 	    	Scanner myInput = new Scanner(System.in); 	    	int Bond = myInput.nextInt(); 	    	myInput.close(); 	    	switch  (Bond){ 	    	case 1 : bondnumber = "C|C,370,0.154"  ; 	    			intermission; 	    	case ii : bondnumber = "C||C,680,0.13"; 	    			suspension; 	    	case iii : bondnumber = "C|||C,890,0.12"; 	    			intermission; 	    	default: Arrangement.out.println("Invalid Number entered."); 	    			break; 	    	} 	    	System.out.println(bondnumber); 	    	 	    		 	    	} 	    	 	    	 	       	      System.out.println("Q - quit program."); 	      strResponse = input.nextLine(); 	      strResponse = strResponse.toUpperCase(); 	    	    } 	      while (!strResponse.equals("Q")); 	    System.out.println("\n\nThank yous and have a nice day."); 		   input.close();  		    	   } 	     	           }            

#13 jon.kiparsky User is offline

Reputation: 12351

  • View blog
  • Posts: 20,991
  • Joined: nineteen-March 11

Re: How to combine Switch Statement and Reading Information from Text file

Posted 08 March 2015 - 06:23 PM

I think yous want an if at line xvi?

(that doesn't explain why you've got that stuff hardcoded in the switch - that's making no sense to me at all)

#14 Java.Coder User is offline

  • New D.I.C Head

Reputation: 0

  • View blog
  • Posts: sixteen
  • Joined: 07-March xv

Re: How to combine Switch Statement and Reading Data from Text file

Posted 08 March 2015 - 06:28 PM

How can I take more than than one string in case 1
I desire program to print

Quote

C|C,370,0.154
C|H,435,0.11
C|N,305,0.15
C|O,360,0.14
C|F,450,0.14
C|C,340,0.18
O|H,500,0.x
O|O,220,0.fifteen
O|Si,375,0.16
North|H,430,0.10
North|O,250,0.12
F|F,160,0.14
H|H,435,0.074

View Postjon.kiparsky, on 08 March 2015 - 06:23 PM, said:

I remember you want an if at line 16?

(that doesn't explain why you've got that stuff hardcoded in the switch - that'south making no sense to me at all)

Can you tell me exact lawmaking at line sixteen. delight i dont take much time ponder and wonder. it is due in next two hours.

I did what I know. It may not exist correct something is better than nothing. I dont know how to read specific line from file to satisfy switch statement.

#fifteen jon.kiparsky User is offline

Reputation: 12351

  • View blog
  • Posts: 20,991
  • Joined: xix-March 11

Re: How to combine Switch Argument and Reading Data from Text file

Posted 08 March 2015 - 06:32 PM

That's the trouble with switches. Y'all can't get dynamic information into them, because they're defined at compile time. So, to be precise, y'all can't read in information from a file at run fourth dimension and get information technology into your switch.

Have yous noticed how we've been telling you that this isn't a adept place to use a switch? It'due south still non a good identify to employ a switch. The problem hither is to read the data from the file and decide if information technology matches the criteria set past the user - it'due south not nigh writing the information into your source code.

Using Switch Statement to Read and Write Data to File C

Source: https://www.dreamincode.net/forums/topic/371651-how-to-combine-switch-statement-and-reading-data-from-text-file/

0 Response to "Using Switch Statement to Read and Write Data to File C"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel